Skip to content

Commit

Permalink
#1163 Remove routes and route tests for sending a PDF letter. (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalbfled authored Sep 28, 2023
1 parent 39634f5 commit d34bc7d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 214 deletions.
9 changes: 1 addition & 8 deletions app/service/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
from app.schema_validation import validate
from app.service import statistics
from app.service.send_notification import send_one_off_notification, send_pdf_letter_notification
from app.service.send_notification import send_one_off_notification
from app.service.sender import send_notification_to_service_users
from app.service.service_data_retention_schema import (
add_service_data_retention_request,
Expand Down Expand Up @@ -622,13 +622,6 @@ def create_one_off_notification(service_id):
return jsonify(resp), 201


@service_blueprint.route('/<uuid:service_id>/send-pdf-letter', methods=['POST'])
@requires_admin_auth()
def create_pdf_letter(service_id):
resp = send_pdf_letter_notification(service_id, request.get_json())
return jsonify(resp), 201


@service_blueprint.route('/<uuid:service_id>/email-reply-to', methods=["GET"])
@requires_admin_auth()
def get_email_reply_to_addresses(service_id):
Expand Down
89 changes: 8 additions & 81 deletions app/service/send_notification.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
from flask import current_app
from notifications_utils.s3 import S3ObjectNotFound, s3download as utils_s3download
from sqlalchemy.orm.exc import NoResultFound

from app import create_random_identifier
from app.config import QueueNames
from app.dao.notifications_dao import _update_notification_status
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
from app.dao.service_sms_sender_dao import dao_get_service_sms_sender_by_id
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
from app.dao.users_dao import get_user_by_id
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue
)
from app.notifications.validators import (
check_service_over_daily_message_limit,
validate_and_format_recipient,
validate_template
)
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue
)
from app.models import (
KEY_TYPE_NORMAL,
PRIORITY,
SMS_TYPE,
EMAIL_TYPE,
LETTER_TYPE,
NOTIFICATION_DELIVERED,
UPLOAD_LETTERS,
)
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id_and_service_id, get_precompiled_letter_template
from app.dao.users_dao import get_user_by_id
from app.letters.utils import (
get_letter_pdf_filename,
get_page_count,
move_uploaded_pdf_to_letters_bucket,
)
from app.v2.errors import BadRequestError
from app.utils import get_public_notify_type_text
from sqlalchemy.orm.exc import NoResultFound


def validate_created_by(service, created_by_id):
Expand Down Expand Up @@ -130,66 +120,3 @@ def get_reply_to_text(notification_type, sender_id, service, template):
else:
reply_to = template.get_reply_to_text()
return reply_to


def send_pdf_letter_notification(service_id, post_data):
service = dao_fetch_service_by_id(service_id)

if not service.has_permissions(LETTER_TYPE):
raise BadRequestError(message="Service is not allowed to send {}".format(
get_public_notify_type_text(LETTER_TYPE, plural=True)))

if not service.has_permissions(UPLOAD_LETTERS):
raise BadRequestError(message="Service is not allowed to send {}".format(
get_public_notify_type_text(UPLOAD_LETTERS, plural=True)))

check_service_over_daily_message_limit(KEY_TYPE_NORMAL, service)
validate_created_by(service, post_data['created_by'])

template = get_precompiled_letter_template(service.id)
file_location = 'service-{}/{}.pdf'.format(service.id, post_data['file_id'])

try:
letter = utils_s3download(current_app.config['TRANSIENT_UPLOADED_LETTERS'], file_location)
except S3ObjectNotFound as e:
current_app.logger.exception('Letter {}.pdf not in transient {} bucket'.format(
post_data['file_id'], current_app.config['TRANSIENT_UPLOADED_LETTERS'])
)
raise e

# Getting the page count won't raise an error since admin has already checked the PDF is valid
billable_units = get_page_count(letter.read())

personalisation = {
'address_line_1': post_data['filename']
}

# TODO: stop hard-coding postage as 'second' once we get postage from the admin
notification = persist_notification(
notification_id=post_data['file_id'],
template_id=template.id,
template_version=template.version,
template_postage=template.postage,
recipient=post_data['filename'],
service=service,
personalisation=personalisation,
notification_type=LETTER_TYPE,
api_key_id=None,
key_type=KEY_TYPE_NORMAL,
reference=create_one_off_reference(LETTER_TYPE),
client_reference=post_data['filename'],
created_by_id=post_data['created_by'],
billable_units=billable_units,
postage='second',
)

upload_filename = get_letter_pdf_filename(
notification.reference,
notification.service.crown,
is_scan_letter=False,
postage=notification.postage
)

move_uploaded_pdf_to_letters_bucket(file_location, upload_filename)

return {'id': str(notification.id)}
23 changes: 0 additions & 23 deletions tests/app/service/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2411,29 +2411,6 @@ def test_send_one_off_notification(sample_service, admin_request, mocker):
assert response['id'] == str(noti.id)


def test_create_pdf_letter(mocker, sample_service_full_permissions, client, fake_uuid, notify_user):
mocker.patch('app.service.send_notification.utils_s3download')
mocker.patch('app.service.send_notification.get_page_count', return_value=1)
mocker.patch('app.service.send_notification.move_uploaded_pdf_to_letters_bucket')

user = sample_service_full_permissions.users[0]
data = json.dumps({
'filename': 'valid.pdf',
'created_by': str(user.id),
'file_id': fake_uuid
})

response = client.post(
url_for('service.create_pdf_letter', service_id=sample_service_full_permissions.id),
data=data,
headers=[('Content-Type', 'application/json'), create_authorization_header()]
)
json_resp = json.loads(response.get_data(as_text=True))

assert response.status_code == 201
assert json_resp == {'id': fake_uuid}


def test_get_notification_for_service_includes_template_redacted(admin_request, sample_notification):
resp = admin_request.get(
'service.get_notification_for_service',
Expand Down
102 changes: 0 additions & 102 deletions tests/app/service/test_send_pdf_letter_notification.py

This file was deleted.

0 comments on commit d34bc7d

Please sign in to comment.