mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 15:15:38 -05:00
- Refactor version 1 of post notificaitons to use the common persist_notificaiton and send_notification_to_queue methods.
- It would be nice to refactor the send_sms and send_email tasks to use these common functions as well, that way I can get rid of the new Notifications.from_v2_api_request method. - Still not happy with the format of the errors. Would like to find a happy place, where the message is descript enough that we do not need external documentation to explain the error. Perhaps we still only need documentation to explain the trial mode concept.
This commit is contained in:
@@ -4,7 +4,6 @@ from notifications_utils.template import Template
|
||||
|
||||
from app.celery import provider_tasks
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
||||
from app.errors import InvalidRequest
|
||||
from app.models import SMS_TYPE, Notification, KEY_TYPE_TEST, EMAIL_TYPE
|
||||
from app.notifications.validators import check_sms_content_char_count
|
||||
from app.v2.errors import BadRequestError
|
||||
@@ -16,21 +15,24 @@ def create_content_for_notification(template, personalisation):
|
||||
personalisation,
|
||||
renderer=PassThrough()
|
||||
)
|
||||
if template_object.missing_data:
|
||||
message = 'Missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
||||
errors = {'template': [message]}
|
||||
raise BadRequestError(errors)
|
||||
|
||||
if template_object.additional_data:
|
||||
message = 'Personalisation not needed for template: {}'.format(", ".join(template_object.additional_data))
|
||||
errors = {'template': [message]}
|
||||
raise BadRequestError(fields=errors)
|
||||
check_placeholders(template_object)
|
||||
|
||||
if template_object.template_type == SMS_TYPE:
|
||||
check_sms_content_char_count(template_object.replaced_content_count)
|
||||
return template_object
|
||||
|
||||
|
||||
def check_placeholders(template_object):
|
||||
if template_object.missing_data:
|
||||
message = 'Template missing personalisation: {}'.format(", ".join(template_object.missing_data))
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
if template_object.additional_data:
|
||||
message = 'Template personalisation not needed for template: {}'.format(
|
||||
", ".join(template_object.additional_data))
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
def persist_notification(template_id,
|
||||
template_version,
|
||||
recipient,
|
||||
@@ -39,14 +41,14 @@ def persist_notification(template_id,
|
||||
notification_type,
|
||||
api_key_id,
|
||||
key_type):
|
||||
notification = Notification.from_v2_api_request(template_id,
|
||||
template_version,
|
||||
recipient,
|
||||
service_id,
|
||||
personalisation,
|
||||
notification_type,
|
||||
api_key_id,
|
||||
key_type)
|
||||
notification = Notification.from_v2_api_request(template_id=template_id,
|
||||
template_version=template_version,
|
||||
recipient=recipient,
|
||||
service_id=service_id,
|
||||
personalisation=personalisation,
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type)
|
||||
dao_create_notification(notification)
|
||||
return notification
|
||||
|
||||
@@ -67,7 +69,7 @@ def send_notification_to_queue(notification, research_mode):
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Failed to send to SQS exception")
|
||||
dao_delete_notifications_and_history_by_id(notification.id)
|
||||
raise InvalidRequest(message="Internal server error", status_code=500)
|
||||
raise
|
||||
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at)
|
||||
|
||||
@@ -7,26 +7,25 @@ from flask import (
|
||||
current_app,
|
||||
json
|
||||
)
|
||||
|
||||
from notifications_utils.template import Template
|
||||
from notifications_utils.renderers import PassThrough
|
||||
from notifications_utils.template import Template
|
||||
|
||||
from app import api_user, create_uuid, statsd_client
|
||||
from app.clients.email.aws_ses import get_aws_responses
|
||||
from app import api_user, create_uuid, DATETIME_FORMAT, statsd_client
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
||||
from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST, Notification, KEY_TYPE_NORMAL, EMAIL_TYPE
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
services_dao,
|
||||
notifications_dao
|
||||
)
|
||||
from app.models import KEY_TYPE_TEAM
|
||||
from app.models import SMS_TYPE
|
||||
from app.notifications.process_client_response import (
|
||||
validate_callback_data,
|
||||
process_sms_client_response
|
||||
)
|
||||
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
|
||||
from app.notifications.validators import check_service_message_limit, check_template_is_for_notification_type, \
|
||||
check_template_is_active
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
sms_template_notification_schema,
|
||||
@@ -35,6 +34,7 @@ from app.schemas import (
|
||||
notifications_statistics_schema,
|
||||
day_schema
|
||||
)
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.utils import pagination_links
|
||||
|
||||
notifications = Blueprint('notifications', __name__)
|
||||
@@ -45,7 +45,6 @@ from app.errors import (
|
||||
)
|
||||
|
||||
register_errors(notifications)
|
||||
from app.celery import provider_tasks
|
||||
|
||||
|
||||
@notifications.route('/notifications/email/ses', methods=['POST'])
|
||||
@@ -219,10 +218,8 @@ def send_notification(notification_type):
|
||||
|
||||
check_service_message_limit(api_user.key_type, service)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=notification['template'],
|
||||
service_id=service_id
|
||||
)
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=notification['template'],
|
||||
service_id=service_id)
|
||||
|
||||
check_template_is_for_notification_type(notification_type, template.template_type)
|
||||
check_template_is_active(template)
|
||||
@@ -231,18 +228,20 @@ def send_notification(notification_type):
|
||||
|
||||
_service_allowed_to_send_to(notification, service)
|
||||
|
||||
notification_id = create_uuid()
|
||||
notification.update({"template_version": template.version})
|
||||
saved_notification = None
|
||||
if not _simulated_recipient(notification['to'], notification_type):
|
||||
persist_notification(
|
||||
service,
|
||||
notification_id,
|
||||
notification,
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT),
|
||||
notification_type,
|
||||
str(api_user.id),
|
||||
api_user.key_type
|
||||
)
|
||||
saved_notification = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=notification['to'],
|
||||
service_id=service.id,
|
||||
personalisation=notification.get('personalisation', None),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_user.id,
|
||||
key_type=api_user.key_type)
|
||||
send_notification_to_queue(saved_notification, service.research_mode)
|
||||
|
||||
notification_id = create_uuid() if saved_notification is None else saved_notification.id
|
||||
notification.update({"template_version": template.version})
|
||||
|
||||
return jsonify(
|
||||
data=get_notification_return_data(
|
||||
@@ -271,43 +270,6 @@ def _simulated_recipient(to_address, notification_type):
|
||||
else to_address in current_app.config['SIMULATED_EMAIL_ADDRESSES'])
|
||||
|
||||
|
||||
def persist_notification(
|
||||
service,
|
||||
notification_id,
|
||||
notification,
|
||||
created_at,
|
||||
notification_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
):
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, notification_type, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
research_mode = service.research_mode or key_type == KEY_TYPE_TEST
|
||||
if notification_type == SMS_TYPE:
|
||||
provider_tasks.deliver_sms.apply_async(
|
||||
[str(notification_id)],
|
||||
queue='send-sms' if not research_mode else 'research-mode'
|
||||
)
|
||||
if notification_type == EMAIL_TYPE:
|
||||
provider_tasks.deliver_email.apply_async(
|
||||
[str(notification_id)],
|
||||
queue='send-email' if not research_mode else 'research-mode'
|
||||
)
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Failed to send to SQS exception")
|
||||
dao_delete_notifications_and_history_by_id(notification_id)
|
||||
raise InvalidRequest(message="Internal server error", status_code=500)
|
||||
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(notification_type, notification_id, created_at)
|
||||
)
|
||||
|
||||
|
||||
def _service_allowed_to_send_to(notification, service):
|
||||
if not service_allowed_to_send_to(notification['to'], service, api_user.key_type):
|
||||
if api_user.key_type == KEY_TYPE_TEAM:
|
||||
|
||||
@@ -18,18 +18,15 @@ def check_template_is_for_notification_type(notification_type, template_type):
|
||||
if notification_type != template_type:
|
||||
raise BadRequestError(
|
||||
message="{0} template is not suitable for {1} notification".format(template_type,
|
||||
notification_type),
|
||||
fields=[{"template": "{0} template is not suitable for {1} notification".format(template_type,
|
||||
notification_type)}])
|
||||
notification_type))
|
||||
|
||||
|
||||
def check_template_is_active(template):
|
||||
if template.archived:
|
||||
raise BadRequestError(fields=[{"template": "has been deleted"}],
|
||||
message="Template has been deleted")
|
||||
raise BadRequestError(message="Template has been deleted")
|
||||
|
||||
|
||||
def service_can_send_to_recipient(send_to, key_type, service, recipient_type):
|
||||
def service_can_send_to_recipient(send_to, key_type, service):
|
||||
if not service_allowed_to_send_to(send_to, service, key_type):
|
||||
if key_type == KEY_TYPE_TEAM:
|
||||
message = 'Can’t send to this recipient using a team-only API key'
|
||||
@@ -38,9 +35,7 @@ def service_can_send_to_recipient(send_to, key_type, service, recipient_type):
|
||||
'Can’t send to this recipient when service is in trial mode '
|
||||
'– see https://www.notifications.service.gov.uk/trial-mode'
|
||||
)
|
||||
raise BadRequestError(
|
||||
fields={recipient_type: [message]}
|
||||
)
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
def check_sms_content_char_count(content_count):
|
||||
@@ -48,6 +43,5 @@ def check_sms_content_char_count(content_count):
|
||||
if (
|
||||
content_count > char_count_limit
|
||||
):
|
||||
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
|
||||
errors = {'content': [message]}
|
||||
raise BadRequestError(fields=errors)
|
||||
message = 'Content for template has a character count greater than the limit of {}'.format(char_count_limit)
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
Reference in New Issue
Block a user