from dataclasses import dataclass from anydi import singleton from fansifter_common.auth.requests import AuthRequest from fansifter_common.auth.services import AuthService from fansifter_common.auth.types import Permission from pydantic import BaseModel from dmp.shopify.dtos import ShopifyCollectionWithArtist from dmp.shopify.exceptions import ShopifyStoreNotYetProcessed from dmp.shopify.repositories import CollectionRepository from dmp.shopify.services import StoreAssociationService @dataclass class GetCollectionArtistAssociationsRequest(AuthRequest): association_id: str class GetCollectionArtistAssociationsResponse(BaseModel): vendor_id: int subaccount_id: int shop_domain: str whole_store_products_count: int | None = None whole_store_global_participant_id: str | None = None collections: list[ShopifyCollectionWithArtist] @singleton class GetCollectionArtistAssociationsHandler: permission = Permission("fan_data_channel", "view") def __init__( self, auth_service: AuthService, store_association_service: StoreAssociationService, collection_repository: CollectionRepository, ) -> None: self.auth_service = auth_service self.store_association_service = store_association_service self.collection_repository = collection_repository def handle( self, request: GetCollectionArtistAssociationsRequest ) -> GetCollectionArtistAssociationsResponse: store = self.store_association_service.get_store(request.association_id) self.auth_service.check_account_resource( request.identity_id, account=store.account, permission=self.permission, resource_id=store.id, ) if not store.is_processed: raise ShopifyStoreNotYetProcessed collections = self.collection_repository.find_by_association_id( association_id=request.association_id ) return GetCollectionArtistAssociationsResponse( vendor_id=store.vendor_id, subaccount_id=store.subaccount_id, shop_domain=store.shop_domain, whole_store_products_count=store.products_count, whole_store_global_participant_id=store.global_participant_id, collections=collections, )