mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
Send Email via the API
- uses the new subject/email from fields present on the templates / service tables - brings the send email api call into line with the sms one. - same fields (to/template_id) - same rules regarding restricted services - wired in as a task into celery Requires - new celery queue - new env property (NOTIFY_EMAIL_DOMAIN)
This commit is contained in:
@@ -3,23 +3,30 @@ import uuid
|
||||
from flask import (
|
||||
Blueprint,
|
||||
jsonify,
|
||||
request
|
||||
request,
|
||||
current_app
|
||||
)
|
||||
|
||||
from app import api_user, encryption
|
||||
from app.aws_sqs import add_notification_to_queue
|
||||
from app.dao import (templates_dao, notifications_dao)
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
users_dao,
|
||||
services_dao,
|
||||
notifications_dao
|
||||
)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
sms_template_notification_schema,
|
||||
notification_status_schema
|
||||
)
|
||||
from app.celery.tasks import send_sms
|
||||
from app.celery.tasks import send_sms, send_email
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
notifications = Blueprint('notifications', __name__)
|
||||
|
||||
from app.errors import register_errors
|
||||
|
||||
register_errors(notifications)
|
||||
|
||||
|
||||
@@ -47,6 +54,12 @@ def create_sms_notification():
|
||||
except NoResultFound:
|
||||
return jsonify(result="error", message={'template': ['Template not found']}), 400
|
||||
|
||||
service = services_dao.dao_fetch_service_by_id(api_user['client'])
|
||||
|
||||
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((
|
||||
@@ -59,17 +72,38 @@ def create_sms_notification():
|
||||
|
||||
@notifications.route('/email', methods=['POST'])
|
||||
def create_email_notification():
|
||||
resp_json = request.get_json()
|
||||
notification, errors = email_notification_schema.load(resp_json)
|
||||
notification, errors = email_notification_schema.load(request.get_json())
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
notification_id = add_notification_to_queue(api_user['client'], "admin", 'email', notification)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=notification['template'],
|
||||
service_id=api_user['client']
|
||||
)
|
||||
|
||||
if not template:
|
||||
return jsonify(result="error", message={'template': ['Template not found']}), 400
|
||||
|
||||
service = services_dao.dao_fetch_service_by_id(api_user['client'])
|
||||
|
||||
if service.restricted:
|
||||
if notification['to'] not in [user.email_address for user in service.users]:
|
||||
return jsonify(result="error", message={'to': ['Email address not permitted for restricted service']}), 400
|
||||
|
||||
notification_id = create_notification_id()
|
||||
|
||||
send_email.apply_async((
|
||||
api_user['client'],
|
||||
notification_id,
|
||||
template.subject,
|
||||
"{}@{}".format(service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user