mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Add methods to add/update letter contacts for a service that handle defaults properly
This commit is contained in:
@@ -28,7 +28,7 @@ def create_or_update_letter_contact(service_id, contact_block):
|
||||
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
|
||||
# TODO: 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
|
||||
@@ -43,3 +43,65 @@ def dao_create_letter_contact(letter_contact):
|
||||
@transactional
|
||||
def dao_update_letter_contact(letter_contact):
|
||||
db.session.add(letter_contact)
|
||||
|
||||
|
||||
@transactional
|
||||
def add_letter_contact_for_service(service_id, contact_block, is_default):
|
||||
old_default = _get_existing_default(service_id)
|
||||
if is_default:
|
||||
_reset_old_default_to_false(old_default)
|
||||
else:
|
||||
_raise_when_no_default(old_default)
|
||||
|
||||
new_letter_contact = ServiceLetterContact(
|
||||
service_id=service_id,
|
||||
contact_block=contact_block,
|
||||
is_default=is_default
|
||||
)
|
||||
db.session.add(new_letter_contact)
|
||||
return new_letter_contact
|
||||
|
||||
|
||||
@transactional
|
||||
def update_letter_contact(service_id, letter_contact_id, contact_block, is_default):
|
||||
old_default = _get_existing_default(service_id)
|
||||
# if we want to make this the default, ensure there are no other existing defaults
|
||||
if is_default:
|
||||
_reset_old_default_to_false(old_default)
|
||||
else:
|
||||
if old_default.id == letter_contact_id:
|
||||
raise InvalidRequest("You must have at least one letter contact as the default.", 400)
|
||||
|
||||
letter_contact_update = ServiceLetterContact.query.get(letter_contact_id)
|
||||
letter_contact_update.contact_block = contact_block
|
||||
letter_contact_update.is_default = is_default
|
||||
db.session.add(letter_contact_update)
|
||||
return letter_contact_update
|
||||
|
||||
|
||||
def _get_existing_default(service_id):
|
||||
letter_contacts = dao_get_letter_contacts_by_service_id(service_id=service_id)
|
||||
if letter_contacts:
|
||||
old_default = [x for x in letter_contacts if x.is_default]
|
||||
if len(old_default) == 1:
|
||||
return old_default[0]
|
||||
else:
|
||||
raise Exception(
|
||||
"There should only be one default letter contact for each service. Service {} has {}".format(
|
||||
service_id,
|
||||
len(old_default)
|
||||
)
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def _reset_old_default_to_false(old_default):
|
||||
if old_default:
|
||||
old_default.is_default = False
|
||||
db.session.add(old_default)
|
||||
|
||||
|
||||
def _raise_when_no_default(old_default):
|
||||
# check that the update is not updating the only default to false
|
||||
if not old_default:
|
||||
raise InvalidRequest("You must have at least one letter contact as the default.", 400)
|
||||
|
||||
Reference in New Issue
Block a user