mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-24 01:11:38 -05:00
- note this is an unexpectedly big change. - When we create a service we pass the service id to the persist method. This means that we don't have the service available to check if in research mode. - All calling methods (expecting the one where we use the notify service) have the service available. So rather than reload it I changed the method signature to pass the service, not the ID to persist. - Touches a few places. Note this means that the update or create methods will fall over on a null service. But this seems correct. Goes back to the story which we need to play to make the service available as the API user so that the need to load and pass around services is minimised.
95 lines
5.2 KiB
Python
95 lines
5.2 KiB
Python
from flask import request, jsonify, current_app
|
|
from sqlalchemy.orm.exc import NoResultFound
|
|
|
|
from app import api_user
|
|
from app.dao import services_dao, templates_dao
|
|
from app.models import SMS_TYPE, EMAIL_TYPE
|
|
from app.notifications.process_notifications import (create_content_for_notification,
|
|
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,
|
|
service_can_send_to_recipient,
|
|
check_sms_content_char_count)
|
|
from app.schema_validation import validate
|
|
from app.v2.errors import BadRequestError
|
|
from app.v2.notifications import notification_blueprint
|
|
from app.v2.notifications.notification_schemas import (post_sms_request,
|
|
create_post_sms_response_from_notification, post_email_request,
|
|
create_post_email_response_from_notification)
|
|
|
|
|
|
@notification_blueprint.route('/sms', methods=['POST'])
|
|
def post_sms_notification():
|
|
form = validate(request.get_json(), post_sms_request)
|
|
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
|
|
|
check_service_message_limit(api_user.key_type, service)
|
|
service_can_send_to_recipient(form['phone_number'], api_user.key_type, service)
|
|
|
|
template, template_with_content = __validate_template(form, service, SMS_TYPE)
|
|
|
|
notification = persist_notification(template_id=template.id,
|
|
template_version=template.version,
|
|
recipient=form['phone_number'],
|
|
service=service,
|
|
personalisation=form.get('personalisation', None),
|
|
notification_type=SMS_TYPE,
|
|
api_key_id=api_user.id,
|
|
key_type=api_user.key_type,
|
|
reference=form.get('reference'))
|
|
send_notification_to_queue(notification, service.research_mode)
|
|
sms_sender = service.sms_sender if service.sms_sender else current_app.config.get('FROM_NUMBER')
|
|
resp = create_post_sms_response_from_notification(notification,
|
|
str(template_with_content),
|
|
sms_sender,
|
|
request.url_root)
|
|
return jsonify(resp), 201
|
|
|
|
|
|
@notification_blueprint.route('/email', methods=['POST'])
|
|
def post_email_notification():
|
|
form = validate(request.get_json(), post_email_request)
|
|
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
|
|
|
check_service_message_limit(api_user.key_type, service)
|
|
service_can_send_to_recipient(form['email_address'], api_user.key_type, service)
|
|
|
|
template, template_with_content = __validate_template(form, service, EMAIL_TYPE)
|
|
notification = persist_notification(template_id=template.id,
|
|
template_version=template.version,
|
|
recipient=form['email_address'],
|
|
service=service,
|
|
personalisation=form.get('personalisation', None),
|
|
notification_type=EMAIL_TYPE,
|
|
api_key_id=api_user.id,
|
|
key_type=api_user.key_type,
|
|
reference=form.get('reference'))
|
|
|
|
send_notification_to_queue(notification, service.research_mode)
|
|
|
|
resp = create_post_email_response_from_notification(notification=notification,
|
|
content=str(template_with_content),
|
|
subject=template_with_content.subject,
|
|
email_from=service.email_from,
|
|
url_root=request.url_root)
|
|
return jsonify(resp), 201
|
|
|
|
|
|
def __validate_template(form, service, notification_type):
|
|
try:
|
|
template = templates_dao.dao_get_template_by_id_and_service_id(template_id=form['template_id'],
|
|
service_id=service.id)
|
|
except NoResultFound:
|
|
message = 'Template not found'
|
|
raise BadRequestError(message=message,
|
|
fields=[{'template': message}])
|
|
|
|
check_template_is_for_notification_type(notification_type, template.template_type)
|
|
check_template_is_active(template)
|
|
template_with_content = create_content_for_notification(template, form.get('personalisation', {}))
|
|
if template.template_type == SMS_TYPE:
|
|
check_sms_content_char_count(template_with_content.content_count)
|
|
return template, template_with_content
|