diff --git a/app/clients/sms/firetext.py b/app/clients/sms/firetext.py index 5b41c2811..9fdf95c2f 100644 --- a/app/clients/sms/firetext.py +++ b/app/clients/sms/firetext.py @@ -28,6 +28,38 @@ firetext_response_status = { } +class FiretextResponses: + response_model = { + '0': { + "firetext_message": 'delivered', + "stats_mapping": 'delivered', + "success": True, + "notify_status": 'delivered' + }, + '1': { + "firetext_message": 'declined', + "success": False, + "stats_mapping": 'failure', + "notify_status": 'failed' + }, + '2': { + "firetext_message": 'Undelivered (Pending with Network)', + "success": False, + "stats_mapping": None, + "notify_status": 'sent' + } + } + + def response_code_to_message(self, response_code): + return self.response_model.get(response_code)['firetext_message'] + + def response_code_to_notify_status(self, response_code): + return self.response_model.get(response_code)['notify_status'] + + def response_code_to_notify_stats(self, response_code): + return self.response_model.get(response_code)['stats_mapping'] + + class FiretextClientException(SmsClientException): def __init__(self, response): self.code = response['code'] diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index daf5bd1ff..f2ef227ac 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -3,7 +3,7 @@ from app import db from app.models import Notification, Job, NotificationStatistics, TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL from sqlalchemy import desc from datetime import datetime, timedelta - +from app.clients.sms.firetext import FiretextResponses def dao_get_notification_statistics_for_service(service_id): return NotificationStatistics.query.filter_by( @@ -28,7 +28,12 @@ def dao_create_notification(notification, notification_type): Job.updated_at: datetime.utcnow() }) - if update_notification_stats(notification, notification_type) == 0: + update_count = db.session.query(NotificationStatistics).filter_by( + day=notification.created_at.strftime('%Y-%m-%d'), + service_id=notification.service_id + ).update(update_query(notification_type, 'requested')) + + if update_count == 0: stats = NotificationStatistics( day=notification.created_at.strftime('%Y-%m-%d'), service_id=notification.service_id, @@ -43,20 +48,24 @@ def dao_create_notification(notification, notification_type): raise -def update_notification_stats(notification, notification_type): - if notification_type == TEMPLATE_TYPE_SMS: - update = { - NotificationStatistics.sms_requested: NotificationStatistics.sms_requested + 1 +def update_query(notification_type, status): + print(notification_type) + print(status) + mapping = { + 'sms': { + 'requested': NotificationStatistics.sms_requested, + 'success': NotificationStatistics.sms_delivered, + 'failure': NotificationStatistics.sms_error + }, + 'email': { + 'requested': NotificationStatistics.emails_requested, + 'success': NotificationStatistics.emails_delivered, + 'failure': NotificationStatistics.emails_error } - else: - update = { - NotificationStatistics.emails_requested: NotificationStatistics.emails_requested + 1 - } - - return db.session.query(NotificationStatistics).filter_by( - day=notification.created_at.strftime('%Y-%m-%d'), - service_id=notification.service_id - ).update(update) + } + return { + mapping[notification_type][status]: mapping[notification_type][status] + 1 + } def dao_update_notification(notification): @@ -71,6 +80,13 @@ def update_notification_status_by_id(notification_id, status): ).update({ Notification.status: status }) + if count == 1: + notification = Notification.query.get(notification_id) + db.session.query(NotificationStatistics).filter_by( + day=notification.created_at.strftime('%Y-%m-%d'), + service_id=notification.service_id + ).update(update_query(notification.template.template_type, FiretextResponses.response_code_to_notify_stats(status))) + db.session.commit() return count diff --git a/tests/app/dao/test_notification_dao.py b/tests/app/dao/test_notification_dao.py index d6a5a95b5..da697ee1c 100644 --- a/tests/app/dao/test_notification_dao.py +++ b/tests/app/dao/test_notification_dao.py @@ -316,7 +316,7 @@ def test_save_notification_and_increment_email_stats(sample_email_template, samp assert stats1.emails_requested == 1 assert stats1.sms_requested == 0 - dao_create_notification(notification_2, sample_email_template) + dao_create_notification(notification_2, sample_email_template.template_type) assert Notification.query.count() == 2 @@ -412,7 +412,7 @@ def test_save_notification_and_increment_job(sample_template, sample_job): assert Job.query.get(sample_job.id).notifications_sent == 1 notification_2 = Notification(**data) - dao_create_notification(notification_2, sample_template) + dao_create_notification(notification_2, sample_template.template_type) assert Notification.query.count() == 2 assert Job.query.get(sample_job.id).notifications_sent == 2