2016-09-28 17:00:17 +01:00
|
|
|
|
import itertools
|
|
|
|
|
|
|
2024-03-13 16:20:39 -06:00
|
|
|
|
from flask import current_app
|
2017-11-28 10:35:16 +00:00
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.dao.services_dao import dao_fetch_service_by_id
|
2024-01-16 07:37:21 -05:00
|
|
|
|
from app.enums import KeyType, RecipientType
|
2024-01-18 10:28:50 -05:00
|
|
|
|
from app.models import ServiceGuestList
|
2024-05-16 10:17:45 -04:00
|
|
|
|
from notifications_utils.recipients import allowed_to_send_to
|
2020-06-26 14:10:12 +01:00
|
|
|
|
|
2016-09-28 10:16:10 +01:00
|
|
|
|
|
|
|
|
|
|
def get_recipients_from_request(request_json, key, type):
|
|
|
|
|
|
return [(type, recipient) for recipient in request_json.get(key)]
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-28 10:19:46 +01:00
|
|
|
|
def get_guest_list_objects(service_id, request_json):
|
2016-09-28 10:16:10 +01:00
|
|
|
|
return [
|
2020-07-28 10:22:13 +01:00
|
|
|
|
ServiceGuestList.from_string(service_id, type, recipient)
|
2016-09-28 10:16:10 +01:00
|
|
|
|
for type, recipient in (
|
2024-02-28 12:41:57 -05:00
|
|
|
|
get_recipients_from_request(
|
2024-01-12 17:27:31 -05:00
|
|
|
|
request_json, "phone_numbers", RecipientType.MOBILE
|
2024-02-28 12:41:57 -05:00
|
|
|
|
)
|
|
|
|
|
|
+ get_recipients_from_request(
|
2024-01-12 17:27:31 -05:00
|
|
|
|
request_json, "email_addresses", RecipientType.EMAIL
|
2024-02-28 12:41:57 -05:00
|
|
|
|
)
|
2016-09-28 10:16:10 +01:00
|
|
|
|
)
|
|
|
|
|
|
]
|
2016-09-28 17:00:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-29 14:54:30 -07:00
|
|
|
|
def service_allowed_to_send_to(
|
|
|
|
|
|
recipient, service, key_type, allow_guest_list_recipients=True
|
|
|
|
|
|
):
|
2024-01-18 10:28:50 -05:00
|
|
|
|
if key_type == KeyType.TEST:
|
2016-09-28 17:00:17 +01:00
|
|
|
|
return True
|
|
|
|
|
|
|
2024-01-18 10:28:50 -05:00
|
|
|
|
if key_type == KeyType.NORMAL and not service.restricted:
|
2016-09-28 17:00:17 +01:00
|
|
|
|
return True
|
|
|
|
|
|
|
2020-06-26 14:10:12 +01:00
|
|
|
|
# Revert back to the ORM model here so we can get some things which
|
|
|
|
|
|
# aren’t in the serialised model
|
|
|
|
|
|
service = dao_fetch_service_by_id(service.id)
|
|
|
|
|
|
|
2016-09-28 17:00:17 +01:00
|
|
|
|
team_members = itertools.chain.from_iterable(
|
2016-10-07 15:38:36 +01:00
|
|
|
|
[user.mobile_number, user.email_address] for user in service.users
|
|
|
|
|
|
)
|
2020-07-28 10:19:46 +01:00
|
|
|
|
guest_list_members = [
|
2023-08-29 14:54:30 -07:00
|
|
|
|
member.recipient for member in service.guest_list if allow_guest_list_recipients
|
2016-10-07 15:38:36 +01:00
|
|
|
|
]
|
2016-09-28 17:00:17 +01:00
|
|
|
|
|
2024-03-13 16:20:39 -06:00
|
|
|
|
# As per discussion we have decided to allow official simulated
|
|
|
|
|
|
# numbers to go out in trial mode for development purposes.
|
|
|
|
|
|
guest_list_members.extend(current_app.config["SIMULATED_SMS_NUMBERS"])
|
2024-01-18 10:28:50 -05:00
|
|
|
|
if (key_type == KeyType.NORMAL and service.restricted) or (
|
|
|
|
|
|
key_type == KeyType.TEAM
|
2016-10-07 15:38:36 +01:00
|
|
|
|
):
|
2016-09-28 17:00:17 +01:00
|
|
|
|
return allowed_to_send_to(
|
2023-08-29 14:54:30 -07:00
|
|
|
|
recipient, itertools.chain(team_members, guest_list_members)
|
2016-09-28 17:00:17 +01:00
|
|
|
|
)
|