mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Add sms notifications from a service to a queue.
This commit is contained in:
@@ -35,13 +35,15 @@ def delete_model_service(service):
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def get_model_services(service_id=None, user_id=None):
|
||||
def get_model_services(service_id=None, user_id=None, _raise=True):
|
||||
# TODO need better mapping from function params to sql query.
|
||||
if user_id and service_id:
|
||||
return Service.query.filter(
|
||||
Service.users.any(id=user_id)).filter_by(id=service_id).one()
|
||||
elif service_id:
|
||||
return Service.query.filter_by(id=service_id).one()
|
||||
result = Service.query.filter_by(id=service_id).one() if _raise else Service.query.filter_by(
|
||||
id=service_id).first()
|
||||
return result
|
||||
elif user_id:
|
||||
return Service.query.filter(Service.users.any(id=user_id)).all()
|
||||
return Service.query.all()
|
||||
|
||||
@@ -86,6 +86,7 @@ class Service(db.Model):
|
||||
restricted = db.Column(db.Boolean, index=False, unique=False, nullable=False)
|
||||
queue_name = db.Column(UUID(as_uuid=True))
|
||||
|
||||
|
||||
class ApiKey(db.Model):
|
||||
__tablename__ = 'api_key'
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import json
|
||||
|
||||
import boto3
|
||||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request,
|
||||
current_app
|
||||
)
|
||||
|
||||
from itsdangerous import URLSafeSerializer
|
||||
from app import notify_alpha_client
|
||||
from app import api_user
|
||||
from app.dao import (templates_dao, services_dao)
|
||||
@@ -20,23 +23,54 @@ def get_notifications(notification_id):
|
||||
return jsonify(notify_alpha_client.fetch_notification_by_id(notification_id)), 200
|
||||
|
||||
|
||||
def _add_notification_to_queue(template_id, service, msg_type, to):
|
||||
q = boto3.resource('sqs', region_name=current_app.config['AWS_REGION']).create_queue(
|
||||
QueueName=str(service.queue_name))
|
||||
import uuid
|
||||
notification = json.dumps({'message_id': str(uuid.uuid4()),
|
||||
'service_id': service.id,
|
||||
'to': to,
|
||||
'message_type': msg_type,
|
||||
'template_id': template_id})
|
||||
serializer = URLSafeSerializer(current_app.config.get('SECRET_KEY'))
|
||||
encrypted = serializer.dumps(notification, current_app.config.get('DANGEROUS_SALT'))
|
||||
q.send_message(MessageBody=encrypted,
|
||||
MessageAttributes={'type': {'StringValue': msg_type, 'DataType': 'String'},
|
||||
'service_id': {'StringValue': str(service.id), 'DataType': 'String'},
|
||||
'template_id': {'StringValue': str(template_id), 'DataType': 'String'}})
|
||||
|
||||
|
||||
@notifications.route('/sms', methods=['POST'])
|
||||
def create_sms_notification():
|
||||
notification = request.get_json()['notification']
|
||||
errors = {}
|
||||
to, to_errors = validate_to(notification, api_user['client'])
|
||||
print("create sms")
|
||||
print(notification)
|
||||
template, template_errors = validate_template(notification, api_user['client'])
|
||||
if to_errors['to']:
|
||||
errors.update(to_errors)
|
||||
if template_errors['template']:
|
||||
errors.update(template_errors)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
return jsonify(notify_alpha_client.send_sms(
|
||||
mobile_number=to,
|
||||
message=template)), 200
|
||||
# TODO: should create a different endpoint for the admin client to send verify codes.
|
||||
if api_user['client'] == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
to, to_errors = validate_to_for_admin_client(notification)
|
||||
content, content_errors = validate_content_for_admin_client(notification)
|
||||
if to_errors['to']:
|
||||
errors.update(to_errors)
|
||||
if content_errors['content']:
|
||||
errors.update(content_errors)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
return jsonify(notify_alpha_client.send_sms(mobile_number=to, message=content)), 200
|
||||
|
||||
else:
|
||||
to, to_errors = validate_to(notification, api_user['client'])
|
||||
template, template_errors = validate_template(notification, api_user['client'])
|
||||
if to_errors['to']:
|
||||
errors.update(to_errors)
|
||||
if template_errors['template']:
|
||||
errors.update(template_errors)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
# add notification to the queue
|
||||
service = services_dao.get_model_services(api_user['client'], _raise=False)
|
||||
_add_notification_to_queue(template.id, service, 'sms', to)
|
||||
return jsonify(notify_alpha_client.send_sms(mobile_number=to, message=template.content)), 200
|
||||
|
||||
|
||||
@notifications.route('/email', methods=['POST'])
|
||||
@@ -66,36 +100,52 @@ def validate_to(json_body, service_id):
|
||||
else:
|
||||
if not mobile_regex.match(mob):
|
||||
errors['to'].append('invalid phone number, must be of format +441234123123')
|
||||
if service_id != current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
service = services_dao.get_model_services(service_id=service_id)
|
||||
if service.restricted:
|
||||
valid = False
|
||||
for usr in service.users:
|
||||
if mob == usr.mobile_number:
|
||||
valid = True
|
||||
break
|
||||
if not valid:
|
||||
errors['to'].append('Invalid phone number for restricted service')
|
||||
|
||||
service = services_dao.get_model_services(service_id=service_id)
|
||||
if service.restricted:
|
||||
valid = False
|
||||
for usr in service.users:
|
||||
if mob == usr.mobile_number:
|
||||
valid = True
|
||||
break
|
||||
if not valid:
|
||||
errors['to'].append('Invalid phone number for restricted service')
|
||||
return mob, errors
|
||||
|
||||
|
||||
def validate_to_for_admin_client(json_body):
|
||||
errors = {"to": []}
|
||||
mob = json_body.get('to', None)
|
||||
if not mob:
|
||||
errors['to'].append('Required data missing')
|
||||
else:
|
||||
if not mobile_regex.match(mob):
|
||||
errors['to'].append('invalid phone number, must be of format +441234123123')
|
||||
return mob, errors
|
||||
|
||||
|
||||
def validate_template(json_body, service_id):
|
||||
errors = {"template": []}
|
||||
template_id = json_body.get('template', None)
|
||||
content = ''
|
||||
template = ''
|
||||
if not template_id:
|
||||
errors['template'].append('Required data missing')
|
||||
else:
|
||||
if service_id == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
content = json_body['template']
|
||||
else:
|
||||
try:
|
||||
template = templates_dao.get_model_templates(
|
||||
template_id=json_body['template'],
|
||||
service_id=service_id)
|
||||
content = template.content
|
||||
except:
|
||||
errors['template'].append("Unable to load template.")
|
||||
try:
|
||||
template = templates_dao.get_model_templates(
|
||||
template_id=json_body['template'],
|
||||
service_id=service_id)
|
||||
except:
|
||||
errors['template'].append("Unable to load template.")
|
||||
return template, errors
|
||||
|
||||
|
||||
def validate_content_for_admin_client(json_body):
|
||||
errors = {"content": []}
|
||||
content = json_body.get('template', None)
|
||||
if not content:
|
||||
errors['content'].append('Required content')
|
||||
|
||||
return content, errors
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ class UserSchema(ma.ModelSchema):
|
||||
class ServiceSchema(ma.ModelSchema):
|
||||
class Meta:
|
||||
model = models.Service
|
||||
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs")
|
||||
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", "queue_name")
|
||||
|
||||
|
||||
class TemplateSchema(ma.ModelSchema):
|
||||
|
||||
Reference in New Issue
Block a user