mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
refactor post_notification to separate sms/email and letter flows
This commit is contained in:
@@ -234,11 +234,11 @@ post_letter_response = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def create_post_sms_response_from_notification(notification, body, from_number, url_root, scheduled_for):
|
def create_post_sms_response_from_notification(notification, content, from_number, url_root, scheduled_for):
|
||||||
noti = __create_notification_response(notification, url_root, scheduled_for)
|
noti = __create_notification_response(notification, url_root, scheduled_for)
|
||||||
noti['content'] = {
|
noti['content'] = {
|
||||||
'from_number': from_number,
|
'from_number': from_number,
|
||||||
'body': body
|
'body': content
|
||||||
}
|
}
|
||||||
return noti
|
return noti
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
import functools
|
||||||
|
|
||||||
from flask import request, jsonify, current_app
|
from flask import request, jsonify, current_app
|
||||||
|
|
||||||
from app import api_user, authenticated_service
|
from app import api_user, authenticated_service
|
||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.models import SMS_TYPE, EMAIL_TYPE, PRIORITY
|
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE, PRIORITY
|
||||||
from app.notifications.process_notifications import (
|
from app.notifications.process_notifications import (
|
||||||
persist_notification,
|
persist_notification,
|
||||||
send_notification_to_queue,
|
send_notification_to_queue,
|
||||||
@@ -16,20 +18,28 @@ from app.notifications.validators import (
|
|||||||
validate_template
|
validate_template
|
||||||
)
|
)
|
||||||
from app.schema_validation import validate
|
from app.schema_validation import validate
|
||||||
|
from app.v2.errors import BadRequestError
|
||||||
from app.v2.notifications import v2_notification_blueprint
|
from app.v2.notifications import v2_notification_blueprint
|
||||||
from app.v2.notifications.notification_schemas import (
|
from app.v2.notifications.notification_schemas import (
|
||||||
post_sms_request,
|
post_sms_request,
|
||||||
create_post_sms_response_from_notification,
|
|
||||||
post_email_request,
|
post_email_request,
|
||||||
create_post_email_response_from_notification)
|
post_letter_request,
|
||||||
|
create_post_sms_response_from_notification,
|
||||||
|
create_post_email_response_from_notification,
|
||||||
|
create_post_letter_response_from_notification
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
|
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
|
||||||
def post_notification(notification_type):
|
def post_notification(notification_type):
|
||||||
if notification_type == EMAIL_TYPE:
|
if notification_type == EMAIL_TYPE:
|
||||||
form = validate(request.get_json(), post_email_request)
|
form = validate(request.get_json(), post_email_request)
|
||||||
else:
|
elif notification_type == SMS_TYPE:
|
||||||
form = validate(request.get_json(), post_sms_request)
|
form = validate(request.get_json(), post_sms_request)
|
||||||
|
elif notification_type == LETTER_TYPE:
|
||||||
|
form = validate(request.get_json(), post_letter_request)
|
||||||
|
else:
|
||||||
|
raise BadRequestError(message='Unknown notification type {}'.format(notification_type))
|
||||||
|
|
||||||
check_service_has_permission(notification_type, authenticated_service.permissions)
|
check_service_has_permission(notification_type, authenticated_service.permissions)
|
||||||
|
|
||||||
@@ -38,12 +48,6 @@ def post_notification(notification_type):
|
|||||||
|
|
||||||
check_rate_limiting(authenticated_service, api_user)
|
check_rate_limiting(authenticated_service, api_user)
|
||||||
|
|
||||||
form_send_to = form['phone_number'] if notification_type == SMS_TYPE else form['email_address']
|
|
||||||
send_to = validate_and_format_recipient(send_to=form_send_to,
|
|
||||||
key_type=api_user.key_type,
|
|
||||||
service=authenticated_service,
|
|
||||||
notification_type=notification_type)
|
|
||||||
|
|
||||||
template, template_with_content = validate_template(
|
template, template_with_content = validate_template(
|
||||||
form['template_id'],
|
form['template_id'],
|
||||||
form.get('personalisation', {}),
|
form.get('personalisation', {}),
|
||||||
@@ -51,20 +55,74 @@ def post_notification(notification_type):
|
|||||||
notification_type,
|
notification_type,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if notification_type == LETTER_TYPE:
|
||||||
|
notification = process_letter_notification(
|
||||||
|
form=form,
|
||||||
|
api_key=api_user,
|
||||||
|
template=template,
|
||||||
|
service=authenticated_service,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
notification = process_sms_or_email_notification(
|
||||||
|
form=form,
|
||||||
|
notification_type=notification_type,
|
||||||
|
api_key=api_user,
|
||||||
|
template=template,
|
||||||
|
service=authenticated_service
|
||||||
|
)
|
||||||
|
|
||||||
|
if notification_type == SMS_TYPE:
|
||||||
|
sms_sender = authenticated_service.sms_sender or current_app.config.get('FROM_NUMBER')
|
||||||
|
create_resp_partial = functools.partial(
|
||||||
|
create_post_sms_response_from_notification,
|
||||||
|
from_number=sms_sender
|
||||||
|
)
|
||||||
|
elif notification_type == EMAIL_TYPE:
|
||||||
|
create_resp_partial = functools.partial(
|
||||||
|
create_post_email_response_from_notification,
|
||||||
|
subject=template_with_content.subject,
|
||||||
|
email_from=authenticated_service.email_from
|
||||||
|
)
|
||||||
|
elif notification_type == LETTER_TYPE:
|
||||||
|
create_resp_partial = functools.partial(
|
||||||
|
create_post_letter_response_from_notification,
|
||||||
|
subject=template_with_content.subject,
|
||||||
|
)
|
||||||
|
|
||||||
|
resp = create_resp_partial(
|
||||||
|
notification=notification,
|
||||||
|
content=str(template_with_content),
|
||||||
|
url_root=request.url_root,
|
||||||
|
scheduled_for=scheduled_for
|
||||||
|
)
|
||||||
|
return jsonify(resp), 201
|
||||||
|
|
||||||
|
|
||||||
|
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service):
|
||||||
|
form_send_to = form['email_address'] if notification_type == EMAIL_TYPE else form['phone_number']
|
||||||
|
|
||||||
|
send_to = validate_and_format_recipient(send_to=form_send_to,
|
||||||
|
key_type=api_key.key_type,
|
||||||
|
service=service,
|
||||||
|
notification_type=notification_type)
|
||||||
|
|
||||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||||
simulated = simulated_recipient(send_to, notification_type)
|
simulated = simulated_recipient(send_to, notification_type)
|
||||||
|
|
||||||
notification = persist_notification(template_id=template.id,
|
notification = persist_notification(
|
||||||
template_version=template.version,
|
template_id=template.id,
|
||||||
recipient=form_send_to,
|
template_version=template.version,
|
||||||
service=authenticated_service,
|
recipient=form_send_to,
|
||||||
personalisation=form.get('personalisation', None),
|
service=service,
|
||||||
notification_type=notification_type,
|
personalisation=form.get('personalisation', None),
|
||||||
api_key_id=api_user.id,
|
notification_type=notification_type,
|
||||||
key_type=api_user.key_type,
|
api_key_id=api_key.id,
|
||||||
client_reference=form.get('reference', None),
|
key_type=api_key.key_type,
|
||||||
simulated=simulated)
|
client_reference=form.get('reference', None),
|
||||||
|
simulated=simulated
|
||||||
|
)
|
||||||
|
|
||||||
|
scheduled_for = form.get("scheduled_for", None)
|
||||||
if scheduled_for:
|
if scheduled_for:
|
||||||
persist_scheduled_notification(notification.id, form["scheduled_for"])
|
persist_scheduled_notification(notification.id, form["scheduled_for"])
|
||||||
else:
|
else:
|
||||||
@@ -72,24 +130,19 @@ def post_notification(notification_type):
|
|||||||
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
||||||
send_notification_to_queue(
|
send_notification_to_queue(
|
||||||
notification=notification,
|
notification=notification,
|
||||||
research_mode=authenticated_service.research_mode,
|
research_mode=service.research_mode,
|
||||||
queue=queue_name
|
queue=queue_name
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
||||||
|
|
||||||
if notification_type == SMS_TYPE:
|
return notification
|
||||||
sms_sender = authenticated_service.sms_sender or current_app.config.get('FROM_NUMBER')
|
|
||||||
resp = create_post_sms_response_from_notification(notification=notification,
|
|
||||||
body=str(template_with_content),
|
def process_letter_notification(*, form, api_key, template, service):
|
||||||
from_number=sms_sender,
|
# create job
|
||||||
url_root=request.url_root,
|
|
||||||
scheduled_for=scheduled_for)
|
# create notification
|
||||||
else:
|
|
||||||
resp = create_post_email_response_from_notification(notification=notification,
|
# trigger build_dvla_file task
|
||||||
content=str(template_with_content),
|
raise NotImplementedError
|
||||||
subject=template_with_content.subject,
|
|
||||||
email_from=authenticated_service.email_from,
|
|
||||||
url_root=request.url_root,
|
|
||||||
scheduled_for=scheduled_for)
|
|
||||||
return jsonify(resp), 201
|
|
||||||
|
|||||||
Reference in New Issue
Block a user