mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 06:52:06 -05:00
Merge pull request #1277 from alphagov/imdad-katie-store-multiple-letter-contacts
[2/4] Upsert into ServiceLetterContact table when updating a service
This commit is contained in:
107
app/dao/service_letter_contact_dao.py
Normal file
107
app/dao/service_letter_contact_dao.py
Normal file
@@ -0,0 +1,107 @@
|
||||
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:
|
||||
# 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
|
||||
)
|
||||
|
||||
|
||||
@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)
|
||||
|
||||
|
||||
@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)
|
||||
@@ -46,8 +46,14 @@ from app.dao.service_whitelist_dao import (
|
||||
dao_add_and_commit_whitelisted_contacts,
|
||||
dao_remove_service_whitelist
|
||||
)
|
||||
from app.dao.service_email_reply_to_dao import create_or_update_email_reply_to, dao_get_reply_to_by_service_id, \
|
||||
add_reply_to_email_address_for_service, update_reply_to_email_address, dao_get_reply_to_by_id
|
||||
from app.dao.service_email_reply_to_dao import (
|
||||
add_reply_to_email_address_for_service,
|
||||
create_or_update_email_reply_to,
|
||||
dao_get_reply_to_by_id,
|
||||
dao_get_reply_to_by_service_id,
|
||||
update_reply_to_email_address
|
||||
)
|
||||
from app.dao.service_letter_contact_dao import create_or_update_letter_contact
|
||||
from app.dao.provider_statistics_dao import get_fragment_count
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.errors import (
|
||||
@@ -144,15 +150,19 @@ 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 'letter_contact_block' in req_json:
|
||||
create_or_update_letter_contact(fetched_service.id, req_json['letter_contact_block'])
|
||||
|
||||
if service_going_live:
|
||||
send_notification_to_service_users(
|
||||
|
||||
Reference in New Issue
Block a user