mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
Add dao to store letter contacts and upsert
This commit is contained in:
45
app/dao/service_letter_contact_dao.py
Normal file
45
app/dao/service_letter_contact_dao.py
Normal 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)
|
||||||
@@ -140,15 +140,16 @@ def update_service(service_id):
|
|||||||
# Capture the status change here as Marshmallow changes this later
|
# Capture the status change here as Marshmallow changes this later
|
||||||
service_going_live = fetched_service.restricted and not req_json.get('restricted', True)
|
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 = dict(service_schema.dump(fetched_service).data.items())
|
||||||
current_data.update(request.get_json())
|
current_data.update(request.get_json())
|
||||||
update_dict = service_schema.load(current_data).data
|
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:
|
if 'sms_sender' in req_json:
|
||||||
insert_or_update_service_sms_sender(fetched_service, req_json['sms_sender'])
|
insert_or_update_service_sms_sender(fetched_service, req_json['sms_sender'])
|
||||||
dao_update_service(update_dict)
|
|
||||||
|
|
||||||
if service_going_live:
|
if service_going_live:
|
||||||
send_notification_to_service_users(
|
send_notification_to_service_users(
|
||||||
|
|||||||
66
tests/app/dao/test_service_letter_contact_dao.py
Normal file
66
tests/app/dao/test_service_letter_contact_dao.py
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from app.dao.service_letter_contact_dao import (
|
||||||
|
create_or_update_letter_contact,
|
||||||
|
dao_get_letter_contacts_by_service_id,
|
||||||
|
)
|
||||||
|
from app.errors import InvalidRequest
|
||||||
|
from app.models import ServiceLetterContact
|
||||||
|
from tests.app.db import create_letter_contact, create_service
|
||||||
|
|
||||||
|
|
||||||
|
def test_dao_get_letter_contacts_by_service_id(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
default_letter_contact = create_letter_contact(service=service, contact_block='Edinburgh, ED1 1AA')
|
||||||
|
another_letter_contact = create_letter_contact(service=service, contact_block='Cardiff, CA1 2DB')
|
||||||
|
|
||||||
|
results = dao_get_letter_contacts_by_service_id(service_id=service.id)
|
||||||
|
|
||||||
|
assert len(results) == 2
|
||||||
|
assert default_letter_contact in results
|
||||||
|
assert another_letter_contact in results
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_or_update_letter_contact_creates_new_entry(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
|
||||||
|
create_or_update_letter_contact(service.id, 'Cardiff, CA1 2DB')
|
||||||
|
|
||||||
|
letter_contacts = dao_get_letter_contacts_by_service_id(service.id)
|
||||||
|
|
||||||
|
assert ServiceLetterContact.query.count() == 1
|
||||||
|
assert letter_contacts[0].service.id == service.id
|
||||||
|
assert letter_contacts[0].contact_block == 'Cardiff, CA1 2DB'
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_or_update_letter_contact_does_not_create_another_entry(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
create_letter_contact(service, 'London, NW1 2DB')
|
||||||
|
create_or_update_letter_contact(service.id, 'Bristol, BR1 2DB')
|
||||||
|
|
||||||
|
letter_contacts = dao_get_letter_contacts_by_service_id(service.id)
|
||||||
|
|
||||||
|
assert len(letter_contacts) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_or_update_letter_contact_updates_existing_entry(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
create_letter_contact(service, 'London, NW1 2DB')
|
||||||
|
|
||||||
|
create_or_update_letter_contact(service.id, 'Bristol, BR1 2DB')
|
||||||
|
|
||||||
|
letter_contact = dao_get_letter_contacts_by_service_id(service.id)
|
||||||
|
|
||||||
|
assert len(letter_contact) == 1
|
||||||
|
assert letter_contact[0].service.id == service.id
|
||||||
|
assert letter_contact[0].contact_block == 'Bristol, BR1 2DB'
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_or_update_letter_contact_raises_exception_if_multiple_contact_blocks_exist(notify_db_session):
|
||||||
|
service = create_service()
|
||||||
|
create_letter_contact(service=service, contact_block='Edinburgh, ED1 1AA')
|
||||||
|
create_letter_contact(service=service, contact_block='Manchester, MA1 2BB', is_default=False)
|
||||||
|
|
||||||
|
with pytest.raises(expected_exception=InvalidRequest) as e:
|
||||||
|
create_or_update_letter_contact(service_id=service.id, contact_block='Swansea, SN1 3CC')
|
||||||
|
assert e.value.message == "Multiple letter contacts were found, this method should not be used."
|
||||||
Reference in New Issue
Block a user