persist_letter saves address correctly to database

the `to` field stores either the phone number or the email address
of the recipient - it's a bit more complicated for letters, since
there are address lines 1 through 6, and a postcode. In utils, they're
stored alongside the personalisation, and we have to ensure that when
we persist to the database we keep as much parity with utils to make
our work easier. Aside from sending, the `to` field is also used to
show recipients on the front end report pages - we've decided that the
best thing to store here is address_line_1 - which is probably going to
be either a person's name, company name, or PO box number

Also, a lot of tests and test cleanup - I added create_template and
create_notification functions in db.py, so if you're creating new
fixtures you can use these functions, and you won't need to pass
notify_db and notify_db_session around, huzzah!

also removed create param from sample_notification since it's not used
anywhere
This commit is contained in:
Leo Hemsted
2017-01-19 12:10:32 +00:00
parent c904025ee9
commit 4f238d241a
4 changed files with 200 additions and 29 deletions

View File

@@ -220,20 +220,24 @@ def persist_letter(
created_at
):
notification = encryption.decrypt(encrypted_notification)
# we store the recipient as just the first item of the person's address
recipient = notification['personalisation']['addressline1']
service = dao_fetch_service_by_id(service_id)
try:
saved_notification = persist_notification(
template_id=notification['template'],
template_version=notification['template_version'],
recipient=notification['to'],
recipient=recipient,
service=service,
personalisation=notification.get('personalisation'),
notification_type=EMAIL_TYPE,
personalisation=notification['personalisation'],
notification_type=LETTER_TYPE,
api_key_id=None,
key_type=KEY_TYPE_NORMAL,
created_at=created_at,
job_id=notification.get('job', None),
job_row_number=notification.get('row_number', None),
job_id=notification['job'],
job_row_number=notification['row_number'],
notification_id=notification_id
)