from dataclasses import dataclass from anydi import singleton from fansifter_common.auth.account import Account from fansifter_common.auth.services import AuthService from fansifter_common.auth.types import Permission from dmp.rosters.repositories import ( ArtistRosterLocalRepRepository, ArtistRosterMainRepRepository, ) from dmp.rosters.services import GlobalFanDataAccessService @dataclass class GetLocalRepCountriesRequest: identity_id: str vendor_id: int subaccount_id: int global_participant_ids: list[str] @property def account(self) -> Account: return Account(vendor_id=self.vendor_id, subaccount_id=self.subaccount_id) @singleton class GetLocalRepCountriesHandler: permission = Permission("roster", "view") def __init__( self, auth_service: AuthService, main_rep_repository: ArtistRosterMainRepRepository, local_rep_repository: ArtistRosterLocalRepRepository, global_fandata_access_service: GlobalFanDataAccessService, ) -> None: self.auth_service = auth_service self.main_rep_repository = main_rep_repository self.local_rep_repository = local_rep_repository self.global_fandata_access_service = global_fandata_access_service def handle(self, request: GetLocalRepCountriesRequest) -> list[str]: self.auth_service.check_account_resource( request.identity_id, account=request.account, permission=self.permission, ) global_fandata_list_enabled = ( self.global_fandata_access_service.is_enabled_for_any_vendor( vendor_ids=[request.vendor_id], ) ) if not global_fandata_list_enabled: return [] local_reps = ( self.local_rep_repository.find_by_account_and_global_participant_ids( vendor_id=request.vendor_id, subaccount_id=request.subaccount_id, global_participant_ids=request.global_participant_ids, ) ) if len(request.global_participant_ids) == 1: return list({local_rep.country_code for local_rep in local_reps}) exists_in_main_rep = ( self.main_rep_repository.exists_by_account_and_global_participant_ids( vendor_id=request.vendor_id, subaccount_id=request.subaccount_id, global_participant_ids=request.global_participant_ids, ) ) if exists_in_main_rep: return [] return list({local_rep.country_code for local_rep in local_reps})