Use personalisation to set client_reference for letters

which were sent through Notify interface only. This is done
to avoid performance dip from additional operation for
other notification types.
This commit is contained in:
Pea Tyczynska
2021-03-19 16:42:55 +00:00
parent a2da8bc070
commit 52c529ab3a
9 changed files with 79 additions and 49 deletions

View File

@@ -58,7 +58,7 @@ from app.models import (
from app.notifications.process_notifications import persist_notification
from app.serialised_models import SerialisedService, SerialisedTemplate
from app.service.utils import service_allowed_to_send_to
from app.utils import DATETIME_FORMAT
from app.utils import DATETIME_FORMAT, get_reference_from_personalisation
@notify_celery.task(name="process-job")
@@ -383,6 +383,7 @@ def save_letter(
job_row_number=notification['row_number'],
notification_id=notification_id,
reference=create_random_identifier(),
client_reference=get_reference_from_personalisation(notification['personalisation']),
reply_to_text=template.reply_to_text,
status=status
)

View File

@@ -121,7 +121,7 @@ def persist_notification(
created_at=notification_created_at,
job_id=job_id,
job_row_number=job_row_number,
client_reference=client_reference or _get_reference_from_personalisation(personalisation),
client_reference=client_reference,
reference=reference,
created_by_id=created_by_id,
status=status,
@@ -159,12 +159,6 @@ def persist_notification(
return notification
def _get_reference_from_personalisation(personalisation):
if personalisation:
return personalisation.get("reference")
return None
def send_notification_to_queue_detached(
key_type, notification_type, notification_id, research_mode, queue=None
):

View File

@@ -108,6 +108,7 @@ def send_notification(notification_type):
if notification_type == SMS_TYPE:
check_if_service_can_send_to_number(authenticated_service, notification_form['to'])
# Do not persist or send notification to the queue if it is a simulated recipient
simulated = simulated_recipient(notification_form['to'], notification_type)
notification_model = persist_notification(template_id=template.id,
@@ -120,7 +121,7 @@ def send_notification(notification_type):
api_key_id=api_user.id,
key_type=api_user.key_type,
simulated=simulated,
reply_to_text=template.reply_to_text
reply_to_text=template.reply_to_text,
)
if not simulated:
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None

View File

@@ -81,12 +81,16 @@ def send_one_off_notification(service_id, post_data):
allow_guest_list_recipients=False,
)
postage = None
client_reference = None
if template.template_type == LETTER_TYPE:
# Validate address and set postage to europe|rest-of-world if international letter,
# otherwise persist_notification with use template postage
postage = validate_address(service, personalisation)
if not postage:
postage = template.postage
from app.utils import get_reference_from_personalisation
client_reference = get_reference_from_personalisation(personalisation)
validate_created_by(service, post_data['created_by'])
sender_id = post_data.get('sender_id', None)
@@ -108,7 +112,8 @@ def send_one_off_notification(service_id, post_data):
created_by_id=post_data['created_by'],
reply_to_text=reply_to,
reference=create_one_off_reference(template.template_type),
postage=postage
postage=postage,
client_reference=client_reference
)
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None

View File

@@ -160,3 +160,9 @@ def get_uuid_string_or_none(val):
def format_sequential_number(sequential_number):
return format(sequential_number, "x").zfill(8)
def get_reference_from_personalisation(personalisation):
if personalisation:
return personalisation.get("reference")
return None