mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Parking some code that updates stats when notification delivery happens
This commit is contained in:
@@ -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):
|
class FiretextClientException(SmsClientException):
|
||||||
def __init__(self, response):
|
def __init__(self, response):
|
||||||
self.code = response['code']
|
self.code = response['code']
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ from app import db
|
|||||||
from app.models import Notification, Job, NotificationStatistics, TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL
|
from app.models import Notification, Job, NotificationStatistics, TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL
|
||||||
from sqlalchemy import desc
|
from sqlalchemy import desc
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from app.clients.sms.firetext import FiretextResponses
|
||||||
|
|
||||||
def dao_get_notification_statistics_for_service(service_id):
|
def dao_get_notification_statistics_for_service(service_id):
|
||||||
return NotificationStatistics.query.filter_by(
|
return NotificationStatistics.query.filter_by(
|
||||||
@@ -28,7 +28,12 @@ def dao_create_notification(notification, notification_type):
|
|||||||
Job.updated_at: datetime.utcnow()
|
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(
|
stats = NotificationStatistics(
|
||||||
day=notification.created_at.strftime('%Y-%m-%d'),
|
day=notification.created_at.strftime('%Y-%m-%d'),
|
||||||
service_id=notification.service_id,
|
service_id=notification.service_id,
|
||||||
@@ -43,20 +48,24 @@ def dao_create_notification(notification, notification_type):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def update_notification_stats(notification, notification_type):
|
def update_query(notification_type, status):
|
||||||
if notification_type == TEMPLATE_TYPE_SMS:
|
print(notification_type)
|
||||||
update = {
|
print(status)
|
||||||
NotificationStatistics.sms_requested: NotificationStatistics.sms_requested + 1
|
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 = {
|
return {
|
||||||
NotificationStatistics.emails_requested: NotificationStatistics.emails_requested + 1
|
mapping[notification_type][status]: mapping[notification_type][status] + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.session.query(NotificationStatistics).filter_by(
|
|
||||||
day=notification.created_at.strftime('%Y-%m-%d'),
|
|
||||||
service_id=notification.service_id
|
|
||||||
).update(update)
|
|
||||||
|
|
||||||
|
|
||||||
def dao_update_notification(notification):
|
def dao_update_notification(notification):
|
||||||
@@ -71,6 +80,13 @@ def update_notification_status_by_id(notification_id, status):
|
|||||||
).update({
|
).update({
|
||||||
Notification.status: status
|
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()
|
db.session.commit()
|
||||||
return count
|
return count
|
||||||
|
|
||||||
|
|||||||
@@ -316,7 +316,7 @@ def test_save_notification_and_increment_email_stats(sample_email_template, samp
|
|||||||
assert stats1.emails_requested == 1
|
assert stats1.emails_requested == 1
|
||||||
assert stats1.sms_requested == 0
|
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
|
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
|
assert Job.query.get(sample_job.id).notifications_sent == 1
|
||||||
|
|
||||||
notification_2 = Notification(**data)
|
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 Notification.query.count() == 2
|
||||||
assert Job.query.get(sample_job.id).notifications_sent == 2
|
assert Job.query.get(sample_job.id).notifications_sent == 2
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user