mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Remove all DAO get service calls, replaced with reference to new service object in context.
This commit is contained in:
@@ -5,20 +5,23 @@ from flask import (
|
||||
current_app
|
||||
)
|
||||
|
||||
from app import api_user
|
||||
from app import api_user, authenticated_service
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
services_dao,
|
||||
notifications_dao
|
||||
)
|
||||
from app.models import KEY_TYPE_TEAM, PRIORITY
|
||||
from app.models import SMS_TYPE
|
||||
from app.notifications.process_notifications import (persist_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient)
|
||||
from app.notifications.validators import (check_service_over_daily_message_limit,
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active, check_rate_limiting)
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
simulated_recipient
|
||||
)
|
||||
from app.notifications.validators import (
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active,
|
||||
check_rate_limiting
|
||||
)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
sms_template_notification_schema,
|
||||
@@ -45,9 +48,10 @@ register_errors(notifications)
|
||||
|
||||
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
|
||||
def get_notification_by_id(notification_id):
|
||||
notification = notifications_dao.get_notification_with_personalisation(str(api_user.service_id),
|
||||
notification_id,
|
||||
key_type=None)
|
||||
notification = notifications_dao.get_notification_with_personalisation(
|
||||
str(authenticated_service.id),
|
||||
notification_id,
|
||||
key_type=None)
|
||||
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
|
||||
|
||||
|
||||
@@ -60,7 +64,7 @@ def get_all_notifications():
|
||||
limit_days = data.get('limit_days')
|
||||
|
||||
pagination = notifications_dao.get_notifications_for_service(
|
||||
str(api_user.service_id),
|
||||
str(authenticated_service.id),
|
||||
personalisation=True,
|
||||
filter_dict=data,
|
||||
page=page,
|
||||
@@ -96,8 +100,6 @@ def send_notification(notification_type):
|
||||
if notification_type not in ['sms', 'email']:
|
||||
assert False
|
||||
|
||||
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
|
||||
|
||||
notification_form, errors = (
|
||||
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
||||
).load(request.get_json())
|
||||
@@ -105,27 +107,27 @@ def send_notification(notification_type):
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
check_rate_limiting(service, api_user)
|
||||
check_rate_limiting(authenticated_service, api_user)
|
||||
|
||||
template = templates_dao.dao_get_template_by_id_and_service_id(
|
||||
template_id=notification_form['template'],
|
||||
service_id=service.id)
|
||||
service_id=authenticated_service.id)
|
||||
|
||||
check_template_is_for_notification_type(notification_type, template.template_type)
|
||||
check_template_is_active(template)
|
||||
|
||||
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
|
||||
|
||||
_service_allowed_to_send_to(notification_form, service)
|
||||
_service_allowed_to_send_to(notification_form, authenticated_service)
|
||||
if notification_type == SMS_TYPE:
|
||||
_service_can_send_internationally(service, notification_form['to'])
|
||||
_service_can_send_internationally(authenticated_service, notification_form['to'])
|
||||
|
||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||
simulated = simulated_recipient(notification_form['to'], notification_type)
|
||||
notification_model = persist_notification(template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=request.get_json()['to'],
|
||||
service=service,
|
||||
service=authenticated_service,
|
||||
personalisation=notification_form.get('personalisation', None),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_user.id,
|
||||
@@ -134,7 +136,7 @@ def send_notification(notification_type):
|
||||
if not simulated:
|
||||
queue_name = 'priority' if template.process_type == PRIORITY else None
|
||||
send_notification_to_queue(notification=notification_model,
|
||||
research_mode=service.research_mode,
|
||||
research_mode=authenticated_service.research_mode,
|
||||
queue=queue_name)
|
||||
else:
|
||||
current_app.logger.info("POST simulated notification for id: {}".format(notification_model.id))
|
||||
|
||||
Reference in New Issue
Block a user