mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
merge from main
This commit is contained in:
@@ -16,18 +16,13 @@ from app.dao.notifications_dao import (
|
||||
dao_create_notification,
|
||||
dao_delete_notifications_by_id,
|
||||
)
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_TEST,
|
||||
NOTIFICATION_CREATED,
|
||||
SMS_TYPE,
|
||||
Notification,
|
||||
)
|
||||
from app.enums import KeyType, NotificationStatus, NotificationType
|
||||
from app.models import Notification
|
||||
from app.v2.errors import BadRequestError
|
||||
|
||||
|
||||
def create_content_for_notification(template, personalisation):
|
||||
if template.template_type == EMAIL_TYPE:
|
||||
if template.template_type == NotificationType.EMAIL:
|
||||
template_object = PlainTextEmailTemplate(
|
||||
{
|
||||
"content": template.content,
|
||||
@@ -36,7 +31,7 @@ def create_content_for_notification(template, personalisation):
|
||||
},
|
||||
personalisation,
|
||||
)
|
||||
if template.template_type == SMS_TYPE:
|
||||
if template.template_type == NotificationType.SMS:
|
||||
template_object = SMSMessageTemplate(
|
||||
{
|
||||
"content": template.content,
|
||||
@@ -76,7 +71,7 @@ def persist_notification(
|
||||
notification_id=None,
|
||||
simulated=False,
|
||||
created_by_id=None,
|
||||
status=NOTIFICATION_CREATED,
|
||||
status=NotificationStatus.CREATED,
|
||||
reply_to_text=None,
|
||||
billable_units=None,
|
||||
document_download_count=None,
|
||||
@@ -111,7 +106,7 @@ def persist_notification(
|
||||
updated_at=updated_at,
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
formatted_recipient = validate_and_format_phone_number(
|
||||
recipient, international=True
|
||||
)
|
||||
@@ -120,10 +115,11 @@ def persist_notification(
|
||||
notification.international = recipient_info.international
|
||||
notification.phone_prefix = recipient_info.country_prefix
|
||||
notification.rate_multiplier = recipient_info.billable_units
|
||||
elif notification_type == EMAIL_TYPE:
|
||||
current_app.logger.info(f"Persisting notification with type: {EMAIL_TYPE}")
|
||||
# This is typically for something like inviting a user or the 90 day email check
|
||||
|
||||
elif notification_type == NotificationType.EMAIL:
|
||||
current_app.logger.info(
|
||||
f"Persisting notification with type: {NotificationType.EMAIL}"
|
||||
)
|
||||
redis_store.set(
|
||||
f"email-address-{notification.id}",
|
||||
format_email_address(notification.to),
|
||||
@@ -134,7 +130,7 @@ def persist_notification(
|
||||
if not simulated:
|
||||
current_app.logger.info("Firing dao_create_notification")
|
||||
dao_create_notification(notification)
|
||||
if key_type != KEY_TYPE_TEST and current_app.config["REDIS_ENABLED"]:
|
||||
if key_type != KeyType.TEST and current_app.config["REDIS_ENABLED"]:
|
||||
current_app.logger.info(
|
||||
"Redis enabled, querying cache key for service id: {}".format(
|
||||
service.id
|
||||
@@ -150,14 +146,14 @@ def persist_notification(
|
||||
def send_notification_to_queue_detached(
|
||||
key_type, notification_type, notification_id, queue=None
|
||||
):
|
||||
if key_type == KEY_TYPE_TEST:
|
||||
if key_type == KeyType.TEST:
|
||||
print("send_notification_to_queue_detached key is test key")
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
if not queue:
|
||||
queue = QueueNames.SEND_SMS
|
||||
deliver_task = provider_tasks.deliver_sms
|
||||
if notification_type == EMAIL_TYPE:
|
||||
if notification_type == NotificationType.EMAIL:
|
||||
if not queue:
|
||||
queue = QueueNames.SEND_EMAIL
|
||||
deliver_task = provider_tasks.deliver_email
|
||||
@@ -183,7 +179,7 @@ def send_notification_to_queue(notification, queue=None):
|
||||
|
||||
|
||||
def simulated_recipient(to_address, notification_type):
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
formatted_simulated_numbers = [
|
||||
validate_and_format_phone_number(number)
|
||||
for number in current_app.config["SIMULATED_SMS_NUMBERS"]
|
||||
|
||||
@@ -5,8 +5,9 @@ from app.celery import tasks
|
||||
from app.config import QueueNames
|
||||
from app.dao.inbound_sms_dao import dao_create_inbound_sms
|
||||
from app.dao.services_dao import dao_fetch_service_by_inbound_number
|
||||
from app.enums import ServicePermissionType
|
||||
from app.errors import InvalidRequest, register_errors
|
||||
from app.models import INBOUND_SMS_TYPE, SMS_TYPE, InboundSms
|
||||
from app.models import InboundSms
|
||||
from app.notifications.sns_handlers import sns_notification_handler
|
||||
|
||||
receive_notifications_blueprint = Blueprint("receive_notifications", __name__)
|
||||
@@ -125,4 +126,6 @@ def fetch_potential_service(inbound_number, provider_name):
|
||||
|
||||
def has_inbound_sms_permissions(permissions):
|
||||
str_permissions = [p.permission for p in permissions]
|
||||
return set([INBOUND_SMS_TYPE, SMS_TYPE]).issubset(set(str_permissions))
|
||||
return {ServicePermissionType.INBOUND_SMS, ServicePermissionType.SMS}.issubset(
|
||||
set(str_permissions)
|
||||
)
|
||||
|
||||
@@ -5,8 +5,8 @@ from app import api_user, authenticated_service
|
||||
from app.aws.s3 import get_personalisation_from_s3, get_phone_number_from_s3
|
||||
from app.config import QueueNames
|
||||
from app.dao import notifications_dao
|
||||
from app.enums import KeyType, NotificationType, TemplateProcessType
|
||||
from app.errors import InvalidRequest, register_errors
|
||||
from app.models import EMAIL_TYPE, KEY_TYPE_TEAM, PRIORITY, SMS_TYPE
|
||||
from app.notifications.process_notifications import (
|
||||
persist_notification,
|
||||
send_notification_to_queue,
|
||||
@@ -113,13 +113,13 @@ def get_all_notifications():
|
||||
|
||||
@notifications.route("/notifications/<string:notification_type>", methods=["POST"])
|
||||
def send_notification(notification_type):
|
||||
if notification_type not in [SMS_TYPE, EMAIL_TYPE]:
|
||||
msg = "{} notification type is not supported".format(notification_type)
|
||||
if notification_type not in {NotificationType.SMS, NotificationType.EMAIL}:
|
||||
msg = f"{notification_type} notification type is not supported"
|
||||
raise InvalidRequest(msg, 400)
|
||||
|
||||
notification_form = (
|
||||
sms_template_notification_schema
|
||||
if notification_type == SMS_TYPE
|
||||
if notification_type == NotificationType.SMS
|
||||
else email_notification_schema
|
||||
).load(request.get_json())
|
||||
|
||||
@@ -145,7 +145,7 @@ def send_notification(notification_type):
|
||||
status_code=400,
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
check_if_service_can_send_to_number(
|
||||
authenticated_service, notification_form["to"]
|
||||
)
|
||||
@@ -165,7 +165,11 @@ def send_notification(notification_type):
|
||||
reply_to_text=template.reply_to_text,
|
||||
)
|
||||
if not simulated:
|
||||
queue_name = QueueNames.PRIORITY if template.process_type == PRIORITY else None
|
||||
queue_name = (
|
||||
QueueNames.PRIORITY
|
||||
if template.process_type == TemplateProcessType.PRIORITY
|
||||
else None
|
||||
)
|
||||
send_notification_to_queue(notification=notification_model, queue=queue_name)
|
||||
|
||||
else:
|
||||
@@ -199,7 +203,7 @@ def get_notification_return_data(notification_id, notification, template):
|
||||
|
||||
def _service_allowed_to_send_to(notification, service):
|
||||
if not service_allowed_to_send_to(notification["to"], service, api_user.key_type):
|
||||
if api_user.key_type == KEY_TYPE_TEAM:
|
||||
if api_user.key_type == KeyType.TEAM:
|
||||
message = "Can’t send to this recipient using a team-only API key"
|
||||
else:
|
||||
message = (
|
||||
@@ -220,7 +224,7 @@ def create_template_object_for_notification(template, personalisation):
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
if (
|
||||
template_object.template_type == SMS_TYPE
|
||||
template_object.template_type == NotificationType.SMS
|
||||
and template_object.is_message_too_long()
|
||||
):
|
||||
message = "Content has a character count greater than the limit of {}".format(
|
||||
|
||||
@@ -15,14 +15,8 @@ from app import redis_store
|
||||
from app.dao.notifications_dao import dao_get_notification_count_for_service
|
||||
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
|
||||
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
INTERNATIONAL_SMS_TYPE,
|
||||
KEY_TYPE_TEAM,
|
||||
KEY_TYPE_TEST,
|
||||
SMS_TYPE,
|
||||
ServicePermission,
|
||||
)
|
||||
from app.enums import KeyType, NotificationType, ServicePermissionType, TemplateType
|
||||
from app.models import ServicePermission
|
||||
from app.notifications.process_notifications import create_content_for_notification
|
||||
from app.serialised_models import SerialisedTemplate
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
@@ -46,7 +40,7 @@ def check_service_over_api_rate_limit(service, api_key):
|
||||
|
||||
|
||||
def check_service_over_total_message_limit(key_type, service):
|
||||
if key_type == KEY_TYPE_TEST or not current_app.config["REDIS_ENABLED"]:
|
||||
if key_type == KeyType.TEST or not current_app.config["REDIS_ENABLED"]:
|
||||
return 0
|
||||
|
||||
cache_key = total_limit_cache_key(service.id)
|
||||
@@ -67,7 +61,7 @@ def check_service_over_total_message_limit(key_type, service):
|
||||
|
||||
|
||||
def check_application_over_retention_limit(key_type, service):
|
||||
if key_type == KEY_TYPE_TEST or not current_app.config["REDIS_ENABLED"]:
|
||||
if key_type == KeyType.TEST or not current_app.config["REDIS_ENABLED"]:
|
||||
return 0
|
||||
total_stats = dao_get_notification_count_for_service(service_id=service.id)
|
||||
|
||||
@@ -110,7 +104,7 @@ def service_can_send_to_recipient(
|
||||
if not service_allowed_to_send_to(
|
||||
send_to, service, key_type, allow_guest_list_recipients
|
||||
):
|
||||
if key_type == KEY_TYPE_TEAM:
|
||||
if key_type == KeyType.TEAM:
|
||||
message = "Can’t send to this recipient using a team-only API key"
|
||||
else:
|
||||
message = (
|
||||
@@ -151,13 +145,13 @@ def validate_and_format_recipient(
|
||||
send_to, key_type, service, allow_guest_list_recipients
|
||||
)
|
||||
|
||||
if notification_type == SMS_TYPE:
|
||||
if notification_type == NotificationType.SMS:
|
||||
international_phone_info = check_if_service_can_send_to_number(service, send_to)
|
||||
|
||||
return validate_and_format_phone_number(
|
||||
number=send_to, international=international_phone_info.international
|
||||
)
|
||||
elif notification_type == EMAIL_TYPE:
|
||||
elif notification_type == NotificationType.EMAIL:
|
||||
return validate_and_format_email_address(email_address=send_to)
|
||||
|
||||
|
||||
@@ -171,7 +165,7 @@ def check_if_service_can_send_to_number(service, number):
|
||||
|
||||
if (
|
||||
international_phone_info.international
|
||||
and INTERNATIONAL_SMS_TYPE not in permissions
|
||||
and ServicePermissionType.INTERNATIONAL_SMS not in permissions
|
||||
):
|
||||
raise BadRequestError(message="Cannot send to international mobile numbers")
|
||||
else:
|
||||
@@ -181,12 +175,12 @@ def check_if_service_can_send_to_number(service, number):
|
||||
def check_is_message_too_long(template_with_content):
|
||||
if template_with_content.is_message_too_long():
|
||||
message = "Your message is too long. "
|
||||
if template_with_content.template_type == SMS_TYPE:
|
||||
if template_with_content.template_type == TemplateType.SMS:
|
||||
message += (
|
||||
f"Text messages cannot be longer than {SMS_CHAR_COUNT_LIMIT} characters. "
|
||||
f"Your message is {template_with_content.content_count_without_prefix} characters long."
|
||||
)
|
||||
elif template_with_content.template_type == EMAIL_TYPE:
|
||||
elif template_with_content.template_type == TemplateType.EMAIL:
|
||||
message += (
|
||||
f"Emails cannot be longer than 2000000 bytes. "
|
||||
f"Your message is {template_with_content.content_size_in_bytes} bytes."
|
||||
@@ -226,9 +220,9 @@ def validate_template(
|
||||
|
||||
|
||||
def check_reply_to(service_id, reply_to_id, type_):
|
||||
if type_ == EMAIL_TYPE:
|
||||
if type_ == NotificationType.EMAIL:
|
||||
return check_service_email_reply_to_id(service_id, reply_to_id, type_)
|
||||
elif type_ == SMS_TYPE:
|
||||
elif type_ == NotificationType.SMS:
|
||||
return check_service_sms_sender_id(service_id, reply_to_id, type_)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user