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 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
Example #3
0
def process_letter_notification(*,
                                letter_data,
                                api_key,
                                service,
                                template,
                                template_with_content,
                                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 service.research_mode and 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,
            service=service,
            template=template,
            reply_to_text=reply_to_text)

    postage = validate_address(service, letter_data['personalisation'])

    test_key = api_key.key_type == KEY_TYPE_TEST

    status = NOTIFICATION_CREATED
    updated_at = None
    if test_key:
        # if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up
        if current_app.config['NOTIFY_ENVIRONMENT'] in [
                'preview', 'development'
        ]:
            status = NOTIFICATION_SENDING
        # mark test letter as delivered and do not create a fake response later
        else:
            status = NOTIFICATION_DELIVERED
            updated_at = datetime.utcnow()

    queue = QueueNames.CREATE_LETTERS_PDF if not test_key else QueueNames.RESEARCH_MODE

    notification = create_letter_notification(letter_data=letter_data,
                                              service=service,
                                              template=template,
                                              api_key=api_key,
                                              status=status,
                                              reply_to_text=reply_to_text,
                                              updated_at=updated_at,
                                              postage=postage)

    get_pdf_for_templated_letter.apply_async([str(notification.id)],
                                             queue=queue)

    if test_key and current_app.config['NOTIFY_ENVIRONMENT'] in [
            'preview', 'development'
    ]:
        create_fake_letter_response_file.apply_async(
            (notification.reference, ), queue=queue)
    resp = create_response_for_post_notification(
        notification_id=notification.id,
        client_reference=notification.client_reference,
        template_id=notification.template_id,
        template_version=notification.template_version,
        notification_type=notification.notification_type,
        reply_to=reply_to_text,
        service_id=notification.service_id,
        template_with_content=template_with_content)
    return resp