from anydi import singleton from fansifter_common.auth.account import Account from fansifter_common.auth.services import AuthService from fansifter_common.auth.types import JointVentureParticipant, Permission from dmp.artists.cache import ArtistCache, cached_artist_data from dmp.artists.dtos import ArtistAccess from dmp.artists.repositories import ArtistJointVentureRepository from dmp.rosters.exceptions import GlobalFanDataListNotAvailableError from dmp.rosters.services import GlobalFanDataAccessService @singleton class ArtistAccessService: def __init__( self, *, auth_service: AuthService, artist_cache: ArtistCache, artist_joint_venture_repository: ArtistJointVentureRepository, global_fandata_access_service: GlobalFanDataAccessService, ) -> None: self.auth_service = auth_service self.artist_cache = artist_cache self.artist_joint_venture_repository = artist_joint_venture_repository self.global_fandata_access_service = global_fandata_access_service def check_artist_access( self, identity_id: str, global_participant_id: str, account: Account | None, *, permission: Permission, ) -> ArtistAccess: if account: joint_ventures = self.get_joint_venture_participants(global_participant_id) self.auth_service.check_account_resource( identity_id, account=account, permission=permission, resource_id=global_participant_id, joint_ventures=joint_ventures, ) return ArtistAccess( vendor_id=account.vendor_id, subaccount_id=account.subaccount_id, is_global=False, ) account_access = self.auth_service.authorize_for_permission( identity_id, permission=permission, ) show_global_fandata_list = ( self.global_fandata_access_service.is_enabled_for_any_vendor( vendor_ids=account_access.vendor_ids ) ) if not show_global_fandata_list: raise GlobalFanDataListNotAvailableError( "Global fan data list is not available for this identity or vendor." ) # check if artist is in global fan data list if not self.global_fandata_access_service.has_access_for_artist_and_any_vendor( global_participant_id=global_participant_id, vendor_ids=account_access.vendor_ids, subaccount_ids=account_access.subaccount_ids, ): raise GlobalFanDataListNotAvailableError( "Global fan data list is not available for this artist." ) return ArtistAccess( vendor_id=None, subaccount_id=None, is_global=True, ) @cached_artist_data("joint-venture-participants") def get_joint_venture_participants( self, global_participant_id: str ) -> list[JointVentureParticipant]: return self.artist_joint_venture_repository.find_by_global_participant_id( global_participant_id=global_participant_id )