"""Recipient handlers.""" from typing import List from flask.typing import ResponseReturnValue from flask_pydantic import validate from collaborator.api import app from collaborator.logic import recipient from collaborator.schemas import BaseSchema from collaborator.utils import handlers as utils @app.route("/recipient/", methods=["GET"]) @utils.fetch_authorized_resources def get_recipient(recipient_id: int, authorized_resources, user) -> ResponseReturnValue: """Get recipient from recipient table by id. Args: account (Account): Account which made the request. recipient_id (int): Primary key id of recipient table Returns: flask.Response """ recipient.check_can_access_recipient_data(authorized_resources, [recipient_id]) return recipient.get_by_id(recipient_id) class RecipientsDataloaderBody(BaseSchema): """Body parameters for POST /recipient/dataloader.""" recipient_ids: List[int] @app.route("/recipient/dataloader", methods=["POST"]) @validate() @utils.fetch_authorized_resources def get_recipients_dataloader( authorized_resources, user, body: RecipientsDataloaderBody ) -> ResponseReturnValue: """Get recipients (dataloader). Args: authorized_resources: List of resources to which the requestor has access user (User): User who made the request body (RecipientsDataloaderBody): Body parameters Returns: flask.Response """ recipient_ids = body.recipient_ids recipient.check_can_access_recipient_data(authorized_resources, recipient_ids) recipients_list = recipient.get_by_ids(recipient_ids) recipients_map = {recipient["id"]: recipient for recipient in recipients_list} message = [ {"data": recipients_map.get(recipient_id, None)} for recipient_id in recipient_ids ] return message