Add updated history count to dao_update_notifications_by_reference

For returned letter updates most notifications won't exist in the
notifications table, so in order to find out whether the reference
matches any known letters we need to check the count of updated
history records.
This commit is contained in:
Alexey Bezhan
2018-08-30 14:27:57 +01:00
parent 0dcf04def9
commit 18ab7f3337
4 changed files with 25 additions and 15 deletions

View File

@@ -288,7 +288,7 @@ def update_letter_pdf_status(reference, status):
update_dict={
'status': status,
'updated_at': datetime.utcnow()
})
})[0]
def replay_letters_in_error(filename=None):

View File

@@ -339,7 +339,7 @@ def update_letter_notifications_to_sent_to_dvla(self, notification_references):
# This task will be called by the FTP app to update notifications as sent to DVLA
provider = get_current_provider(LETTER_TYPE)
updated_count = dao_update_notifications_by_reference(
updated_count, _ = dao_update_notifications_by_reference(
notification_references,
{
'status': NOTIFICATION_SENDING,
@@ -357,7 +357,7 @@ def update_letter_notifications_to_sent_to_dvla(self, notification_references):
def update_letter_notifications_to_error(self, notification_references):
# This task will be called by the FTP app to update notifications as sent to DVLA
updated_count = dao_update_notifications_by_reference(
updated_count, _ = dao_update_notifications_by_reference(
notification_references,
{
'status': NOTIFICATION_TECHNICAL_FAILURE,
@@ -465,7 +465,7 @@ def update_letter_notification(filename, temporary_failures, update):
status = NOTIFICATION_TEMPORARY_FAILURE
temporary_failures.append(update.reference)
updated_count = dao_update_notifications_by_reference(
updated_count, _ = dao_update_notifications_by_reference(
references=[update.reference],
update_dict={"status": status,
"billable_units": update.page_count,

View File

@@ -465,14 +465,14 @@ def dao_update_notifications_by_reference(references, update_dict):
synchronize_session=False
)
NotificationHistory.query.filter(
updated_history_count = NotificationHistory.query.filter(
NotificationHistory.reference.in_(references)
).update(
update_dict,
synchronize_session=False
)
return updated_count
return updated_count, updated_history_count
@statsd(namespace="dao")

View File

@@ -1653,9 +1653,12 @@ def test_dao_update_notifications_by_reference_updated_notificaitons_and_history
notification_1 = create_notification(template=sample_template, reference='ref')
notification_2 = create_notification(template=sample_template, reference='ref')
updated_count = dao_update_notifications_by_reference(references=['ref'],
update_dict={"status": "delivered",
"billable_units": 2}
updated_count, updated_history_count = dao_update_notifications_by_reference(
references=['ref'],
update_dict={
"status": "delivered",
"billable_units": 2
}
)
assert updated_count == 2
updated_1 = Notification.query.get(notification_1.id)
@@ -1665,6 +1668,7 @@ def test_dao_update_notifications_by_reference_updated_notificaitons_and_history
assert updated_2.billable_units == 2
assert updated_2.status == 'delivered'
assert updated_history_count == 2
updated_history_1 = NotificationHistory.query.get(notification_1.id)
assert updated_history_1.billable_units == 2
assert updated_history_1.status == 'delivered'
@@ -1676,22 +1680,28 @@ def test_dao_update_notifications_by_reference_updated_notificaitons_and_history
def test_dao_update_notifications_by_reference_returns_zero_when_no_notifications_to_update(notify_db):
updated_count = dao_update_notifications_by_reference(references=['ref'],
update_dict={"status": "delivered",
"billable_units": 2}
updated_count, updated_history_count = dao_update_notifications_by_reference(
references=['ref'],
update_dict={
"status": "delivered",
"billable_units": 2
}
)
assert updated_count == 0
assert updated_history_count == 0
def test_dao_update_notifications_by_reference_set_returned_letter_status(sample_letter_template):
notification = create_notification(template=sample_letter_template, reference='ref')
updated_count = dao_update_notifications_by_reference(
updated_count, updated_history_count = dao_update_notifications_by_reference(
references=['ref'],
update_dict={"status": "returned-letter"}
)
assert updated_count == 1
assert updated_history_count == 1
assert Notification.query.get(notification.id).status == 'returned-letter'