mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-07 03:48:26 -04:00
Update the billable units with the page count from the response file for letter notifications.
This commit is contained in:
@@ -33,20 +33,19 @@ from app.dao.jobs_dao import (
|
|||||||
dao_get_job_by_id,
|
dao_get_job_by_id,
|
||||||
all_notifications_are_created_for_job,
|
all_notifications_are_created_for_job,
|
||||||
dao_get_all_notifications_for_job,
|
dao_get_all_notifications_for_job,
|
||||||
dao_update_job_status)
|
dao_update_job_status
|
||||||
|
)
|
||||||
from app.dao.notifications_dao import (
|
from app.dao.notifications_dao import (
|
||||||
get_notification_by_id,
|
get_notification_by_id,
|
||||||
update_notification_status_by_reference,
|
|
||||||
dao_update_notifications_for_job_to_sent_to_dvla,
|
dao_update_notifications_for_job_to_sent_to_dvla,
|
||||||
dao_update_notifications_by_reference,
|
dao_update_notifications_by_reference,
|
||||||
dao_get_last_notification_added_for_job_id)
|
dao_get_last_notification_added_for_job_id
|
||||||
|
)
|
||||||
from app.dao.provider_details_dao import get_current_provider
|
from app.dao.provider_details_dao import get_current_provider
|
||||||
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
|
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
|
||||||
from app.dao.services_dao import dao_fetch_service_by_id, fetch_todays_total_message_count
|
from app.dao.services_dao import dao_fetch_service_by_id, fetch_todays_total_message_count
|
||||||
from app.dao.templates_dao import dao_get_template_by_id
|
from app.dao.templates_dao import dao_get_template_by_id
|
||||||
from app.models import (
|
from app.models import (
|
||||||
Job,
|
|
||||||
Notification,
|
|
||||||
DVLA_RESPONSE_STATUS_SENT,
|
DVLA_RESPONSE_STATUS_SENT,
|
||||||
EMAIL_TYPE,
|
EMAIL_TYPE,
|
||||||
JOB_STATUS_CANCELLED,
|
JOB_STATUS_CANCELLED,
|
||||||
@@ -449,9 +448,12 @@ def update_letter_notifications_statuses(self, filename):
|
|||||||
for update in notification_updates:
|
for update in notification_updates:
|
||||||
status = NOTIFICATION_DELIVERED if update.status == DVLA_RESPONSE_STATUS_SENT \
|
status = NOTIFICATION_DELIVERED if update.status == DVLA_RESPONSE_STATUS_SENT \
|
||||||
else NOTIFICATION_TECHNICAL_FAILURE
|
else NOTIFICATION_TECHNICAL_FAILURE
|
||||||
notification = update_notification_status_by_reference(
|
notification = dao_update_notifications_by_reference(
|
||||||
update.reference,
|
references=[update.reference],
|
||||||
status
|
update_dict={"status": status,
|
||||||
|
"billable_units": update.page_count,
|
||||||
|
"updated_at": datetime.utcnow()
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if not notification:
|
if not notification:
|
||||||
|
|||||||
@@ -493,7 +493,6 @@ def dao_update_notifications_for_job_to_sent_to_dvla(job_id, provider):
|
|||||||
@statsd(namespace="dao")
|
@statsd(namespace="dao")
|
||||||
@transactional
|
@transactional
|
||||||
def dao_update_notifications_by_reference(references, update_dict):
|
def dao_update_notifications_by_reference(references, update_dict):
|
||||||
now = datetime.utcnow()
|
|
||||||
updated_count = Notification.query.filter(
|
updated_count = Notification.query.filter(
|
||||||
Notification.reference.in_(references)
|
Notification.reference.in_(references)
|
||||||
).update(
|
).update(
|
||||||
|
|||||||
@@ -9,12 +9,9 @@ from app.models import (
|
|||||||
Notification,
|
Notification,
|
||||||
NOTIFICATION_CREATED,
|
NOTIFICATION_CREATED,
|
||||||
NOTIFICATION_DELIVERED,
|
NOTIFICATION_DELIVERED,
|
||||||
NOTIFICATION_TECHNICAL_FAILURE,
|
|
||||||
NOTIFICATION_SENDING,
|
NOTIFICATION_SENDING,
|
||||||
NOTIFICATION_STATUS_LETTER_RECEIVED,
|
|
||||||
NOTIFICATION_TECHNICAL_FAILURE
|
NOTIFICATION_TECHNICAL_FAILURE
|
||||||
)
|
)
|
||||||
from app.dao.notifications_dao import dao_update_notifications_by_reference
|
|
||||||
from app.celery.tasks import (
|
from app.celery.tasks import (
|
||||||
process_updates_from_file,
|
process_updates_from_file,
|
||||||
update_dvla_job_to_error,
|
update_dvla_job_to_error,
|
||||||
@@ -96,8 +93,10 @@ def test_update_letter_notifications_statuses_builds_updates_list(notify_api, mo
|
|||||||
|
|
||||||
|
|
||||||
def test_update_letter_notifications_statuses_persisted(notify_api, mocker, sample_letter_template):
|
def test_update_letter_notifications_statuses_persisted(notify_api, mocker, sample_letter_template):
|
||||||
sent_letter = create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING)
|
sent_letter = create_notification(sample_letter_template, reference='ref-foo', status=NOTIFICATION_SENDING,
|
||||||
failed_letter = create_notification(sample_letter_template, reference='ref-bar', status=NOTIFICATION_SENDING)
|
billable_units=0)
|
||||||
|
failed_letter = create_notification(sample_letter_template, reference='ref-bar', status=NOTIFICATION_SENDING,
|
||||||
|
billable_units=0)
|
||||||
|
|
||||||
valid_file = '{}|Sent|1|Unsorted\n{}|Failed|2|Sorted'.format(
|
valid_file = '{}|Sent|1|Unsorted\n{}|Failed|2|Sorted'.format(
|
||||||
sent_letter.reference, failed_letter.reference)
|
sent_letter.reference, failed_letter.reference)
|
||||||
@@ -106,7 +105,11 @@ def test_update_letter_notifications_statuses_persisted(notify_api, mocker, samp
|
|||||||
update_letter_notifications_statuses(filename='foo.txt')
|
update_letter_notifications_statuses(filename='foo.txt')
|
||||||
|
|
||||||
assert sent_letter.status == NOTIFICATION_DELIVERED
|
assert sent_letter.status == NOTIFICATION_DELIVERED
|
||||||
|
assert sent_letter.billable_units == 1
|
||||||
|
assert sent_letter.updated_at
|
||||||
assert failed_letter.status == NOTIFICATION_TECHNICAL_FAILURE
|
assert failed_letter.status == NOTIFICATION_TECHNICAL_FAILURE
|
||||||
|
assert failed_letter.billable_units == 2
|
||||||
|
assert failed_letter.updated
|
||||||
|
|
||||||
|
|
||||||
def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references(
|
def test_update_letter_notifications_to_sent_to_dvla_updates_based_on_notification_references(
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ from app.dao.notifications_dao import (
|
|||||||
set_scheduled_notification_to_processed,
|
set_scheduled_notification_to_processed,
|
||||||
update_notification_status_by_id,
|
update_notification_status_by_id,
|
||||||
update_notification_status_by_reference,
|
update_notification_status_by_reference,
|
||||||
dao_get_last_notification_added_for_job_id)
|
dao_get_last_notification_added_for_job_id, dao_update_notifications_by_reference)
|
||||||
|
|
||||||
from app.dao.services_dao import dao_update_service
|
from app.dao.services_dao import dao_update_service
|
||||||
from tests.app.db import (
|
from tests.app.db import (
|
||||||
@@ -2065,3 +2065,38 @@ def test_dao_get_last_notification_added_for_job_id_no_notifications(sample_temp
|
|||||||
def test_dao_get_last_notification_added_for_job_id_no_notifications(sample_template, fake_uuid):
|
def test_dao_get_last_notification_added_for_job_id_no_notifications(sample_template, fake_uuid):
|
||||||
|
|
||||||
assert dao_get_last_notification_added_for_job_id(fake_uuid) is None
|
assert dao_get_last_notification_added_for_job_id(fake_uuid) is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_dao_update_notifications_by_reference_updated_notificaitons_and_history(sample_template):
|
||||||
|
notification_0 = create_notification(template=sample_template, reference='noref')
|
||||||
|
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}
|
||||||
|
)
|
||||||
|
assert updated_count == 2
|
||||||
|
updated_1 = Notification.query.get(notification_1.id)
|
||||||
|
assert updated_1.billable_units == 2
|
||||||
|
assert updated_1.status == 'delivered'
|
||||||
|
updated_2 = Notification.query.get(notification_2.id)
|
||||||
|
assert updated_2.billable_units == 2
|
||||||
|
assert updated_2.status == 'delivered'
|
||||||
|
|
||||||
|
updated_history_1 = NotificationHistory.query.get(notification_1.id)
|
||||||
|
assert updated_history_1.billable_units == 2
|
||||||
|
assert updated_history_1.status == 'delivered'
|
||||||
|
updated_history_2 = Notification.query.get(notification_2.id)
|
||||||
|
assert updated_history_2.billable_units == 2
|
||||||
|
assert updated_history_2.status == 'delivered'
|
||||||
|
|
||||||
|
assert notification_0 == Notification.query.get(notification_0.id)
|
||||||
|
|
||||||
|
|
||||||
|
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}
|
||||||
|
)
|
||||||
|
assert updated_count == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user