Files
notifications-api/app/service/utils.py

55 lines
1.6 KiB
Python
Raw Normal View History

import itertools
from notifications_utils.recipients import allowed_to_send_to
2021-03-10 13:55:06 +00:00
from app.dao.services_dao import dao_fetch_service_by_id
from app.models import (
2021-03-10 13:55:06 +00:00
KEY_TYPE_NORMAL,
KEY_TYPE_TEAM,
KEY_TYPE_TEST,
GuestListRecipientType,
ServiceGuestList,
2021-03-10 13:55:06 +00:00
)
def get_recipients_from_request(request_json, key, type):
return [(type, recipient) for recipient in request_json.get(key)]
def get_guest_list_objects(service_id, request_json):
return [
ServiceGuestList.from_string(service_id, type, recipient)
for type, recipient in (
get_recipients_from_request(request_json, "phone_numbers", GuestListRecipientType.MOBILE)
+ get_recipients_from_request(request_json, "email_addresses", GuestListRecipientType.EMAIL)
)
]
2023-08-29 14:54:30 -07:00
def service_allowed_to_send_to(
recipient, service, key_type, allow_guest_list_recipients=True
):
if key_type == KEY_TYPE_TEST:
return True
if key_type == KEY_TYPE_NORMAL and not service.restricted:
return True
# Revert back to the ORM model here so we can get some things which
# arent in the serialised model
service = dao_fetch_service_by_id(service.id)
team_members = itertools.chain.from_iterable(
[user.mobile_number, user.email_address] for user in service.users
)
guest_list_members = [
2023-08-29 14:54:30 -07:00
member.recipient for member in service.guest_list if allow_guest_list_recipients
]
2023-08-29 14:54:30 -07:00
if (key_type == KEY_TYPE_NORMAL and service.restricted) or (
key_type == KEY_TYPE_TEAM
):
return allowed_to_send_to(
2023-08-29 14:54:30 -07:00
recipient, itertools.chain(team_members, guest_list_members)
)