2017-09-22 10:02:59 +01:00
|
|
|
from sqlalchemy import desc
|
|
|
|
|
|
2017-09-08 11:14:26 +01:00
|
|
|
from app import db
|
2021-04-14 07:11:01 +01:00
|
|
|
from app.dao.dao_utils import autocommit
|
2017-09-14 12:23:52 +01:00
|
|
|
from app.errors import InvalidRequest
|
2018-04-25 16:34:36 +01:00
|
|
|
from app.exceptions import ArchiveValidationError
|
2017-09-08 11:14:26 +01:00
|
|
|
from app.models import ServiceEmailReplyTo
|
|
|
|
|
|
|
|
|
|
|
2017-09-14 12:23:52 +01:00
|
|
|
def dao_get_reply_to_by_service_id(service_id):
|
|
|
|
|
reply_to = db.session.query(
|
|
|
|
|
ServiceEmailReplyTo
|
|
|
|
|
).filter(
|
2018-04-25 12:19:12 +01:00
|
|
|
ServiceEmailReplyTo.service_id == service_id,
|
|
|
|
|
ServiceEmailReplyTo.archived == False # noqa
|
2017-09-22 10:02:59 +01:00
|
|
|
).order_by(desc(ServiceEmailReplyTo.is_default), desc(ServiceEmailReplyTo.created_at)).all()
|
2017-09-14 12:23:52 +01:00
|
|
|
return reply_to
|
|
|
|
|
|
|
|
|
|
|
2017-09-21 17:02:58 +01:00
|
|
|
def dao_get_reply_to_by_id(service_id, reply_to_id):
|
|
|
|
|
reply_to = db.session.query(
|
|
|
|
|
ServiceEmailReplyTo
|
|
|
|
|
).filter(
|
|
|
|
|
ServiceEmailReplyTo.service_id == service_id,
|
2018-04-25 12:19:12 +01:00
|
|
|
ServiceEmailReplyTo.id == reply_to_id,
|
|
|
|
|
ServiceEmailReplyTo.archived == False # noqa
|
2017-09-21 17:02:58 +01:00
|
|
|
).order_by(ServiceEmailReplyTo.created_at).one()
|
|
|
|
|
return reply_to
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2017-09-20 12:20:12 +01:00
|
|
|
def add_reply_to_email_address_for_service(service_id, email_address, is_default):
|
2017-09-14 17:54:38 +01:00
|
|
|
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_reply_to = ServiceEmailReplyTo(service_id=service_id, email_address=email_address, is_default=is_default)
|
|
|
|
|
db.session.add(new_reply_to)
|
|
|
|
|
return new_reply_to
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2017-09-20 12:20:12 +01:00
|
|
|
def update_reply_to_email_address(service_id, reply_to_id, email_address, is_default):
|
2017-09-14 17:54:38 +01:00
|
|
|
old_default = _get_existing_default(service_id)
|
|
|
|
|
if is_default:
|
|
|
|
|
_reset_old_default_to_false(old_default)
|
|
|
|
|
else:
|
|
|
|
|
if old_default.id == reply_to_id:
|
|
|
|
|
raise InvalidRequest("You must have at least one reply to email address as the default.", 400)
|
|
|
|
|
|
|
|
|
|
reply_to_update = ServiceEmailReplyTo.query.get(reply_to_id)
|
|
|
|
|
reply_to_update.email_address = email_address
|
|
|
|
|
reply_to_update.is_default = is_default
|
|
|
|
|
db.session.add(reply_to_update)
|
|
|
|
|
return reply_to_update
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 07:11:01 +01:00
|
|
|
@autocommit
|
2018-04-25 16:34:36 +01:00
|
|
|
def archive_reply_to_email_address(service_id, reply_to_id):
|
|
|
|
|
reply_to_archive = ServiceEmailReplyTo.query.filter_by(
|
|
|
|
|
id=reply_to_id,
|
|
|
|
|
service_id=service_id
|
|
|
|
|
).one()
|
|
|
|
|
|
|
|
|
|
if reply_to_archive.is_default:
|
|
|
|
|
raise ArchiveValidationError("You cannot delete a default email reply to address")
|
|
|
|
|
|
|
|
|
|
reply_to_archive.archived = True
|
|
|
|
|
|
|
|
|
|
db.session.add(reply_to_archive)
|
|
|
|
|
return reply_to_archive
|
|
|
|
|
|
|
|
|
|
|
2017-09-14 17:54:38 +01:00
|
|
|
def _get_existing_default(service_id):
|
|
|
|
|
existing_reply_to = dao_get_reply_to_by_service_id(service_id=service_id)
|
|
|
|
|
if existing_reply_to:
|
|
|
|
|
old_default = [x for x in existing_reply_to if x.is_default]
|
|
|
|
|
if len(old_default) == 1:
|
|
|
|
|
return old_default[0]
|
|
|
|
|
else:
|
2017-09-20 11:58:18 +01:00
|
|
|
raise Exception(
|
2017-09-14 17:54:38 +01:00
|
|
|
"There should only be one default reply to email for each service. Service {} has {}".format(
|
|
|
|
|
service_id, len(old_default)))
|
2017-09-20 11:58:18 +01:00
|
|
|
return None
|
2017-09-14 17:54:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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 reply to email address as the default.", 400)
|