mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:26:08 -05:00
Merge pull request #1635 from alphagov/remove-failed-as-a-status
Remove failed as a possible status
This commit is contained in:
@@ -5,7 +5,6 @@ from monotonic import monotonic
|
||||
from requests import request, RequestException
|
||||
|
||||
from app.clients.sms import (SmsClient, SmsClientResponseException)
|
||||
from app.clients import STATISTICS_DELIVERED, STATISTICS_FAILURE
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -15,24 +14,9 @@ logger = logging.getLogger(__name__)
|
||||
# the notification status to temporary-failure rather than permanent failure.
|
||||
# See the code in the notification_dao.update_notifications_status_by_id
|
||||
firetext_responses = {
|
||||
'0': {
|
||||
"message": 'Delivered',
|
||||
"notification_statistics_status": STATISTICS_DELIVERED,
|
||||
"success": True,
|
||||
"notification_status": 'delivered'
|
||||
},
|
||||
'1': {
|
||||
"message": 'Declined',
|
||||
"success": False,
|
||||
"notification_statistics_status": STATISTICS_FAILURE,
|
||||
"notification_status": 'permanent-failure'
|
||||
},
|
||||
'2': {
|
||||
"message": 'Undelivered (Pending with Network)',
|
||||
"success": True,
|
||||
"notification_statistics_status": None,
|
||||
"notification_status": 'pending'
|
||||
}
|
||||
'0': 'delivered',
|
||||
'1': 'permanent-failure',
|
||||
'2': 'pending'
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,45 +1,18 @@
|
||||
import json
|
||||
from monotonic import monotonic
|
||||
from requests import (request, RequestException)
|
||||
from app.clients import (STATISTICS_DELIVERED, STATISTICS_FAILURE)
|
||||
from app.clients.sms import (SmsClient, SmsClientResponseException)
|
||||
|
||||
mmg_response_map = {
|
||||
'2': {
|
||||
"message": ' Permanent failure',
|
||||
"notification_statistics_status": STATISTICS_FAILURE,
|
||||
"success": False,
|
||||
"notification_status": 'permanent-failure'
|
||||
},
|
||||
'3': {
|
||||
"message": 'Delivered',
|
||||
"notification_statistics_status": STATISTICS_DELIVERED,
|
||||
"success": True,
|
||||
"notification_status": 'delivered'
|
||||
},
|
||||
'4': {
|
||||
"message": ' Temporary failure',
|
||||
"notification_statistics_status": STATISTICS_FAILURE,
|
||||
"success": False,
|
||||
"notification_status": 'temporary-failure'
|
||||
},
|
||||
'5': {
|
||||
"message": 'Permanent failure',
|
||||
"notification_statistics_status": STATISTICS_FAILURE,
|
||||
"success": False,
|
||||
"notification_status": 'permanent-failure'
|
||||
},
|
||||
'default': {
|
||||
"message": 'Declined',
|
||||
"success": False,
|
||||
"notification_statistics_status": STATISTICS_FAILURE,
|
||||
"notification_status": 'failed'
|
||||
}
|
||||
'2': 'permanent-failure',
|
||||
'3': 'delivered',
|
||||
'4': 'temporary-failure',
|
||||
'5': 'permanent-failure'
|
||||
}
|
||||
|
||||
|
||||
def get_mmg_responses(status):
|
||||
return mmg_response_map.get(status, mmg_response_map.get('default'))
|
||||
return mmg_response_map[status]
|
||||
|
||||
|
||||
class MMGClientResponseException(SmsClientResponseException):
|
||||
|
||||
@@ -4,6 +4,7 @@ from datetime import datetime
|
||||
from flask import current_app
|
||||
|
||||
from app import statsd_client
|
||||
from app.clients import ClientException
|
||||
from app.dao import notifications_dao
|
||||
from app.clients.sms.firetext import get_firetext_responses
|
||||
from app.clients.sms.mmg import get_mmg_responses
|
||||
@@ -39,8 +40,8 @@ def process_sms_client_response(status, reference, client_name):
|
||||
try:
|
||||
uuid.UUID(reference, version=4)
|
||||
except ValueError:
|
||||
message = "{} callback with invalid reference {}".format(client_name, reference)
|
||||
return success, message
|
||||
errors = "{} callback with invalid reference {}".format(client_name, reference)
|
||||
return success, errors
|
||||
|
||||
try:
|
||||
response_parser = sms_response_mapper[client_name]
|
||||
@@ -49,32 +50,27 @@ def process_sms_client_response(status, reference, client_name):
|
||||
|
||||
# validate status
|
||||
try:
|
||||
response_dict = response_parser(status)
|
||||
notification_status = response_parser(status)
|
||||
current_app.logger.info('{} callback return status of {} for reference: {}'.format(
|
||||
client_name, status, reference)
|
||||
)
|
||||
except KeyError:
|
||||
msg = "{} callback failed: status {} not found.".format(client_name, status)
|
||||
return success, msg
|
||||
_process_for_status(notification_status='technical-failure', client_name=client_name, reference=reference)
|
||||
raise ClientException("{} callback failed: status {} not found.".format(client_name, status))
|
||||
|
||||
notification_status = response_dict['notification_status']
|
||||
notification_status_message = response_dict['message']
|
||||
notification_success = response_dict['success']
|
||||
success = _process_for_status(notification_status=notification_status, client_name=client_name, reference=reference)
|
||||
return success, errors
|
||||
|
||||
|
||||
def _process_for_status(notification_status, client_name, reference):
|
||||
# record stats
|
||||
notification = notifications_dao.update_notification_status_by_id(reference, notification_status)
|
||||
if not notification:
|
||||
current_app.logger.warning("{} callback failed: notification {} either not found or already updated "
|
||||
"from sending. Status {}".format(client_name,
|
||||
reference,
|
||||
notification_status_message))
|
||||
return success, errors
|
||||
|
||||
if not notification_success:
|
||||
current_app.logger.debug(
|
||||
"{} delivery failed: notification {} has error found. Status {}".format(client_name,
|
||||
reference,
|
||||
notification_status_message))
|
||||
notification_status))
|
||||
return
|
||||
|
||||
statsd_client.incr('callback.{}.{}'.format(client_name.lower(), notification_status))
|
||||
if notification.sent_at:
|
||||
@@ -92,4 +88,4 @@ def process_sms_client_response(status, reference, client_name):
|
||||
send_delivery_status_to_service.apply_async([str(notification.id)], queue=QueueNames.CALLBACKS)
|
||||
|
||||
success = "{} callback succeeded. reference {} updated".format(client_name, reference)
|
||||
return success, errors
|
||||
return success
|
||||
|
||||
Reference in New Issue
Block a user