mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Merge pull request #1253 from alphagov/add-multiple-reply-to-email-endpoints
Add multiple reply to email endpoints
This commit is contained in:
@@ -1,32 +1,39 @@
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.errors import InvalidRequest
|
||||
from app.models import ServiceEmailReplyTo
|
||||
|
||||
|
||||
def create_or_update_email_reply_to(service_id, email_address):
|
||||
reply_to = dao_get_reply_to_by_service_id(service_id)
|
||||
if reply_to:
|
||||
reply_to.email_address = email_address
|
||||
dao_update_reply_to_email(reply_to)
|
||||
else:
|
||||
reply_to = ServiceEmailReplyTo(service_id=service_id, email_address=email_address)
|
||||
dao_create_reply_to_email_address(reply_to)
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_create_reply_to_email_address(reply_to_email):
|
||||
db.session.add(reply_to_email)
|
||||
|
||||
|
||||
def dao_get_reply_to_by_service_id(service_id):
|
||||
reply_to = db.session.query(
|
||||
ServiceEmailReplyTo
|
||||
).filter(
|
||||
ServiceEmailReplyTo.service_id == service_id
|
||||
).first()
|
||||
).all()
|
||||
return reply_to
|
||||
|
||||
|
||||
def create_or_update_email_reply_to(service_id, email_address):
|
||||
reply_to = dao_get_reply_to_by_service_id(service_id)
|
||||
if len(reply_to) == 0:
|
||||
reply_to = ServiceEmailReplyTo(service_id=service_id, email_address=email_address)
|
||||
dao_create_reply_to_email_address(reply_to)
|
||||
elif len(reply_to) == 1:
|
||||
reply_to[0].email_address = email_address
|
||||
dao_update_reply_to_email(reply_to[0])
|
||||
else:
|
||||
# Once we move allowing multiple email address this methods will be removed
|
||||
raise InvalidRequest(
|
||||
"Multiple reply to email addresses were found, this method should not be used.",
|
||||
status_code=500
|
||||
)
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_create_reply_to_email_address(reply_to_email):
|
||||
db.session.add(reply_to_email)
|
||||
|
||||
|
||||
@transactional
|
||||
def dao_update_reply_to_email(reply_to):
|
||||
db.session.add(reply_to)
|
||||
|
||||
@@ -1343,3 +1343,11 @@ class ServiceEmailReplyTo(db.Model):
|
||||
is_default = db.Column(db.Boolean, nullable=False, default=True)
|
||||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
||||
|
||||
def serialize(self):
|
||||
return {
|
||||
'email_address': self.email_address,
|
||||
'is_default': self.is_default,
|
||||
'created_at': self.created_at,
|
||||
'updated_at': self.updated_at
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ from app.dao.service_whitelist_dao import (
|
||||
dao_add_and_commit_whitelisted_contacts,
|
||||
dao_remove_service_whitelist
|
||||
)
|
||||
from app.dao.service_email_reply_to_dao import create_or_update_email_reply_to
|
||||
from app.dao.service_email_reply_to_dao import create_or_update_email_reply_to, dao_get_reply_to_by_service_id
|
||||
from app.dao.provider_statistics_dao import get_fragment_count
|
||||
from app.dao.users_dao import get_user_by_id
|
||||
from app.errors import (
|
||||
@@ -523,6 +523,12 @@ def create_one_off_notification(service_id):
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/email-reply-to', methods=["GET"])
|
||||
def get_email_reply_to_addresses(service_id):
|
||||
result = dao_get_reply_to_by_service_id(service_id)
|
||||
return jsonify([i.serialize() for i in result]), 200
|
||||
|
||||
|
||||
@service_blueprint.route('/unique', methods=["GET"])
|
||||
def is_service_name_unique():
|
||||
name, email_from = check_request_args(request)
|
||||
|
||||
Reference in New Issue
Block a user