mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
Don't send the SMS if we have failed to save in the database
This commit is contained in:
@@ -4,6 +4,7 @@ from app.dao.templates_dao import get_model_templates
|
||||
from app.dao.notifications_dao import save_notification
|
||||
from app.models import Notification
|
||||
from flask import current_app
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
|
||||
@notify_celery.task(name="send-sms")
|
||||
@@ -11,20 +12,21 @@ def send_sms(service_id, notification_id, encrypted_notification):
|
||||
notification = encryption.decrypt(encrypted_notification)
|
||||
template = get_model_templates(notification['template'])
|
||||
|
||||
status = 'sent'
|
||||
|
||||
try:
|
||||
twilio_client.send_sms(notification['to'], template.content)
|
||||
except TwilioClientException as e:
|
||||
current_app.logger.info(e)
|
||||
status = 'failed'
|
||||
notification_db_object = Notification(
|
||||
id=notification_id,
|
||||
template_id=notification['template'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
status='sent'
|
||||
)
|
||||
save_notification(notification_db_object)
|
||||
|
||||
notification_db_object = Notification(
|
||||
id=notification_id,
|
||||
template_id=notification['template'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
status=status
|
||||
)
|
||||
try:
|
||||
twilio_client.send_sms(notification['to'], template.content)
|
||||
except TwilioClientException as e:
|
||||
current_app.logger.debug(e)
|
||||
save_notification(notification_db_object, {"status": "failed"})
|
||||
|
||||
save_notification(notification_db_object)
|
||||
except SQLAlchemyError as e:
|
||||
current_app.logger.debug(e)
|
||||
|
||||
@@ -4,14 +4,12 @@ from itsdangerous import URLSafeSerializer
|
||||
|
||||
|
||||
class Encryption:
|
||||
|
||||
def init_app(self, app):
|
||||
self.serializer = URLSafeSerializer(app.config.get('SECRET_KEY'))
|
||||
self.salt = app.config.get('DANGEROUS_SALT')
|
||||
|
||||
def encrypt(self, thing_to_encrypt):
|
||||
return self.serializer.dumps(thing_to_encrypt, salt=
|
||||
self.salt)
|
||||
return self.serializer.dumps(thing_to_encrypt, salt=self.salt)
|
||||
|
||||
def decrypt(self, thing_to_decrypt):
|
||||
return self.serializer.loads(thing_to_decrypt, salt=self.salt)
|
||||
|
||||
Reference in New Issue
Block a user