mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 05:28:49 -04:00
Updates to fire text integration:
- client updated to raise errors with fire text error codes/messages New endpoint - /notifications/sms/firetext For delivery notifications to be sent to.
This commit is contained in:
@@ -71,7 +71,7 @@ def create_app():
|
||||
def init_app(app):
|
||||
@app.before_request
|
||||
def required_authentication():
|
||||
if request.path != url_for('status.show_status'):
|
||||
if request.path not in [url_for('status.show_status'), url_for('notifications.process_firetext_response')]:
|
||||
from app.authentication import auth
|
||||
error = auth.requires_auth()
|
||||
if error:
|
||||
|
||||
@@ -208,6 +208,9 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
|
||||
notification_id=notification_id
|
||||
)
|
||||
except FiretextClientException as e:
|
||||
current_app.logger.error(
|
||||
"SMS notification {} failed".format(notification_id)
|
||||
)
|
||||
current_app.logger.exception(e)
|
||||
notification_db_object.status = 'failed'
|
||||
dao_update_notification(notification_db_object)
|
||||
|
||||
@@ -9,6 +9,24 @@ from requests import request, RequestException, HTTPError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
firetext_response_status = {
|
||||
'0': {
|
||||
"firetext_message": 'delivered',
|
||||
"success": True,
|
||||
"notify_status": 'delivered'
|
||||
},
|
||||
'1': {
|
||||
"firetext_message": 'declined',
|
||||
"success": False,
|
||||
"notify_status": 'failed'
|
||||
},
|
||||
'2': {
|
||||
"firetext_message": 'Undelivered (Pending with Network)',
|
||||
"success": False,
|
||||
"notify_status": 'sent'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class FiretextClientException(SmsClientException):
|
||||
def __init__(self, response):
|
||||
|
||||
@@ -86,6 +86,10 @@ def get_notification(service_id, notification_id):
|
||||
return Notification.query.filter_by(service_id=service_id, id=notification_id).one()
|
||||
|
||||
|
||||
def get_notification_by_id(notification_id):
|
||||
return Notification.query.filter_by(id=notification_id).first()
|
||||
|
||||
|
||||
def get_notifications_for_service(service_id, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id).order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
|
||||
@@ -231,7 +231,7 @@ class VerifyCode(db.Model):
|
||||
return check_hash(cde, self._code)
|
||||
|
||||
|
||||
NOTIFICATION_STATUS_TYPES = ['sent', 'failed']
|
||||
NOTIFICATION_STATUS_TYPES = ['sent', 'delivered', 'failed']
|
||||
|
||||
|
||||
class Notification(db.Model):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
|
||||
from flask import (
|
||||
Blueprint,
|
||||
@@ -9,7 +10,7 @@ from flask import (
|
||||
)
|
||||
|
||||
from utils.template import Template
|
||||
|
||||
from app.clients.sms.firetext import firetext_response_status
|
||||
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT
|
||||
from app.authentication.auth import require_admin
|
||||
from app.dao import (
|
||||
@@ -32,6 +33,67 @@ from app.errors import register_errors
|
||||
register_errors(notifications)
|
||||
|
||||
|
||||
@notifications.route('/notifications/sms/firetext', methods=['POST'])
|
||||
def process_firetext_response():
|
||||
if 'status' not in request.form:
|
||||
current_app.logger.info(
|
||||
"Firetext callback failed: status missing"
|
||||
)
|
||||
return jsonify(result="error", message="Firetext callback failed: status missing"), 400
|
||||
|
||||
if len(request.form.get('reference', '')) <= 0:
|
||||
current_app.logger.info(
|
||||
"Firetext callback with no reference"
|
||||
)
|
||||
return jsonify(result="success", message="Firetext callback succeeded"), 200
|
||||
|
||||
notification_id = request.form['reference']
|
||||
status = request.form['status']
|
||||
|
||||
try:
|
||||
uuid.UUID(notification_id, version=4)
|
||||
except ValueError:
|
||||
current_app.logger.info(
|
||||
"Firetext callback with invalid reference {}".format(notification_id)
|
||||
)
|
||||
return jsonify(
|
||||
result="error", message="Firetext callback with invalid reference {}".format(notification_id)
|
||||
), 400
|
||||
|
||||
notification_status = firetext_response_status.get(status, None)
|
||||
if not notification_status:
|
||||
current_app.logger.info(
|
||||
"Firetext callback failed: status {} not found.".format(status)
|
||||
)
|
||||
return jsonify(result="error", message="Firetext callback failed: status {} not found.".format(status)), 400
|
||||
|
||||
notification = notifications_dao.get_notification_by_id(notification_id)
|
||||
if not notification:
|
||||
current_app.logger.info(
|
||||
"Firetext callback failed: notification {} not found. Status {}".format(notification_id, status)
|
||||
)
|
||||
return jsonify(
|
||||
result="error",
|
||||
message="Firetext callback failed: notification {} not found. Status {}".format(
|
||||
notification_id,
|
||||
notification_status['firetext_message']
|
||||
)
|
||||
), 404
|
||||
|
||||
if not notification_status['success']:
|
||||
current_app.logger.info(
|
||||
"Firetext delivery failed: notification {} has error found. Status {}".format(
|
||||
notification_id,
|
||||
firetext_response_status[status]['firetext_message']
|
||||
)
|
||||
)
|
||||
notification.status = notification_status['notify_status']
|
||||
notifications_dao.dao_update_notification(notification)
|
||||
return jsonify(
|
||||
result="success", message="Firetext callback succeeded. reference {} updated".format(notification_id)
|
||||
), 200
|
||||
|
||||
|
||||
@notifications.route('/notifications/<string:notification_id>', methods=['GET'])
|
||||
def get_notifications(notification_id):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user