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.adapters.db import ReportingDB from dmp.artists.dtos import CampaignType, Label from dmp.rosters.repositories import ArtistRosterMainRepRepository from dmp.rosters.services import GlobalFanDataAccessService @dataclass class GetArtistMainRepAccountsRequest(AuthRequest): global_participant_id: str campaign_type: CampaignType | None @singleton class GetArtistMainRepAccountsHandler: default_permission = Permission("roster", "view") def __init__( self, db: ReportingDB, auth_service: AuthService, artist_roster_main_rep_repository: ArtistRosterMainRepRepository, global_fandata_access_service: GlobalFanDataAccessService, ) -> None: self.db = db self.auth_service = auth_service self.artist_roster_main_rep_repository = artist_roster_main_rep_repository self.global_fandata_access_service = global_fandata_access_service def handle(self, request: GetArtistMainRepAccountsRequest) -> list[Label]: account_access = self.auth_service.authorize_for_permission( request.identity_id, permission=self._get_permission(request), ) global_vendor_ids = ( self.global_fandata_access_service.get_enabled_for_any_vendor( vendor_ids=account_access.vendor_ids, ) ) main_reps = ( self.artist_roster_main_rep_repository.find_by_artist_and_any_account( global_participant_id=request.global_participant_id, vendor_ids=global_vendor_ids, subaccount_ids=account_access.subaccount_ids, ) ) return [ Label( vendor_id=main_rep.vendor_id, subaccount_id=main_rep.subaccount_id, ) for main_rep in main_reps ] def _get_permission(self, request: GetArtistMainRepAccountsRequest) -> Permission: if request.campaign_type == "TEXT": return Permission("text_campaign", "create") elif request.campaign_type == "EMAIL": return Permission("email_campaign", "create") return self.default_permission