From f2d4bc795e271f5465753a9d09e208c7c216db53 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Thu, 26 Apr 2018 10:00:11 +0100 Subject: [PATCH] Add DAO function and endpoint for archiving letter contact blocks Added a new DAO function which archives letter contact blocks by setting archived to True. This raises an ArchiveValidationError if trying to archive the default letter block for a service or the default letter contact block for a template. Added a new endpoint for archiving letter contact blocks. --- app/dao/service_letter_contact_dao.py | 29 +++++++++- app/service/rest.py | 8 +++ .../dao/test_service_letter_contact_dao.py | 53 ++++++++++++++++++- tests/app/service/test_rest.py | 32 +++++++++++ 4 files changed, 120 insertions(+), 2 deletions(-) diff --git a/app/dao/service_letter_contact_dao.py b/app/dao/service_letter_contact_dao.py index 9c422ef7c..80facf402 100644 --- a/app/dao/service_letter_contact_dao.py +++ b/app/dao/service_letter_contact_dao.py @@ -3,7 +3,8 @@ from sqlalchemy import desc from app import db from app.dao.dao_utils import transactional from app.errors import InvalidRequest -from app.models import ServiceLetterContact +from app.exceptions import ArchiveValidationError +from app.models import ServiceLetterContact, Template def dao_get_letter_contacts_by_service_id(service_id): @@ -65,6 +66,32 @@ def update_letter_contact(service_id, letter_contact_id, contact_block, is_defau return letter_contact_update +@transactional +def archive_letter_contact(service_id, letter_contact_id): + letter_contact_to_archive = ServiceLetterContact.query.filter_by( + id=letter_contact_id, + service_id=service_id + ).one() + + if _is_template_default(letter_contact_id): + raise ArchiveValidationError("You cannot delete the default letter contact block for a template") + if letter_contact_to_archive.is_default: + raise ArchiveValidationError("You cannot delete a default letter contact block") + + letter_contact_to_archive.archived = True + + db.session.add(letter_contact_to_archive) + return letter_contact_to_archive + + +def _is_template_default(letter_contact_id): + template_defaults = Template.query.filter_by( + service_letter_contact_id=letter_contact_id + ).all() + + return any(template_defaults) + + def _get_existing_default(service_id): letter_contacts = dao_get_letter_contacts_by_service_id(service_id=service_id) if letter_contacts: diff --git a/app/service/rest.py b/app/service/rest.py index 9414ce61c..8a32baebc 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -59,6 +59,7 @@ from app.dao.service_email_reply_to_dao import ( update_reply_to_email_address ) from app.dao.service_letter_contact_dao import ( + archive_letter_contact, dao_get_letter_contacts_by_service_id, dao_get_letter_contact_by_id, add_letter_contact_for_service, @@ -638,6 +639,13 @@ def update_service_letter_contact(service_id, letter_contact_id): return jsonify(data=new_reply_to.serialize()), 200 +@service_blueprint.route('//letter-contact//archive', methods=['POST']) +def delete_service_letter_contact(service_id, letter_contact_id): + archived_letter_contact = archive_letter_contact(service_id, letter_contact_id) + + return jsonify(data=archived_letter_contact.serialize()), 200 + + @service_blueprint.route('//sms-sender', methods=['POST']) def add_service_sms_sender(service_id): dao_fetch_service_by_id(service_id) diff --git a/tests/app/dao/test_service_letter_contact_dao.py b/tests/app/dao/test_service_letter_contact_dao.py index 812aa7fba..0584892b6 100644 --- a/tests/app/dao/test_service_letter_contact_dao.py +++ b/tests/app/dao/test_service_letter_contact_dao.py @@ -4,13 +4,15 @@ from sqlalchemy.exc import SQLAlchemyError from app.dao.service_letter_contact_dao import ( add_letter_contact_for_service, + archive_letter_contact, dao_get_letter_contacts_by_service_id, dao_get_letter_contact_by_id, update_letter_contact ) from app.errors import InvalidRequest +from app.exceptions import ArchiveValidationError from app.models import ServiceLetterContact -from tests.app.db import create_letter_contact, create_service +from tests.app.db import create_letter_contact, create_service, create_template def test_dao_get_letter_contacts_by_service_id(notify_db_session): @@ -170,6 +172,55 @@ def test_update_letter_contact_unset_default_for_only_letter_contact_raises_exce ) +def test_archive_letter_contact(notify_db_session): + service = create_service() + create_letter_contact(service=service, contact_block='Aberdeen, AB12 23X') + letter_contact = create_letter_contact(service=service, contact_block='Edinburgh, ED1 1AA', is_default=False) + + archive_letter_contact(service.id, letter_contact.id) + + assert letter_contact.archived + assert letter_contact.updated_at is not None + + +def test_archive_letter_contact_does_not_archive_a_letter_contact_for_a_different_service( + notify_db_session, + sample_service, +): + service = create_service(service_name="First service") + letter_contact = create_letter_contact( + service=sample_service, + contact_block='Edinburgh, ED1 1AA', + is_default=False) + + with pytest.raises(SQLAlchemyError): + archive_letter_contact(service.id, letter_contact.id) + + assert not letter_contact.archived + + +def test_archive_letter_contact_does_not_archive_a_service_default_letter_contact(notify_db_session): + service = create_service() + letter_contact = create_letter_contact(service=service, contact_block='Edinburgh, ED1 1AA') + + with pytest.raises(ArchiveValidationError) as e: + archive_letter_contact(service.id, letter_contact.id) + + assert 'You cannot delete a default letter contact block' in str(e.value) + + +def test_archive_letter_contact_does_not_archive_a_template_default_letter_contact(notify_db_session): + service = create_service() + create_letter_contact(service=service, contact_block='Edinburgh, ED1 1AA') + template_default = create_letter_contact(service=service, contact_block='Aberdeen, AB12 23X', is_default=False) + create_template(service=service, template_type='letter', reply_to=template_default.id) + + with pytest.raises(ArchiveValidationError) as e: + archive_letter_contact(service.id, template_default.id) + + assert 'You cannot delete the default letter contact block for a template' in str(e.value) + + def test_dao_get_letter_contact_by_id(sample_service): letter_contact = create_letter_contact(service=sample_service, contact_block='Aberdeen, AB12 23X') result = dao_get_letter_contact_by_id(service_id=sample_service.id, letter_contact_id=letter_contact.id) diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 157ed077b..9dd47f2e0 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -2733,6 +2733,38 @@ def test_update_service_letter_contact_returns_404_when_invalid_service_id(clien assert result['message'] == 'No result found' +def test_delete_service_letter_contact_can_archive_letter_contact(admin_request, notify_db_session): + service = create_service() + create_letter_contact(service=service, contact_block='Edinburgh, ED1 1AA') + letter_contact = create_letter_contact(service=service, contact_block='Swansea, SN1 3CC', is_default=False) + + admin_request.post( + 'service.delete_service_letter_contact', + service_id=service.id, + letter_contact_id=letter_contact.id, + ) + + assert letter_contact.archived is True + + +def test_delete_service_letter_contact_returns_400_if_archiving_template_default(admin_request, notify_db_session): + service = create_service() + create_letter_contact(service=service, contact_block='Edinburgh, ED1 1AA') + letter_contact = create_letter_contact(service=service, contact_block='Swansea, SN1 3CC', is_default=False) + create_template(service=service, template_type='letter', reply_to=letter_contact.id) + + response = admin_request.post( + 'service.delete_service_letter_contact', + service_id=service.id, + letter_contact_id=letter_contact.id, + _expected_status=400 + ) + assert response == { + 'message': 'You cannot delete the default letter contact block for a template', + 'result': 'error'} + assert letter_contact.archived is False + + def test_add_service_sms_sender_can_add_multiple_senders(client, notify_db_session): service = create_service() data = {