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 dmp.artists.cache import ArtistCache, cached_artist_data from dmp.artists.dtos import Label from dmp.artists.repositories import ArtistAccountRepository from dmp.rosters.services import GlobalFanDataAccessService @dataclass class GetArtistAccountsWithFanDataRequest(AuthRequest): global_participant_id: str @dataclass class GetArtistAccountsWithFanDataResponse: has_fandata: bool has_global_fandata_list: bool accounts: list[Label] @singleton class GetArtistAccountsWithFanDataHandler: permission = Permission("roster", "view") def __init__( self, auth_service: AuthService, artist_cache: ArtistCache, artist_account_repository: ArtistAccountRepository, global_fandata_access_service: GlobalFanDataAccessService, ) -> None: self.auth_service = auth_service self.artist_cache = artist_cache self.artist_account_repository = artist_account_repository self.global_fandata_access_service = global_fandata_access_service def handle( self, request: GetArtistAccountsWithFanDataRequest ) -> GetArtistAccountsWithFanDataResponse: account_access = self.auth_service.authorize_for_permission( request.identity_id, permission=self.permission ) accounts = self._get_artist_accounts_with_fandata( request.global_participant_id, vendor_ids=account_access.vendor_ids, subaccount_ids=account_access.subaccount_ids, ) has_global_fandata_list = ( self.global_fandata_access_service.is_enabled_for_any_vendor( vendor_ids=account_access.vendor_ids ) ) # Check if there is a rep for any account has_rep_for_any_account = False if has_global_fandata_list: has_rep_for_any_account = ( self.global_fandata_access_service.has_access_for_artist_and_any_vendor( global_participant_id=request.global_participant_id, vendor_ids=account_access.vendor_ids, subaccount_ids=account_access.subaccount_ids, ) ) if not has_rep_for_any_account: has_global_fandata_list = False return GetArtistAccountsWithFanDataResponse( has_fandata=bool(accounts) or has_rep_for_any_account, has_global_fandata_list=has_global_fandata_list, accounts=accounts if not has_global_fandata_list else [], ) @cached_artist_data("accounts-with-fandata") def _get_artist_accounts_with_fandata( self, global_participant_id: str, vendor_ids: list[int], subaccount_ids: list[int], ) -> list[Label]: return self.artist_account_repository.find_accounts_having_fandata( global_participant_id, vendor_ids=vendor_ids, subaccount_ids=subaccount_ids, )