mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
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.
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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('/<uuid:service_id>/letter-contact/<uuid:letter_contact_id>/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('/<uuid:service_id>/sms-sender', methods=['POST'])
|
||||
def add_service_sms_sender(service_id):
|
||||
dao_fetch_service_by_id(service_id)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user