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:
Katie Smith
2018-04-26 10:00:11 +01:00
parent 472b86f3f4
commit f2d4bc795e
4 changed files with 120 additions and 2 deletions

View File

@@ -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:

View File

@@ -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)