Add a view function for pre-compiled PDF letters

Adds a separate view function that is registered under the same
route as existing letter POST notification.
This commit is contained in:
Alexey Bezhan
2018-02-23 10:38:36 +00:00
parent e659253b01
commit 5327298371
3 changed files with 113 additions and 2 deletions

View File

@@ -206,7 +206,6 @@ post_email_response = {
"required": ["id", "content", "uri", "template"]
}
post_letter_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST letter notification schema",
@@ -221,6 +220,19 @@ post_letter_request = {
"additionalProperties": False
}
post_precompiled_letter_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST precompiled letter notification schema",
"type": "object",
"title": "POST v2/notifications/letter",
"properties": {
"reference": {"type": "string"},
"content": {"type": "string"}
},
"required": ["reference", "content"],
"additionalProperties": False
}
letter_content = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Letter content for POST letter notification",

View File

@@ -7,7 +7,10 @@ from notifications_utils.recipients import try_validate_and_format_phone_number
from app import api_user, authenticated_service
from app.config import QueueNames
from app.dao.notifications_dao import update_notification_status_by_reference
from app.dao.templates_dao import dao_create_template
from app.dao.users_dao import get_user_by_id
from app.models import (
Template,
SMS_TYPE,
EMAIL_TYPE,
LETTER_TYPE,
@@ -44,7 +47,8 @@ from app.v2.notifications import v2_notification_blueprint
from app.v2.notifications.notification_schemas import (
post_sms_request,
post_email_request,
post_letter_request
post_letter_request,
post_precompiled_letter_request
)
from app.v2.notifications.create_response import (
create_post_sms_response_from_notification,
@@ -53,6 +57,47 @@ from app.v2.notifications.create_response import (
)
@v2_notification_blueprint.route('/{}'.format(LETTER_TYPE), methods=['POST'])
def post_precompiled_letter_notification():
if 'content' not in (request.get_json() or {}):
return post_notification(LETTER_TYPE)
form = validate(request.get_json(), post_precompiled_letter_request)
#check_service_has_permission(notification_type, authenticated_service.permissions)
check_rate_limiting(authenticated_service, api_user)
template = get_precompiled_letter_template(authenticated_service.id)
form['personalisation'] = {
'address_line_1': form['reference']
}
reply_to = get_reply_to_text(LETTER_TYPE, form, template)
notification = process_letter_notification(
letter_data=form,
api_key=api_user,
template=template,
reply_to_text=reply_to
)
create_resp_partial = functools.partial(
create_post_letter_response_from_notification,
subject=template.subject,
)
resp = create_resp_partial(
notification=notification,
content=None,
url_root=request.url_root,
scheduled_for=None,
)
return jsonify(resp), 201
@v2_notification_blueprint.route('/<notification_type>', methods=['POST'])
def post_notification(notification_type):
if notification_type == EMAIL_TYPE:
@@ -222,3 +267,27 @@ def get_reply_to_text(notification_type, form, template):
reply_to = template.get_reply_to_text()
return reply_to
def get_precompiled_letter_template(service_id):
template = Template.query.filter_by(
service_id=service_id,
template_type=LETTER_TYPE,
hidden=True
).first()
if template is not None:
return template
template = Template(
name='Pre-compiled PDF letter',
created_by=get_user_by_id(api_user.created_by_id),
service_id=service_id,
template_type=LETTER_TYPE,
hidden=True,
subject='Pre-compiled PDF',
content='',
)
dao_create_template(template)
return template

View File

@@ -1,4 +1,5 @@
import uuid
from unittest.mock import ANY
import pytest
from freezegun import freeze_time
@@ -694,3 +695,32 @@ def test_post_email_notification_with_invalid_reply_to_id_returns_400(client, sa
assert 'email_reply_to_id {} does not exist in database for service id {}'. \
format(fake_uuid, sample_email_template.service_id) in resp_json['errors'][0]['message']
assert 'BadRequestError' in resp_json['errors'][0]['error']
def test_post_precompiled_letter_notification_returns_201(client, sample_service, mocker):
mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.apply_async')
data = {
"reference": "letter-reference",
"content": "abcdefgh"
}
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.post(
path="v2/notifications/letter",
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 201, response.get_data(as_text=True)
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {
'content': {'body': None, 'subject': 'Pre-compiled PDF'},
'id': ANY,
'reference': 'letter-reference',
'scheduled_for': None,
'template': {
'id': ANY,
'uri': ANY,
'version': 1
},
'uri': ANY
}