def test_persist_scheduled_notification(sample_notification): persist_scheduled_notification(sample_notification.id, "2017-05-12 14:15") scheduled_notification = ScheduledNotification.query.all() assert len(scheduled_notification) == 1 assert scheduled_notification[0].notification_id == sample_notification.id assert scheduled_notification[0].scheduled_for == datetime.datetime( 2017, 5, 12, 18, 15)
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service, reply_to_text=None): 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 simulated = simulated_recipient(send_to, notification_type) personalisation = process_document_uploads(form.get('personalisation'), service, simulated=simulated) additional_email_parameters = {"importance": form.get('importance', None), "cc_address": form.get('cc_address', None)} \ if notification_type == EMAIL_TYPE else {} notification = persist_notification( template_id=template.id, template_version=template.version, recipient=form_send_to, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key.id, key_type=api_key.key_type, client_reference=form.get('reference', None), simulated=simulated, reply_to_text=reply_to_text, additional_email_parameters=additional_email_parameters) scheduled_for = form.get("scheduled_for", None) if scheduled_for: persist_scheduled_notification(notification.id, form["scheduled_for"]) else: if not simulated: queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None send_notification_to_queue(notification=notification, research_mode=service.research_mode, queue=queue_name) else: current_app.logger.debug( "POST simulated notification for id: {}".format( notification.id)) return notification
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service, reply_to_text=None): 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 simulated = simulated_recipient(send_to, notification_type) notification = persist_notification( template_id=template.id, template_version=template.version, recipient=form_send_to, service=service, personalisation=form.get('personalisation', None), notification_type=notification_type, api_key_id=api_key.id, key_type=api_key.key_type, client_reference=form.get('reference', None), simulated=simulated, reply_to_text=reply_to_text, status_callback_url=form.get('status_callback_url', None), status_callback_bearer_token=form.get('status_callback_bearer_token', None), ) scheduled_for = form.get("scheduled_for", None) if scheduled_for: persist_scheduled_notification(notification.id, form["scheduled_for"]) else: if not simulated: queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None send_notification_to_queue(notification=notification, research_mode=service.research_mode, queue=queue_name) else: current_app.logger.debug( "POST simulated notification for id: {}".format( notification.id)) return notification
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service, reply_to_text=None): 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 simulated = simulated_recipient(send_to, notification_type) personalisation = process_document_uploads(form.get("personalisation"), service, simulated, template.id) notification = { "id": create_uuid(), "template": str(template.id), "template_version": str(template.version), "to": form_send_to, "personalisation": personalisation, "simulated": simulated, "api_key": str(api_key.id), "key_type": str(api_key.key_type), "client_reference": form.get("reference", None), } encrypted_notification_data = encryption.encrypt(notification) scheduled_for = form.get("scheduled_for", None) if scheduled_for: notification = persist_notification( template_id=template.id, template_version=template.version, recipient=form_send_to, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key.id, key_type=api_key.key_type, client_reference=form.get("reference", None), simulated=simulated, reply_to_text=reply_to_text, ) persist_scheduled_notification(notification.id, form["scheduled_for"]) elif current_app.config["FF_NOTIFICATION_CELERY_PERSISTENCE"] and not simulated: # depending on the type route to the appropriate save task if notification_type == EMAIL_TYPE: current_app.logger.info("calling save email task") save_email.apply_async( (authenticated_service.id, create_uuid(), encrypted_notification_data), queue=QueueNames.DATABASE if not authenticated_service.research_mode else QueueNames.RESEARCH_MODE, ) elif notification_type == SMS_TYPE: save_sms.apply_async( (authenticated_service.id, create_uuid(), encrypted_notification_data), queue=QueueNames.DATABASE if not authenticated_service.research_mode else QueueNames.RESEARCH_MODE, ) else: notification = persist_notification( template_id=template.id, template_version=template.version, recipient=form_send_to, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key.id, key_type=api_key.key_type, client_reference=form.get("reference", None), simulated=simulated, reply_to_text=reply_to_text, ) if not simulated: send_notification_to_queue( notification=notification, research_mode=service.research_mode, queue=template.queue_to_use(), ) else: current_app.logger.debug("POST simulated notification for id: {}".format(notification.id)) if not isinstance(notification, Notification): notification["template_id"] = notification["template"] notification["api_key_id"] = notification["api_key"] notification["template_version"] = template.version notification["service"] = service notification["service_id"] = service.id notification["reply_to_text"] = reply_to_text del notification["template"] del notification["api_key"] del notification["simulated"] notification = Notification(**notification) return notification
def process_sms_or_email_notification(*, form, notification_type, api_key, template, service, reply_to_text=None): notification_id = None 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 simulated = simulated_recipient(send_to, notification_type) personalisation, document_download_count = process_document_uploads( form.get('personalisation'), service, simulated=simulated) if str(service.id) in current_app.config.get('HIGH_VOLUME_SERVICE') and api_key.key_type == KEY_TYPE_NORMAL \ and notification_type == EMAIL_TYPE: # Put GOV.UK Email notifications onto a queue # To take the pressure off the db for API requests put the notification for our high volume service onto a queue # the task will then save the notification, then call send_notification_to_queue. # We know that this team does not use the GET request, but relies on callbacks to get the status updates. try: notification_id = uuid.uuid4() notification = save_email_to_queue( form=form, notification_id=str(notification_id), notification_type=notification_type, api_key=api_key, template=template, service_id=service.id, personalisation=personalisation, document_download_count=document_download_count, reply_to_text=reply_to_text) return notification except SQSError: # if SQS cannot put the task on the queue, it's probably because the notification body was too long and it # went over SQS's 256kb message limit. If so, we current_app.logger.info( f'Notification {notification_id} failed to save to high volume queue. Using normal flow instead' ) notification = persist_notification( notification_id=notification_id, template_id=template.id, template_version=template.version, recipient=form_send_to, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key.id, key_type=api_key.key_type, client_reference=form.get('reference', None), simulated=simulated, reply_to_text=reply_to_text, document_download_count=document_download_count) scheduled_for = form.get("scheduled_for", None) if scheduled_for: persist_scheduled_notification(notification.id, form["scheduled_for"]) else: if not simulated: queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None send_notification_to_queue(notification=notification, research_mode=service.research_mode, queue=queue_name) else: current_app.logger.debug( "POST simulated notification for id: {}".format( notification.id)) return notification