mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
- Refactor v2 post_notification to use a single method for sms and email.
- Added the `simulate` notification logic to version 2. We have 3 email addresses and phone numbers that are used to simulate a successful post to /notifications. This was missed out of the version 2 endpoint. - Added a test to template_dao to check for the default value of normal for new templates - in v2 get_notifications, casted the path param to a uuid, if not uuid abort(404)
This commit is contained in:
@@ -6,12 +6,13 @@ 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)
|
||||
send_notification_to_queue,
|
||||
simulated_recipient)
|
||||
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)
|
||||
check_sms_content_char_count,
|
||||
validate_and_format_recipient)
|
||||
from app.schema_validation import validate
|
||||
from app.v2.errors import BadRequestError
|
||||
from app.v2.notifications import notification_blueprint
|
||||
@@ -20,62 +21,52 @@ from app.v2.notifications.notification_schemas import (post_sms_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)
|
||||
@notification_blueprint.route('/<notification_type>', methods=['POST'])
|
||||
def post_notification(notification_type):
|
||||
if notification_type == EMAIL_TYPE:
|
||||
form = validate(request.get_json(), post_email_request)
|
||||
else:
|
||||
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)
|
||||
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=service,
|
||||
notification_type=notification_type)
|
||||
|
||||
template, template_with_content = __validate_template(form, service, SMS_TYPE)
|
||||
template, template_with_content = __validate_template(form, service, notification_type)
|
||||
|
||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||
simulated = simulated_recipient(send_to, notification_type)
|
||||
notification = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=form['phone_number'],
|
||||
recipient=send_to,
|
||||
service=service,
|
||||
personalisation=form.get('personalisation', None),
|
||||
notification_type=SMS_TYPE,
|
||||
notification_type=notification_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=notification,
|
||||
body=str(template_with_content),
|
||||
from_number=sms_sender,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
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,
|
||||
service_id=service.id)
|
||||
reference=form.get('reference', None),
|
||||
simulated=simulated)
|
||||
if not simulated:
|
||||
send_notification_to_queue(notification, service.research_mode)
|
||||
else:
|
||||
current_app.logger.info("POST simulated notification for id: {}".format(notification.id))
|
||||
if notification_type == SMS_TYPE:
|
||||
sms_sender = service.sms_sender if service.sms_sender else current_app.config.get('FROM_NUMBER')
|
||||
resp = create_post_sms_response_from_notification(notification=notification,
|
||||
body=str(template_with_content),
|
||||
from_number=sms_sender,
|
||||
url_root=request.url_root,
|
||||
service_id=service.id)
|
||||
else:
|
||||
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,
|
||||
service_id=service.id)
|
||||
return jsonify(resp), 201
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user