2021-03-10 13:55:06 +00:00
|
|
|
|
from flask import Blueprint, current_app, jsonify, request
|
2016-10-28 17:10:00 +01:00
|
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
|
from app import api_user, authenticated_service
|
2024-01-22 10:55:09 -08:00
|
|
|
|
from app.aws.s3 import get_personalisation_from_s3, get_phone_number_from_s3
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.dao import notifications_dao
|
2024-08-09 09:18:58 -07:00
|
|
|
|
from app.enums import KeyType, NotificationType
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.errors import InvalidRequest, register_errors
|
2017-05-05 15:23:06 +01:00
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
|
persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
simulated_recipient,
|
2017-05-05 15:23:06 +01:00
|
|
|
|
)
|
|
|
|
|
|
from app.notifications.validators import (
|
2020-06-16 17:55:02 +01:00
|
|
|
|
check_if_service_can_send_to_number,
|
2017-06-29 18:02:21 +01:00
|
|
|
|
service_has_permission,
|
2020-06-16 17:55:02 +01:00
|
|
|
|
validate_template,
|
2017-05-05 15:23:06 +01:00
|
|
|
|
)
|
2025-05-14 15:01:01 -04:00
|
|
|
|
from app.public_schemas.public import PublicNotificationResponseSchema
|
2016-02-03 13:16:19 +00:00
|
|
|
|
from app.schemas import (
|
2016-02-16 11:22:44 +00:00
|
|
|
|
email_notification_schema,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
notifications_filter_schema,
|
|
|
|
|
|
sms_template_notification_schema,
|
2016-02-16 11:22:44 +00:00
|
|
|
|
)
|
2016-10-28 17:10:00 +01:00
|
|
|
|
from app.service.utils import service_allowed_to_send_to
|
2025-05-16 16:23:59 -04:00
|
|
|
|
from app.utils import (
|
|
|
|
|
|
get_public_notify_type_text,
|
|
|
|
|
|
get_template_instance,
|
|
|
|
|
|
pagination_links,
|
|
|
|
|
|
template_model_to_dict,
|
|
|
|
|
|
)
|
2024-05-16 10:17:45 -04:00
|
|
|
|
from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
notifications = Blueprint("notifications", __name__)
|
2016-02-17 17:04:50 +00:00
|
|
|
|
register_errors(notifications)
|
2016-03-21 13:24:37 +00:00
|
|
|
|
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@notifications.route("/notifications/<uuid:notification_id>", methods=["GET"])
|
2016-08-09 13:07:48 +01:00
|
|
|
|
def get_notification_by_id(notification_id):
|
2017-05-05 15:23:06 +01:00
|
|
|
|
notification = notifications_dao.get_notification_with_personalisation(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
str(authenticated_service.id), notification_id, key_type=None
|
|
|
|
|
|
)
|
2025-05-09 15:14:07 -04:00
|
|
|
|
|
2024-01-22 10:55:09 -08:00
|
|
|
|
if notification.job_id is not None:
|
|
|
|
|
|
notification.personalisation = get_personalisation_from_s3(
|
|
|
|
|
|
notification.service_id,
|
|
|
|
|
|
notification.job_id,
|
|
|
|
|
|
notification.job_row_number,
|
|
|
|
|
|
)
|
|
|
|
|
|
recipient = get_phone_number_from_s3(
|
|
|
|
|
|
notification.service_id,
|
|
|
|
|
|
notification.job_id,
|
|
|
|
|
|
notification.job_row_number,
|
|
|
|
|
|
)
|
|
|
|
|
|
notification.to = recipient
|
|
|
|
|
|
notification.normalised_to = recipient
|
2025-05-09 15:14:07 -04:00
|
|
|
|
|
2025-05-16 16:23:59 -04:00
|
|
|
|
if not hasattr(notification, "body") or not notification.body:
|
|
|
|
|
|
template_dict = template_model_to_dict(notification.template)
|
|
|
|
|
|
template = get_template_instance(
|
|
|
|
|
|
template_dict, notification.personalisation or {}
|
|
|
|
|
|
)
|
|
|
|
|
|
notification.body = template.content_with_placeholders_filled_in
|
|
|
|
|
|
|
|
|
|
|
|
schema = PublicNotificationResponseSchema()
|
2025-06-09 13:39:08 -04:00
|
|
|
|
schema.context = {
|
|
|
|
|
|
"notification_instance": notification,
|
2025-06-10 10:33:59 -07:00
|
|
|
|
"template_subject": (
|
|
|
|
|
|
getattr(template, "subject", None) if hasattr(template, "subject") else None
|
|
|
|
|
|
),
|
2025-06-09 13:39:08 -04:00
|
|
|
|
}
|
2025-05-16 16:23:59 -04:00
|
|
|
|
serialized = schema.dump(notification)
|
|
|
|
|
|
|
2025-05-09 15:14:07 -04:00
|
|
|
|
return jsonify(data={"notification": serialized}), 200
|
2016-01-19 11:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@notifications.route("/notifications", methods=["GET"])
|
2016-03-01 13:30:10 +00:00
|
|
|
|
def get_all_notifications():
|
2024-05-15 08:34:56 -07:00
|
|
|
|
current_app.logger.debug("enter get_all_notifications()")
|
2022-05-06 15:25:14 +01:00
|
|
|
|
data = notifications_filter_schema.load(request.args)
|
2024-05-15 08:34:56 -07:00
|
|
|
|
current_app.logger.debug(
|
|
|
|
|
|
f"get_all_notifications() data {data} request.args {request.args}"
|
|
|
|
|
|
)
|
2022-05-06 15:25:14 +01:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
include_jobs = data.get("include_jobs", False)
|
|
|
|
|
|
page = data.get("page", 1)
|
|
|
|
|
|
page_size = data.get("page_size", current_app.config.get("API_PAGE_SIZE"))
|
|
|
|
|
|
limit_days = data.get("limit_days")
|
2016-03-01 13:30:10 +00:00
|
|
|
|
|
2016-03-21 12:37:34 +00:00
|
|
|
|
pagination = notifications_dao.get_notifications_for_service(
|
2017-05-05 15:23:06 +01:00
|
|
|
|
str(authenticated_service.id),
|
2016-08-09 13:07:48 +01:00
|
|
|
|
personalisation=True,
|
2016-03-21 12:37:34 +00:00
|
|
|
|
filter_dict=data,
|
2016-04-19 10:52:52 +01:00
|
|
|
|
page=page,
|
2016-04-28 16:10:35 +01:00
|
|
|
|
page_size=page_size,
|
2016-06-30 18:43:15 +01:00
|
|
|
|
limit_days=limit_days,
|
2016-09-15 16:01:26 +01:00
|
|
|
|
key_type=api_user.key_type,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
include_jobs=include_jobs,
|
|
|
|
|
|
)
|
2025-05-16 16:23:59 -04:00
|
|
|
|
|
|
|
|
|
|
serialized = []
|
2024-01-22 10:55:09 -08:00
|
|
|
|
for notification in pagination.items:
|
2025-05-28 13:47:35 -04:00
|
|
|
|
personalisation = notification.personalisation
|
|
|
|
|
|
|
2024-01-22 10:55:09 -08:00
|
|
|
|
if notification.job_id is not None:
|
2025-05-28 13:47:35 -04:00
|
|
|
|
personalisation = get_personalisation_from_s3(
|
2024-01-22 10:55:09 -08:00
|
|
|
|
notification.service_id,
|
|
|
|
|
|
notification.job_id,
|
|
|
|
|
|
notification.job_row_number,
|
|
|
|
|
|
)
|
|
|
|
|
|
recipient = get_phone_number_from_s3(
|
|
|
|
|
|
notification.service_id,
|
|
|
|
|
|
notification.job_id,
|
|
|
|
|
|
notification.job_row_number,
|
|
|
|
|
|
)
|
2025-05-28 13:47:35 -04:00
|
|
|
|
# Safe to set dynamically for serialization purposes
|
2024-01-22 10:55:09 -08:00
|
|
|
|
notification.to = recipient
|
|
|
|
|
|
notification.normalised_to = recipient
|
|
|
|
|
|
|
2025-05-28 13:47:35 -04:00
|
|
|
|
subject = None
|
|
|
|
|
|
|
|
|
|
|
|
if not getattr(notification, "body", None):
|
2025-05-16 16:23:59 -04:00
|
|
|
|
template_dict = template_model_to_dict(notification.template)
|
2025-05-28 13:47:35 -04:00
|
|
|
|
template = get_template_instance(template_dict, personalisation or {})
|
2025-05-16 16:23:59 -04:00
|
|
|
|
notification.body = template.content_with_placeholders_filled_in
|
|
|
|
|
|
if hasattr(template, "subject"):
|
2025-05-28 13:47:35 -04:00
|
|
|
|
subject = template.subject
|
|
|
|
|
|
|
|
|
|
|
|
notification.personalisation = personalisation
|
2025-05-16 16:23:59 -04:00
|
|
|
|
|
|
|
|
|
|
schema = PublicNotificationResponseSchema()
|
|
|
|
|
|
schema.context = {"notification_instance": notification}
|
2025-05-28 13:47:35 -04:00
|
|
|
|
notification_data = schema.dump(notification)
|
|
|
|
|
|
|
|
|
|
|
|
if subject is not None:
|
|
|
|
|
|
notification_data["subject"] = subject
|
|
|
|
|
|
|
|
|
|
|
|
serialized.append(notification_data)
|
2025-05-16 16:23:59 -04:00
|
|
|
|
|
2024-05-14 07:23:47 -07:00
|
|
|
|
result = jsonify(
|
2025-05-16 16:23:59 -04:00
|
|
|
|
notifications=serialized,
|
2024-05-15 08:34:56 -07:00
|
|
|
|
page_size=page_size,
|
|
|
|
|
|
total=pagination.total,
|
|
|
|
|
|
links=pagination_links(
|
|
|
|
|
|
pagination, ".get_all_notifications", **request.args.to_dict()
|
2023-08-29 14:54:30 -07:00
|
|
|
|
),
|
|
|
|
|
|
)
|
2024-05-15 08:34:56 -07:00
|
|
|
|
return result, 200
|
2016-03-01 13:30:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
@notifications.route("/notifications/<string:notification_type>", methods=["POST"])
|
2016-02-25 11:35:32 +00:00
|
|
|
|
def send_notification(notification_type):
|
2024-02-28 12:40:52 -05:00
|
|
|
|
if notification_type not in {NotificationType.SMS, NotificationType.EMAIL}:
|
2025-05-16 16:23:59 -04:00
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
f"{notification_type} notification type is not supported", 400
|
|
|
|
|
|
)
|
2016-02-24 09:55:05 +00:00
|
|
|
|
|
2022-05-06 15:25:14 +01:00
|
|
|
|
notification_form = (
|
2023-08-29 14:54:30 -07:00
|
|
|
|
sms_template_notification_schema
|
2024-02-28 12:40:52 -05:00
|
|
|
|
if notification_type == NotificationType.SMS
|
2023-08-29 14:54:30 -07:00
|
|
|
|
else email_notification_schema
|
2016-02-29 11:03:48 +00:00
|
|
|
|
).load(request.get_json())
|
2017-04-26 15:56:45 +01:00
|
|
|
|
|
2020-03-04 17:04:11 +00:00
|
|
|
|
template, template_with_content = validate_template(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
template_id=notification_form["template"],
|
|
|
|
|
|
personalisation=notification_form.get("personalisation", {}),
|
2020-03-04 17:04:11 +00:00
|
|
|
|
service=authenticated_service,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
notification_type=notification_type,
|
2020-03-04 17:04:11 +00:00
|
|
|
|
)
|
2016-06-14 15:07:23 +01:00
|
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
|
_service_allowed_to_send_to(notification_form, authenticated_service)
|
2025-05-16 16:23:59 -04:00
|
|
|
|
|
2017-06-30 15:00:44 +01:00
|
|
|
|
if not service_has_permission(notification_type, authenticated_service.permissions):
|
|
|
|
|
|
raise InvalidRequest(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
{
|
|
|
|
|
|
"service": [
|
2025-05-16 16:23:59 -04:00
|
|
|
|
f"Cannot send {get_public_notify_type_text(notification_type, plural=True)}"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
]
|
|
|
|
|
|
},
|
2025-05-16 16:23:59 -04:00
|
|
|
|
400,
|
2017-06-30 15:00:44 +01:00
|
|
|
|
)
|
2017-06-29 18:02:21 +01:00
|
|
|
|
|
2024-02-28 12:40:52 -05:00
|
|
|
|
if notification_type == NotificationType.SMS:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
check_if_service_can_send_to_number(
|
|
|
|
|
|
authenticated_service, notification_form["to"]
|
|
|
|
|
|
)
|
2021-03-19 16:42:55 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
simulated = simulated_recipient(notification_form["to"], notification_type)
|
2025-05-16 16:23:59 -04:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
notification_model = persist_notification(
|
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
2025-05-16 16:23:59 -04:00
|
|
|
|
recipient=notification_form["to"],
|
2023-08-29 14:54:30 -07:00
|
|
|
|
service=authenticated_service,
|
2025-05-16 16:23:59 -04:00
|
|
|
|
personalisation=notification_form.get("personalisation"),
|
2023-08-29 14:54:30 -07:00
|
|
|
|
notification_type=notification_type,
|
|
|
|
|
|
api_key_id=api_user.id,
|
|
|
|
|
|
key_type=api_user.key_type,
|
|
|
|
|
|
simulated=simulated,
|
|
|
|
|
|
reply_to_text=template.reply_to_text,
|
|
|
|
|
|
)
|
2023-08-29 16:21:18 -07:00
|
|
|
|
|
2025-05-16 16:23:59 -04:00
|
|
|
|
if not simulated:
|
|
|
|
|
|
send_notification_to_queue(notification=notification_model, queue=None)
|
2017-01-17 12:08:24 +00:00
|
|
|
|
else:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
current_app.logger.debug(
|
2025-05-16 16:23:59 -04:00
|
|
|
|
f"POST simulated notification for id: {notification_model.id}"
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2025-05-16 16:23:59 -04:00
|
|
|
|
|
2017-01-18 09:56:26 +00:00
|
|
|
|
notification_form.update({"template_version": template.version})
|
2017-01-17 12:08:24 +00:00
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
return (
|
|
|
|
|
|
jsonify(
|
|
|
|
|
|
data=get_notification_return_data(
|
|
|
|
|
|
notification_model.id, notification_form, template_with_content
|
|
|
|
|
|
)
|
|
|
|
|
|
),
|
|
|
|
|
|
201,
|
|
|
|
|
|
)
|
2016-06-15 12:11:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_notification_return_data(notification_id, notification, template):
|
|
|
|
|
|
output = {
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"template_version": notification["template_version"],
|
|
|
|
|
|
"notification": {"id": notification_id},
|
|
|
|
|
|
"body": template.content_with_placeholders_filled_in,
|
2016-06-15 12:11:48 +01:00
|
|
|
|
}
|
2023-08-29 14:54:30 -07:00
|
|
|
|
if hasattr(template, "subject"):
|
|
|
|
|
|
output["subject"] = template.subject
|
2016-06-15 12:11:48 +01:00
|
|
|
|
return output
|
2016-05-19 16:42:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
def _service_allowed_to_send_to(notification, service):
|
2023-08-29 14:54:30 -07:00
|
|
|
|
if not service_allowed_to_send_to(notification["to"], service, api_user.key_type):
|
2024-01-18 10:28:50 -05:00
|
|
|
|
if api_user.key_type == KeyType.TEAM:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
message = "Can’t send to this recipient using a team-only API key"
|
2016-10-03 10:57:10 +01:00
|
|
|
|
else:
|
|
|
|
|
|
message = (
|
2023-08-29 14:54:30 -07:00
|
|
|
|
"Can’t send to this recipient when service is in trial mode "
|
|
|
|
|
|
"– see https://www.notifications.service.gov.uk/trial-mode"
|
2016-10-03 10:57:10 +01:00
|
|
|
|
)
|
2025-05-16 16:23:59 -04:00
|
|
|
|
raise InvalidRequest({"to": [message]}, 400)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_template_object_for_notification(template, personalisation):
|
2020-04-06 14:25:43 +01:00
|
|
|
|
template_object = template._as_utils_template_with_personalisation(personalisation)
|
2016-12-09 15:56:25 +00:00
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
|
if template_object.missing_data:
|
2023-08-29 14:54:30 -07:00
|
|
|
|
message = "Missing personalisation: {}".format(
|
|
|
|
|
|
", ".join(template_object.missing_data)
|
|
|
|
|
|
)
|
2025-05-16 16:23:59 -04:00
|
|
|
|
raise InvalidRequest({"template": [message]}, 400)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
|
|
|
|
|
|
if (
|
2024-02-28 12:40:52 -05:00
|
|
|
|
template_object.template_type == NotificationType.SMS
|
2023-08-29 14:54:30 -07:00
|
|
|
|
and template_object.is_message_too_long()
|
2016-10-03 10:57:10 +01:00
|
|
|
|
):
|
2025-05-16 16:23:59 -04:00
|
|
|
|
raise InvalidRequest(
|
|
|
|
|
|
{
|
|
|
|
|
|
"content": [
|
|
|
|
|
|
f"Content has a character count greater than the limit of {SMS_CHAR_COUNT_LIMIT}"
|
|
|
|
|
|
]
|
|
|
|
|
|
},
|
|
|
|
|
|
400,
|
2023-08-29 14:54:30 -07:00
|
|
|
|
)
|
2016-10-03 10:57:10 +01:00
|
|
|
|
return template_object
|