def save_letter( self, service_id, notification_id, encrypted_notification, ): notification = encryption.decrypt(encrypted_notification) postal_address = PostalAddress.from_personalisation( Columns(notification['personalisation'])) service = SerialisedService.from_id(service_id) template = SerialisedTemplate.from_id_and_service_id( notification['template'], service_id=service.id, version=notification['template_version'], ) try: # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up status = NOTIFICATION_CREATED if not service.research_mode else NOTIFICATION_SENDING saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], postage=postal_address.postage if postal_address.international else template.postage, recipient=postal_address.normalised, service=service, personalisation=notification['personalisation'], notification_type=LETTER_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_at=datetime.utcnow(), job_id=notification['job'], job_row_number=notification['row_number'], notification_id=notification_id, reference=create_random_identifier(), reply_to_text=template.reply_to_text, status=status) if not service.research_mode: letters_pdf_tasks.get_pdf_for_templated_letter.apply_async( [str(saved_notification.id)], queue=QueueNames.CREATE_LETTERS_PDF) elif current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: research_mode_tasks.create_fake_letter_response_file.apply_async( (saved_notification.reference, ), queue=QueueNames.RESEARCH_MODE) else: update_notification_status_by_reference( saved_notification.reference, 'delivered') current_app.logger.debug("Letter {} created at {}".format( saved_notification.id, saved_notification.created_at)) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def save_letter( self, service_id, notification_id, encrypted_notification, ): notification = encryption.decrypt(encrypted_notification) # we store the recipient as just the first item of the person's address recipient = notification["personalisation"]["addressline1"] service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id(notification["template"], version=notification["template_version"]) check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service) try: # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up status = NOTIFICATION_CREATED if not service.research_mode else NOTIFICATION_SENDING saved_notification = persist_notification( template_id=notification["template"], template_version=notification["template_version"], template_postage=template.postage, recipient=recipient, service=service, personalisation=notification["personalisation"], notification_type=LETTER_TYPE, api_key_id=notification.get("api_key", None), key_type=KEY_TYPE_NORMAL, created_at=datetime.utcnow(), job_id=notification["job"], job_row_number=notification["row_number"], notification_id=notification_id, reference=create_random_identifier(), reply_to_text=template.get_reply_to_text(), status=status, ) if not service.research_mode: send_notification_to_queue(saved_notification, service.research_mode) elif current_app.config["NOTIFY_ENVIRONMENT"] in [ "preview", "development" ]: research_mode_tasks.create_fake_letter_response_file.apply_async( (saved_notification.reference, ), queue=QueueNames.RESEARCH_MODE) else: update_notification_status_by_reference( saved_notification.reference, "delivered") current_app.logger.debug("Letter {} created at {}".format( saved_notification.id, saved_notification.created_at)) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def save_letter( self, service_id, notification_id, encrypted_notification, ): notification = encryption.decrypt(encrypted_notification) # we store the recipient as just the first item of the person's address recipient = notification['personalisation']['addressline1'] service = dao_fetch_service_by_id(service_id) template = dao_get_template_by_id(notification['template'], version=notification['template_version']) try: # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up status = NOTIFICATION_CREATED if not service.research_mode else NOTIFICATION_SENDING saved_notification = persist_notification( template_id=notification['template'], template_version=notification['template_version'], recipient=recipient, service=service, personalisation=notification['personalisation'], notification_type=LETTER_TYPE, api_key_id=None, key_type=KEY_TYPE_NORMAL, created_at=datetime.utcnow(), job_id=notification['job'], job_row_number=notification['row_number'], notification_id=notification_id, reference=create_random_identifier(), reply_to_text=template.get_reply_to_text(), status=status) if not service.research_mode: letters_pdf_tasks.create_letters_pdf.apply_async( [str(saved_notification.id)], queue=QueueNames.CREATE_LETTERS_PDF) elif current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: research_mode_tasks.create_fake_letter_response_file.apply_async( (saved_notification.reference, ), queue=QueueNames.RESEARCH_MODE) else: update_notification_status_by_reference( saved_notification.reference, 'delivered') current_app.logger.debug("Letter {} created at {}".format( saved_notification.id, saved_notification.created_at)) except SQLAlchemyError as e: handle_exception(self, notification, notification_id, e)
def test_should_not_update_status_one_notification_status_is_delivered(notify_db, notify_db_session, sample_email_template, ses_provider): notification = sample_notification(notify_db=notify_db, notify_db_session=notify_db_session, template=sample_email_template, status='sending') assert Notification.query.get(notification.id).status == "sending" notification.reference = 'reference' dao_update_notification(notification) update_notification_status_by_reference('reference', 'delivered') assert Notification.query.get(notification.id).status == 'delivered' update_notification_status_by_reference('reference', 'failed') assert Notification.query.get(notification.id).status == 'delivered'
def test_should_not_update_status_by_reference_if_not_sending_and_does_not_update_job(notify_db, notify_db_session): job = sample_job(notify_db, notify_db_session) notification = sample_notification(notify_db, notify_db_session, status='delivered', reference='reference', job=job) assert Notification.query.get(notification.id).status == 'delivered' assert not update_notification_status_by_reference('reference', 'failed') assert Notification.query.get(notification.id).status == 'delivered' assert job == Job.query.get(notification.job_id)
def process_letter_notification(*, letter_data, api_key, template, reply_to_text, precompiled=False): if api_key.key_type == KEY_TYPE_TEAM: raise BadRequestError( message='Cannot send letters with a team api key', status_code=403) if not api_key.service.research_mode and api_key.service.restricted and api_key.key_type != KEY_TYPE_TEST: raise BadRequestError( message='Cannot send letters when service is in trial mode', status_code=403) if precompiled: return process_precompiled_letter_notifications( letter_data=letter_data, api_key=api_key, template=template, reply_to_text=reply_to_text) test_key = api_key.key_type == KEY_TYPE_TEST # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up status = NOTIFICATION_CREATED if not test_key else NOTIFICATION_SENDING queue = QueueNames.CREATE_LETTERS_PDF if not test_key else QueueNames.RESEARCH_MODE notification = create_letter_notification(letter_data=letter_data, template=template, api_key=api_key, status=status, reply_to_text=reply_to_text) create_letters_pdf.apply_async([str(notification.id)], queue=queue) if test_key: if current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: create_fake_letter_response_file.apply_async( (notification.reference, ), queue=queue) else: update_notification_status_by_reference(notification.reference, NOTIFICATION_DELIVERED) return notification
def test_should_by_able_to_update_status_by_reference(sample_email_template, ses_provider): data = _notification_json(sample_email_template, status='sending') notification = Notification(**data) dao_create_notification(notification) assert Notification.query.get(notification.id).status == "sending" notification.reference = 'reference' dao_update_notification(notification) updated = update_notification_status_by_reference('reference', 'delivered') assert updated.status == 'delivered' assert Notification.query.get(notification.id).status == 'delivered'
def _process_for_status(notification_status, client_name, provider_reference, detailed_status_code=None): # record stats if client_name == 'Twilio': notification = notifications_dao.update_notification_status_by_reference( reference=provider_reference, status=notification_status) else: notification = notifications_dao.update_notification_status_by_id( notification_id=provider_reference, status=notification_status, sent_by=client_name.lower(), detailed_status_code=detailed_status_code) if not notification: return statsd_client.incr('callback.{}.{}'.format(client_name.lower(), notification_status)) if notification.sent_at: statsd_client.timing_with_dates( 'callback.{}.elapsed-time'.format(client_name.lower()), datetime.utcnow(), notification.sent_at) if notification.billable_units == 0: service = notification.service template_model = dao_get_template_by_id(notification.template_id, notification.template_version) template = SMSMessageTemplate( template_model.__dict__, values=notification.personalisation, prefix=service.name, show_prefix=service.prefix_sms, ) notification.billable_units = template.fragment_count notifications_dao.dao_update_notification(notification) if notification_status != NOTIFICATION_PENDING: service_callback_api = get_service_delivery_status_callback_api_for_service( service_id=notification.service_id) # queue callback task only if the service_callback_api exists if service_callback_api: encrypted_notification = create_delivery_status_callback_data( notification, service_callback_api) send_delivery_status_to_service.apply_async( [str(notification.id), encrypted_notification], queue=QueueNames.CALLBACKS)
def test_should_return_zero_count_if_no_notification_with_reference(): assert not update_notification_status_by_reference('something', 'delivered')
def test_should_not_update_status_by_reference_if_not_sending(notify_db, notify_db_session): notification = sample_notification(notify_db, notify_db_session, status='created', reference='reference') assert Notification.query.get(notification.id).status == 'created' updated = update_notification_status_by_reference('reference', 'failed') assert Notification.query.get(notification.id).status == 'created' assert not updated
def process_letter_notification(*, letter_data, api_key, template, reply_to_text, precompiled=False): if api_key.key_type == KEY_TYPE_TEAM: raise BadRequestError( message='Cannot send letters with a team api key', status_code=403) if not api_key.service.research_mode and api_key.service.restricted and api_key.key_type != KEY_TYPE_TEST: raise BadRequestError( message='Cannot send letters when service is in trial mode', status_code=403) if precompiled: return process_precompiled_letter_notifications( letter_data=letter_data, api_key=api_key, template=template, reply_to_text=reply_to_text) address = PostalAddress.from_personalisation( letter_data['personalisation'], allow_international_letters=api_key.service.has_permission( INTERNATIONAL_LETTERS), ) if not address.has_enough_lines: raise ValidationError( message=f'Address must be at least {PostalAddress.MIN_LINES} lines' ) if address.has_too_many_lines: raise ValidationError( message= f'Address must be no more than {PostalAddress.MAX_LINES} lines') if not address.has_valid_last_line: if address.allow_international_letters: raise ValidationError( message= f'Last line of address must be a real UK postcode or another country' ) raise ValidationError(message='Must be a real UK postcode') test_key = api_key.key_type == KEY_TYPE_TEST # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up status = NOTIFICATION_CREATED if not test_key else NOTIFICATION_SENDING queue = QueueNames.CREATE_LETTERS_PDF if not test_key else QueueNames.RESEARCH_MODE notification = create_letter_notification(letter_data=letter_data, template=template, api_key=api_key, status=status, reply_to_text=reply_to_text) create_letters_pdf.apply_async([str(notification.id)], queue=queue) if test_key: if current_app.config['NOTIFY_ENVIRONMENT'] in [ 'preview', 'development' ]: create_fake_letter_response_file.apply_async( (notification.reference, ), queue=queue) else: update_notification_status_by_reference(notification.reference, NOTIFICATION_DELIVERED) return notification
def process_ses_response(ses_request): client_name = 'SES' try: errors = validate_callback_data(data=ses_request, fields=['Message'], client_name=client_name) if errors: return errors ses_message = json.loads(ses_request['Message']) errors = validate_callback_data(data=ses_message, fields=['notificationType'], client_name=client_name) if errors: return errors notification_type = ses_message['notificationType'] if notification_type == 'Bounce': notification_type = determine_notification_bounce_type( notification_type, ses_message) elif notification_type == 'Complaint': complaint, notification, recipient = handle_complaint(ses_message) _check_and_queue_complaint_callback_task(complaint, notification, recipient) return try: aws_response_dict = get_aws_responses(notification_type) except KeyError: error = "{} callback failed: status {} not found".format( client_name, notification_type) return error notification_status = aws_response_dict['notification_status'] try: reference = ses_message['mail']['messageId'] notification = notifications_dao.update_notification_status_by_reference( reference, notification_status) if not notification: warning = "SES callback failed: notification either not found or already updated " \ "from sending. Status {} for notification reference {}".format(notification_status, reference) current_app.logger.warning(warning) return if not aws_response_dict['success']: current_app.logger.info( "SES delivery failed: notification id {} and reference {} has error found. Status {}" .format(notification.id, reference, aws_response_dict['message'])) else: current_app.logger.info( '{} callback return status of {} for notification: {}'. format(client_name, notification_status, notification.id)) statsd_client.incr('callback.ses.{}'.format(notification_status)) if notification.sent_at: statsd_client.timing_with_dates( 'callback.ses.elapsed-time'.format(client_name.lower()), datetime.utcnow(), notification.sent_at) _check_and_queue_callback_task(notification) return except KeyError: error = "SES callback failed: messageId missing" return error except ValueError: error = "{} callback failed: invalid json".format(client_name) return error