mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 17:01:35 -05:00
Add process_mmg_responses
Refactor process_firetext_responses Removed the abstract ClientResponses for firetext and mmg. There is a map for each response to handle the status codes sent by each client. Since MMG has about 20 different status code, none of which seem to be a pending state (unlike firetext that has 3 status one for pending - network delay). For MMG status codes, look for 00 as successful, everything else is assumed to be a failure.
This commit is contained in:
71
app/notifications/process_client_response.py
Normal file
71
app/notifications/process_client_response.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import uuid
|
||||
from flask import current_app
|
||||
from app.dao import notifications_dao
|
||||
from app.clients.sms.firetext import get_firetext_responses
|
||||
from app.clients.sms.mmg import get_mmg_responses
|
||||
|
||||
sms_response_mapper = {'MMG': get_mmg_responses,
|
||||
'Firetext': get_firetext_responses
|
||||
}
|
||||
|
||||
|
||||
def validate_callback_data(data, fields, client_name):
|
||||
errors = []
|
||||
for f in fields:
|
||||
if len(data.get(f, '')) <= 0:
|
||||
error = "{} callback failed: {} missing".format(client_name, f)
|
||||
errors.append(error)
|
||||
return errors if len(errors) > 0 else None
|
||||
|
||||
|
||||
def process_sms_client_response(status, reference, client_name):
|
||||
success = None
|
||||
errors = None
|
||||
# validate reference
|
||||
if reference == 'send-sms-code':
|
||||
success = "{} callback succeeded: send-sms-code".format(client_name)
|
||||
return success, errors
|
||||
|
||||
try:
|
||||
uuid.UUID(reference, version=4)
|
||||
except ValueError:
|
||||
message = "{} callback with invalid reference {}".format(client_name, reference)
|
||||
return success, message
|
||||
|
||||
try:
|
||||
response_parser = sms_response_mapper[client_name]
|
||||
except KeyError:
|
||||
return success, 'unknown sms client: {}'.format(client_name)
|
||||
|
||||
# validate status
|
||||
try:
|
||||
response_dict = 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
|
||||
|
||||
notification_status = response_dict['notification_status']
|
||||
notification_statistics_status = response_dict['notification_statistics_status']
|
||||
notification_status_message = response_dict['message']
|
||||
notification_success = response_dict['success']
|
||||
|
||||
# record stats
|
||||
update_success = notifications_dao.update_notification_status_by_id(reference,
|
||||
notification_status,
|
||||
notification_statistics_status)
|
||||
if update_success == 0:
|
||||
status_error = "{} callback failed: notification {} not found. Status {}".format(client_name,
|
||||
reference,
|
||||
notification_status_message)
|
||||
return success, status_error
|
||||
|
||||
if not notification_success:
|
||||
current_app.logger.info(
|
||||
"{} delivery failed: notification {} has error found. Status {}".format(client_name,
|
||||
reference,
|
||||
notification_status_message))
|
||||
|
||||
success = "{} callback succeeded. reference {} updated".format(client_name, reference)
|
||||
return success, errors
|
||||
Reference in New Issue
Block a user