2018-04-25 12:19:12 +01:00
|
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
|
|
|
2017-11-25 11:31:36 +00:00
|
|
|
|
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
|
|
|
|
|
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
2017-06-13 17:33:04 +01:00
|
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2023-03-02 20:20:31 -05:00
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
|
2017-06-16 16:30:03 +01:00
|
|
|
|
from app.dao.users_dao import get_user_by_id
|
2024-08-09 09:18:58 -07:00
|
|
|
|
from app.enums import KeyType, NotificationType
|
2024-05-30 12:27:07 -07:00
|
|
|
|
from app.errors import BadRequestError
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
|
persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
|
)
|
|
|
|
|
|
from app.notifications.validators import (
|
2023-04-28 12:37:06 -07:00
|
|
|
|
check_service_over_total_message_limit,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
validate_and_format_recipient,
|
|
|
|
|
|
validate_template,
|
|
|
|
|
|
)
|
2017-06-16 16:30:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_created_by(service, created_by_id):
|
|
|
|
|
|
user = get_user_by_id(created_by_id)
|
|
|
|
|
|
if service not in user.services:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
message = (
|
|
|
|
|
|
'Can’t create notification - {} is not part of the "{}" service'.format(
|
|
|
|
|
|
user.name, service.name
|
|
|
|
|
|
)
|
2017-06-16 16:30:03 +01:00
|
|
|
|
)
|
|
|
|
|
|
raise BadRequestError(message=message)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-03-02 20:20:31 -05:00
|
|
|
|
# TODO: possibly unnecessary after removing letters
|
2018-11-30 16:35:00 +00:00
|
|
|
|
def create_one_off_reference(template_type):
|
2018-12-03 11:33:59 +00:00
|
|
|
|
return None
|
2018-11-30 16:35:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-06-13 17:33:04 +01:00
|
|
|
|
def send_one_off_notification(service_id, post_data):
|
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
|
template = dao_get_template_by_id_and_service_id(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_id=post_data["template_id"], service_id=service_id
|
2017-06-13 17:33:04 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
personalisation = post_data.get("personalisation", None)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
2017-06-15 13:40:38 +01:00
|
|
|
|
validate_template(template.id, personalisation, service, template.template_type)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
2024-01-18 10:28:50 -05:00
|
|
|
|
check_service_over_total_message_limit(KeyType.NORMAL, service)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
|
|
|
|
|
validate_and_format_recipient(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
send_to=post_data["to"],
|
2024-01-18 10:28:50 -05:00
|
|
|
|
key_type=KeyType.NORMAL,
|
2017-06-13 17:33:04 +01:00
|
|
|
|
service=service,
|
2018-01-17 15:20:04 +00:00
|
|
|
|
notification_type=template.template_type,
|
2020-07-28 10:19:46 +01:00
|
|
|
|
allow_guest_list_recipients=False,
|
2017-06-13 17:33:04 +01:00
|
|
|
|
)
|
2021-03-19 16:42:55 +00:00
|
|
|
|
client_reference = None
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
validate_created_by(service, post_data["created_by"])
|
2017-06-16 16:30:03 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sender_id = post_data.get("sender_id", None)
|
2017-12-15 17:13:55 +00:00
|
|
|
|
reply_to = get_reply_to_text(
|
|
|
|
|
|
notification_type=template.template_type,
|
|
|
|
|
|
sender_id=sender_id,
|
|
|
|
|
|
service=service,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template=template,
|
2017-12-15 17:13:55 +00:00
|
|
|
|
)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
notification = persist_notification(
|
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
recipient=post_data["to"],
|
2017-06-13 17:33:04 +01:00
|
|
|
|
service=service,
|
|
|
|
|
|
personalisation=personalisation,
|
|
|
|
|
|
notification_type=template.template_type,
|
|
|
|
|
|
api_key_id=None,
|
2024-01-18 10:28:50 -05:00
|
|
|
|
key_type=KeyType.NORMAL,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
created_by_id=post_data["created_by"],
|
2018-11-30 16:35:00 +00:00
|
|
|
|
reply_to_text=reply_to,
|
|
|
|
|
|
reference=create_one_off_reference(template.template_type),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
client_reference=client_reference,
|
2017-06-13 17:33:04 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
2024-08-09 09:11:28 -07:00
|
|
|
|
queue_name = None
|
2018-10-31 14:30:46 +00:00
|
|
|
|
|
2023-03-02 20:20:31 -05:00
|
|
|
|
send_notification_to_queue(
|
|
|
|
|
|
notification=notification,
|
|
|
|
|
|
queue=queue_name,
|
|
|
|
|
|
)
|
2017-06-13 17:33:04 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return {"id": str(notification.id)}
|
2017-11-25 11:31:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-15 17:13:55 +00:00
|
|
|
|
def get_reply_to_text(notification_type, sender_id, service, template):
|
2017-11-25 11:31:36 +00:00
|
|
|
|
reply_to = None
|
2017-12-15 17:13:55 +00:00
|
|
|
|
if sender_id:
|
2018-04-25 14:23:00 +01:00
|
|
|
|
try:
|
2024-02-28 12:40:52 -05:00
|
|
|
|
if notification_type == NotificationType.EMAIL:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
message = "Reply to email address not found"
|
2018-04-25 14:23:00 +01:00
|
|
|
|
reply_to = dao_get_reply_to_by_id(service.id, sender_id).email_address
|
2024-02-28 12:40:52 -05:00
|
|
|
|
elif notification_type == NotificationType.SMS:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
message = "SMS sender not found"
|
|
|
|
|
|
reply_to = dao_get_service_sms_senders_by_id(
|
|
|
|
|
|
service.id, sender_id
|
|
|
|
|
|
).get_reply_to_text()
|
2018-04-25 14:23:00 +01:00
|
|
|
|
except NoResultFound:
|
|
|
|
|
|
raise BadRequestError(message=message)
|
2017-12-15 17:13:55 +00:00
|
|
|
|
else:
|
|
|
|
|
|
reply_to = template.get_reply_to_text()
|
2017-11-25 11:31:36 +00:00
|
|
|
|
return reply_to
|