"""Account Catalog logic tier.""" from typing import List, Optional from collaborator.models.snowflake.product_persister import ProductPersister from collaborator.utils import api as api_utils from collaborator.utils.helpers import ( check_collaborators_authorization, check_vendors_authorization, ) def get_products( authorized_resources, vendor_id: Optional[int], collaborator_id: Optional[int], limit: Optional[int], offset: Optional[int], min_tracks_with_splits: Optional[int], max_tracks_with_splits: Optional[int], min_tracks_without_splits: Optional[int], max_tracks_without_splits: Optional[int], term: Optional[str], label_participant_uuids: Optional[List[str]], subaccount_id: Optional[int], deleted: Optional[bool], sort_key: Optional[str], sort_direction: Optional[str], ): """Get products for vendor. Args: authorized_resources (list): List of requester's authorized resources. vendor_id (int): ID of vendor to fetch products for. limit (int): Pagination limit. offset (int): Pagination offset. min_tracks_with_splits (Optional[int]): Minimum number of tracks with splits. max_tracks_with_splits (Optional[int]): Maximum number of tracks with splits. min_tracks_without_splits (Optional[int]): Minimum number of tracks without splits. max_tracks_without_splits (Optional[int]): Maximum number of tracks without splits. label_participant_uuids (Optional[List[str]]): UUIDs of label participant to filter by. subaccount_id (Optional[int]): ID of subaccount to filter by. Returns: response.Response: Paginated repsonse. """ if vendor_id: check_vendors_authorization(authorized_resources, [vendor_id]) if collaborator_id: check_collaborators_authorization(authorized_resources, [collaborator_id]) rows, total_results = ProductPersister.get_products( vendor_id=vendor_id, collaborator_id=collaborator_id, limit=limit, offset=offset, min_tracks_with_splits=min_tracks_with_splits, max_tracks_with_splits=max_tracks_with_splits, min_tracks_without_splits=min_tracks_without_splits, max_tracks_without_splits=max_tracks_without_splits, term=term, label_participant_uuids=label_participant_uuids, subaccount_id=subaccount_id, deleted=deleted, sort_key=sort_key, sort_direction=sort_direction, ) products = [{"product_id": row.product_id} for row in rows] return api_utils.create_paginated_response(products, total_results) def _format_product_counts(row): """Format product counts row. Args: row: Result of product counts query. Returns: dict: Formatted dict """ return { "product_id": row.product_id, "splits_count": row.splits_count, "tracks_count": row.tracks_count, "collaborators_count": row.collaborators_count, "tracks_with_splits_count": row.tracks_with_splits_count, } def get_counts_for_products(authorized_resources, product_ids: List[int]) -> list: """Get counts for products by ID. Args: authorized_resources (list): List of requester's authorized resources. product_ids (list[int]): List of product IDs. Returns: list: Dataloader response. """ rows = ProductPersister.get_counts_for_products(product_ids) vendor_ids = list(set(row.vendor_id for row in rows)) authorized_vendor_ids = check_vendors_authorization( authorized_resources, vendor_ids, throw_if_unauthorized=False ) authorized_results_by_id = { row.product_id: row for row in rows if row.vendor_id in authorized_vendor_ids } return [ { "data": ( _format_product_counts(authorized_results_by_id[product_id]) if product_id in authorized_results_by_id else None ) } for product_id in product_ids ]