mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Moved the sending sms for a job into celery tasks
This commit is contained in:
@@ -9,7 +9,7 @@ from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
|
||||
@notify_celery.task(name="send-sms")
|
||||
def send_sms(service_id, notification_id, encrypted_notification):
|
||||
def send_sms(service_id, notification_id, encrypted_notification, job_id=None):
|
||||
notification = encryption.decrypt(encrypted_notification)
|
||||
template = get_model_templates(notification['template'])
|
||||
|
||||
@@ -19,6 +19,7 @@ def send_sms(service_id, notification_id, encrypted_notification):
|
||||
template_id=notification['template'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
job_id=job_id,
|
||||
status='sent'
|
||||
)
|
||||
save_notification(notification_db_object)
|
||||
|
||||
@@ -14,7 +14,7 @@ def save_job(job, update_dict={}):
|
||||
|
||||
|
||||
def get_job(service_id, job_id):
|
||||
return Job.query.filter_by(service_id=service_id, id=job_id).one()
|
||||
return Job.query.filter_by(service_id=service_id, id=job_id).first()
|
||||
|
||||
|
||||
def get_jobs_by_service(service_id):
|
||||
|
||||
@@ -8,17 +8,17 @@ from flask import (
|
||||
)
|
||||
|
||||
from app import api_user, encryption
|
||||
from app.aws_sqs import add_notification_to_queue
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
users_dao,
|
||||
services_dao,
|
||||
notifications_dao
|
||||
notifications_dao,
|
||||
jobs_dao
|
||||
)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
sms_template_notification_schema,
|
||||
notification_status_schema
|
||||
notification_status_schema,
|
||||
job_sms_template_notification_schema
|
||||
)
|
||||
from app.celery.tasks import send_sms, send_email
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
@@ -73,6 +73,44 @@ def create_sms_notification():
|
||||
return jsonify({'notification_id': notification_id}), 201
|
||||
|
||||
|
||||
@notifications.route('/sms/service/<service_id>', methods=['POST'])
|
||||
def create_sms_for_service(service_id):
|
||||
notification, errors = job_sms_template_notification_schema.load(request.get_json())
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=notification['template'],
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
if not template:
|
||||
return jsonify(result="error", message={'template': ['Template or service not found']}), 400
|
||||
|
||||
job_id = notification['job']
|
||||
|
||||
job = jobs_dao.get_job(service_id, job_id)
|
||||
|
||||
if not job:
|
||||
return jsonify(result="error", message={'job': ['Job not found']}), 400
|
||||
|
||||
service = services_dao.dao_fetch_service_by_id(service_id)
|
||||
|
||||
if service.restricted:
|
||||
if notification['to'] not in [user.email_address for user in service.users]:
|
||||
return jsonify(result="error", message={'to': ['Invalid phone number for restricted service']}), 400
|
||||
|
||||
notification_id = create_notification_id()
|
||||
|
||||
send_sms.apply_async((
|
||||
api_user['client'],
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
job_id),
|
||||
queue='sms')
|
||||
return jsonify({'notification_id': notification_id}), 201
|
||||
|
||||
|
||||
@notifications.route('/email', methods=['POST'])
|
||||
def create_email_notification():
|
||||
notification, errors = email_notification_schema.load(request.get_json())
|
||||
@@ -103,27 +141,3 @@ def create_email_notification():
|
||||
encryption.encrypt(notification)),
|
||||
queue='email')
|
||||
return jsonify({'notification_id': notification_id}), 201
|
||||
|
||||
|
||||
@notifications.route('/sms/service/<service_id>', methods=['POST'])
|
||||
def create_sms_for_service(service_id):
|
||||
resp_json = request.get_json()
|
||||
|
||||
notification, errors = sms_template_notification_schema.load(resp_json)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
template_id = notification['template']
|
||||
job_id = notification['job']
|
||||
|
||||
# TODO: job/job_id is in notification and can used to update job status
|
||||
|
||||
# TODO: remove once beta is reading notifications from the queue
|
||||
template = templates_dao.get_model_templates(template_id)
|
||||
|
||||
if template.service.id != uuid.UUID(service_id):
|
||||
message = "Invalid template: id {} for service id: {}".format(template.id, service_id)
|
||||
return jsonify(result="error", message=message), 400
|
||||
|
||||
notification_id = add_notification_to_queue(service_id, template_id, 'sms', notification)
|
||||
return jsonify({'notification_id': notification_id}), 201
|
||||
|
||||
@@ -108,6 +108,11 @@ class SmsTemplateNotificationSchema(SmsNotificationSchema):
|
||||
job = fields.String()
|
||||
|
||||
|
||||
class JobSmsTemplateNotificationSchema(SmsNotificationSchema):
|
||||
template = fields.Int(required=True)
|
||||
job = fields.String(required=True)
|
||||
|
||||
|
||||
class SmsAdminNotificationSchema(SmsNotificationSchema):
|
||||
content = fields.Str(required=True)
|
||||
|
||||
@@ -138,6 +143,7 @@ old_request_verify_code_schema = OldRequestVerifyCodeSchema()
|
||||
request_verify_code_schema = RequestVerifyCodeSchema()
|
||||
sms_admin_notification_schema = SmsAdminNotificationSchema()
|
||||
sms_template_notification_schema = SmsTemplateNotificationSchema()
|
||||
job_sms_template_notification_schema = JobSmsTemplateNotificationSchema()
|
||||
email_notification_schema = EmailNotificationSchema()
|
||||
notification_status_schema = NotificationStatusSchema()
|
||||
notifications_status_schema = NotificationStatusSchema(many=True)
|
||||
|
||||
Reference in New Issue
Block a user