Don't send the SMS if we have failed to save in the database

This commit is contained in:
Martyn Inglis
2016-02-16 17:42:04 +00:00
parent 0933e5c647
commit a2341be0e2
4 changed files with 26 additions and 23 deletions

View File

@@ -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)