Add dao to store letter contacts and upsert

This commit is contained in:
Imdad Ahad
2017-09-25 12:26:29 +01:00
parent 9a1410f48d
commit fa3f4f3c20
3 changed files with 116 additions and 4 deletions

View File

@@ -0,0 +1,45 @@
from app import db
from app.dao.dao_utils import transactional
from app.errors import InvalidRequest
from app.models import ServiceLetterContact
def dao_get_letter_contacts_by_service_id(service_id):
letter_contacts = db.session.query(
ServiceLetterContact
).filter(
ServiceLetterContact.service_id == service_id
).order_by(
ServiceLetterContact.created_at
).all()
return letter_contacts
def create_or_update_letter_contact(service_id, contact_block):
letter_contacts = dao_get_letter_contacts_by_service_id(service_id)
if len(letter_contacts) == 0:
letter_contact = ServiceLetterContact(
service_id=service_id,
contact_block=contact_block
)
dao_create_letter_contact(letter_contact)
elif len(letter_contacts) == 1:
letter_contacts[0].contact_block = contact_block
dao_update_letter_contact(letter_contacts[0])
else:
# Once we move allowing letter contact blocks, this method will be removed
raise InvalidRequest(
"Multiple letter contacts were found, this method should not be used.",
status_code=500
)
@transactional
def dao_create_letter_contact(letter_contact):
db.session.add(letter_contact)
@transactional
def dao_update_letter_contact(letter_contact):
db.session.add(letter_contact)

View File

@@ -140,15 +140,16 @@ def update_service(service_id):
# Capture the status change here as Marshmallow changes this later
service_going_live = fetched_service.restricted and not req_json.get('restricted', True)
if 'reply_to_email_address' in req_json:
create_or_update_email_reply_to(fetched_service.id, req_json['reply_to_email_address'])
current_data = dict(service_schema.dump(fetched_service).data.items())
current_data.update(request.get_json())
update_dict = service_schema.load(current_data).data
dao_update_service(update_dict)
if 'reply_to_email_address' in req_json:
create_or_update_email_reply_to(fetched_service.id, req_json['reply_to_email_address'])
if 'sms_sender' in req_json:
insert_or_update_service_sms_sender(fetched_service, req_json['sms_sender'])
dao_update_service(update_dict)
if service_going_live:
send_notification_to_service_users(