mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Refactored to make code clearer
This commit is contained in:
@@ -27,7 +27,8 @@ from app.notifications.process_notifications import (
|
||||
from app.notifications.validators import (
|
||||
check_template_is_for_notification_type,
|
||||
check_template_is_active,
|
||||
check_rate_limiting
|
||||
check_rate_limiting,
|
||||
service_has_permission,
|
||||
)
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
@@ -121,10 +122,19 @@ def send_notification(notification_type):
|
||||
|
||||
_service_allowed_to_send_to(notification_form, authenticated_service)
|
||||
if notification_type == SMS_TYPE:
|
||||
_service_has_permission(authenticated_service, SMS_TYPE)
|
||||
if service_has_permission(SMS_TYPE, authenticated_service.permissions) is False:
|
||||
raise InvalidRequest(
|
||||
{'to': ["Cannot send text messages"]},
|
||||
status_code=400
|
||||
)
|
||||
|
||||
_service_can_send_internationally(authenticated_service, notification_form['to'])
|
||||
elif notification_type == EMAIL_TYPE:
|
||||
_service_has_permission(authenticated_service, EMAIL_TYPE)
|
||||
if service_has_permission(EMAIL_TYPE, authenticated_service.permissions) is False:
|
||||
raise InvalidRequest(
|
||||
{'to': ["Cannot send emails"]},
|
||||
status_code=400
|
||||
)
|
||||
|
||||
# Do not persist or send notification to the queue if it is a simulated recipient
|
||||
simulated = simulated_recipient(notification_form['to'], notification_type)
|
||||
@@ -167,18 +177,6 @@ def get_notification_return_data(notification_id, notification, template):
|
||||
return output
|
||||
|
||||
|
||||
def _service_has_permission(service, notify_type):
|
||||
if notify_type not in [p.permission for p in service.permissions]:
|
||||
notify_type_text = notify_type + 's'
|
||||
if notify_type == SMS_TYPE:
|
||||
notify_type_text = 'text messages'
|
||||
|
||||
raise InvalidRequest(
|
||||
{'to': ["Cannot send {}".format(notify_type_text)]},
|
||||
status_code=400
|
||||
)
|
||||
|
||||
|
||||
def _service_can_send_internationally(service, number):
|
||||
international_phone_info = get_international_phone_info(number)
|
||||
|
||||
|
||||
@@ -73,17 +73,8 @@ def service_can_send_to_recipient(send_to, key_type, service):
|
||||
raise BadRequestError(message=message)
|
||||
|
||||
|
||||
def service_has_permission(service, permission):
|
||||
if permission not in [p.permission for p in service.permissions]:
|
||||
action = 'send'
|
||||
permission_text = permission + 's'
|
||||
if permission == SMS_TYPE:
|
||||
permission_text = 'text messages'
|
||||
elif permission == SCHEDULE_NOTIFICATIONS:
|
||||
action = 'schedule'
|
||||
permission_text = "notifications (this feature is invite-only)"
|
||||
|
||||
raise BadRequestError(message="Cannot {} {}".format(action, permission_text))
|
||||
def service_has_permission(notify_type, permissions):
|
||||
return notify_type in [p.permission for p in permissions]
|
||||
|
||||
|
||||
def validate_and_format_recipient(send_to, key_type, service, notification_type):
|
||||
|
||||
Reference in New Issue
Block a user