def replay_created_notifications(): # if the notification has not be send after 1 hour, then try to resend. resend_created_notifications_older_than = (60 * 60) for notification_type in (EMAIL_TYPE, SMS_TYPE): notifications_to_resend = notifications_not_yet_sent( resend_created_notifications_older_than, notification_type) if len(notifications_to_resend) > 0: current_app.logger.info( "Sending {} {} notifications " "to the delivery queue because the notification " "status was created.".format(len(notifications_to_resend), notification_type)) for n in notifications_to_resend: send_notification_to_queue(notification=n, research_mode=n.service.research_mode) # if the letter has not be send after an hour, then create a zendesk ticket letters = letters_missing_from_sending_bucket( resend_created_notifications_older_than) if len(letters) > 0: msg = "{} letters were created over an hour ago, " \ "but do not have an updated_at timestamp or billable units. " \ "\n Creating app.celery.letters_pdf_tasks.create_letters tasks to upload letter to S3 " \ "and update notifications for the following notification ids: " \ "\n {}".format(len(letters), [x.id for x in letters]) current_app.logger.info(msg) for letter in letters: create_letters_pdf.apply_async([str(letter.id)], queue=QueueNames.CREATE_LETTERS_PDF)
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 replay_create_pdf_letters(notification_id): print("Create task to create_letters_pdf for notification: {}".format( notification_id)) create_letters_pdf.apply_async([str(notification_id)], queue=QueueNames.CREATE_LETTERS_PDF)
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