Save the first non-empty line as recipient

Since we now allow the address to be populated from any three lines, we
can’t guarantee that the recipient will be in the `addressline1` field.
This commit is contained in:
Chris Hill-Scott
2020-04-04 17:31:24 +01:00
parent 7032bac178
commit 36e61272c5
2 changed files with 26 additions and 12 deletions

View File

@@ -3,6 +3,8 @@ from datetime import datetime
from collections import namedtuple, defaultdict
from flask import current_app
from notifications_utils.columns import Columns
from notifications_utils.postal_address import PostalAddress
from notifications_utils.recipients import RecipientCSV
from notifications_utils.statsd_decorators import statsd
from notifications_utils.timezones import convert_utc_to_bst
@@ -341,7 +343,9 @@ def save_letter(
notification = encryption.decrypt(encrypted_notification)
# we store the recipient as just the first item of the person's address
recipient = notification['personalisation']['addressline1']
recipient = PostalAddress.from_personalisation(
Columns(notification['personalisation'])
).normalised_lines[0]
service = dao_fetch_service_by_id(service_id)
template = dao_get_template_by_id(notification['template'], version=notification['template_version'])

View File

@@ -950,7 +950,26 @@ def test_save_sms_does_not_send_duplicate_and_does_not_put_in_retry_queue(sample
assert not retry.called
def test_save_letter_saves_letter_to_database(mocker, notify_db_session):
@pytest.mark.parametrize('personalisation', (
{
'addressline1': 'Foo',
'addressline2': 'Bar',
'addressline3': 'Baz',
'addressline4': 'Wibble',
'addressline5': 'Wobble',
'addressline6': 'Wubble',
'postcode': 'SE1 2SA',
},
{
# The address isnt normalised when we store it in the
# `personalisation` column, but the first line is normalised for
# Storing in the `to` column
'addressline2': ' Foo ',
'addressline4': 'Bar',
'addressline6': 'se12sa',
},
))
def test_save_letter_saves_letter_to_database(mocker, notify_db_session, personalisation):
service = create_service()
contact_block = create_letter_contact(service=service, contact_block="Address contact", is_default=True)
template = create_template(service=service, template_type=LETTER_TYPE, reply_to=contact_block.id)
@@ -959,18 +978,9 @@ def test_save_letter_saves_letter_to_database(mocker, notify_db_session):
mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life")
mocker.patch('app.celery.tasks.letters_pdf_tasks.create_letters_pdf.apply_async')
personalisation = {
'addressline1': 'Foo',
'addressline2': 'Bar',
'addressline3': 'Baz',
'addressline4': 'Wibble',
'addressline5': 'Wobble',
'addressline6': 'Wubble',
'postcode': 'SE1 2SA',
}
notification_json = _notification_json(
template=job.template,
to='Foo',
to='This is ignored for letters',
personalisation=personalisation,
job_id=job.id,
row_number=1