mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-23 15:56:45 -04:00
Merge pull request #2551 from alphagov/fix-0-billable-units
Fix 0 billable units on provider callback
This commit is contained in:
@@ -2,6 +2,7 @@ import uuid
|
|||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
|
from notifications_utils.template import SMSMessageTemplate
|
||||||
|
|
||||||
from app import statsd_client
|
from app import statsd_client
|
||||||
from app.clients import ClientException
|
from app.clients import ClientException
|
||||||
@@ -15,6 +16,7 @@ from app.celery.service_callback_tasks import (
|
|||||||
from app.config import QueueNames
|
from app.config import QueueNames
|
||||||
from app.dao.notifications_dao import dao_update_notification
|
from app.dao.notifications_dao import dao_update_notification
|
||||||
from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service
|
from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service
|
||||||
|
from app.dao.templates_dao import dao_get_template_by_id
|
||||||
from app.models import NOTIFICATION_PENDING
|
from app.models import NOTIFICATION_PENDING
|
||||||
|
|
||||||
sms_response_mapper = {
|
sms_response_mapper = {
|
||||||
@@ -92,6 +94,19 @@ def _process_for_status(notification_status, client_name, provider_reference):
|
|||||||
notification.sent_at
|
notification.sent_at
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if notification.billable_units == 0:
|
||||||
|
service = notification.service
|
||||||
|
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
|
||||||
|
|
||||||
|
template = SMSMessageTemplate(
|
||||||
|
template_model.__dict__,
|
||||||
|
values=notification.personalisation,
|
||||||
|
prefix=service.name,
|
||||||
|
show_prefix=service.prefix_sms,
|
||||||
|
)
|
||||||
|
notification.billable_units = template.fragment_count
|
||||||
|
notifications_dao.dao_update_notification(notification)
|
||||||
|
|
||||||
if notification_status != NOTIFICATION_PENDING:
|
if notification_status != NOTIFICATION_PENDING:
|
||||||
service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=notification.service_id)
|
service_callback_api = get_service_delivery_status_callback_api_for_service(service_id=notification.service_id)
|
||||||
# queue callback task only if the service_callback_api exists
|
# queue callback task only if the service_callback_api exists
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ def test_process_sms_response_does_not_send_status_update_for_pending(sample_not
|
|||||||
send_mock.assert_not_called()
|
send_mock.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
def test_process_sms_updates_sent_by_with_client_name_if_not_in_noti(notify_db, sample_notification):
|
def test_process_sms_updates_sent_by_with_client_name_if_not_in_noti(sample_notification):
|
||||||
sample_notification.sent_by = None
|
sample_notification.sent_by = None
|
||||||
success, error = process_sms_client_response(
|
success, error = process_sms_client_response(
|
||||||
status='3', provider_reference=str(sample_notification.id), client_name='MMG')
|
status='3', provider_reference=str(sample_notification.id), client_name='MMG')
|
||||||
@@ -105,7 +105,16 @@ def test_process_sms_updates_sent_by_with_client_name_if_not_in_noti(notify_db,
|
|||||||
assert sample_notification.sent_by == 'mmg'
|
assert sample_notification.sent_by == 'mmg'
|
||||||
|
|
||||||
|
|
||||||
def test_process_sms_does_not_update_sent_by_if_already_set(mocker, notify_db, sample_notification):
|
def test_process_sms_updates_billable_units_if_zero(sample_notification):
|
||||||
|
sample_notification.billable_units = 0
|
||||||
|
success, error = process_sms_client_response(
|
||||||
|
status='3', provider_reference=str(sample_notification.id), client_name='MMG')
|
||||||
|
assert error is None
|
||||||
|
assert success == 'MMG callback succeeded. reference {} updated'.format(sample_notification.id)
|
||||||
|
assert sample_notification.billable_units == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_process_sms_does_not_update_sent_by_if_already_set(mocker, sample_notification):
|
||||||
mock_update = mocker.patch('app.notifications.process_client_response.set_notification_sent_by')
|
mock_update = mocker.patch('app.notifications.process_client_response.set_notification_sent_by')
|
||||||
sample_notification.sent_by = 'MMG'
|
sample_notification.sent_by = 'MMG'
|
||||||
process_sms_client_response(
|
process_sms_client_response(
|
||||||
|
|||||||
Reference in New Issue
Block a user