"""Label participant handler.""" from switchboard_consumer.constants.exceptions import ( ParticipantHandlerGraphQLError) from switchboard_consumer.constants.label_participant import ( ROLE_MAPPING_OVERRIDE, SUPPORTED_ROLE_CATEGORIES ) from switchboard_consumer.utils.graphql import is_error class LabelParticipantHandler: """"Handle filtering and creating label participants for a track.""" def __init__(self, orchard_client, vendor_id, subaccount_id, correlation_id, logger): """Create a LabelParticipantHandler instance. Args: orchard_client: orchard graphql client vendor_id: target vendor id subaccount_id: target subaccount id correlation_id: correlation id for current execution logger: Logger instance """ self.orchard_client = orchard_client self.logger = logger self.correlation_id = correlation_id self.supported_roles = self.get_supported_roles() self.vendor_id = vendor_id # 0 because the personnel mutation always requires a subaccount value self.subaccount_id = subaccount_id or 0 def get_supported_roles(self): roles = {} categories = self.orchard_client.get_participation_role_categories( self.correlation_id ) if is_error(categories): errors = categories.get('errors') message = ('Error encountered fetching' ' participation role categories') self.logger.error(message, custom_fields={'raw_errors': errors}) raise ParticipantHandlerGraphQLError( message, errors) for category in categories: if category['name'] in SUPPORTED_ROLE_CATEGORIES: roles = { **roles, **{ role['ddexRoleName']: role['name'] for role in category['roles'] if role['ddexRoleName'] # Ignore null ddex role names } } # Apply role mapping override roles = { **roles, **ROLE_MAPPING_OVERRIDE } return roles def create_label_participant(self, contribution): """Create label participant""" role = self.supported_roles.get(contribution['role']) if role: self.logger.info('Creating label participant', custom_fields={ 'contribution': contribution, 'vendor_id': self.vendor_id, 'subaccount_id': self.subaccount_id }) response = self.orchard_client.create_label_participant( contribution['participant'], self.vendor_id, self.subaccount_id, self.correlation_id ) if is_error(response): errors = response.get('errors') message = ('Error encountered creating' ' label participant') self.logger.error(message, custom_fields={ 'raw_errors': errors, 'contribution': contribution}) raise ParticipantHandlerGraphQLError( message, errors) return { 'labelParticipantId': response['id'], 'participationRoleName': role } def create_label_participants(self, contributions): """Create label participants from track contributors""" label_participants = [] # Look for studio personnel in contributors for contribution in contributions: label_participant = self.create_label_participant(contribution) if label_participant: label_participants.append(label_participant) return label_participants def set_label_participants_for_track(self, isrc, label_participants): """Set label participants on track""" for index, label_participant in enumerate(label_participants): label_participant.update({'sequenceNumber': index + 1}) self.logger.info('Setting label participants onto track', custom_fields={ 'isrc': isrc, 'label_participants': label_participants }) response = \ self.orchard_client.set_label_sound_recording_participations( isrc, self.vendor_id, self.subaccount_id, label_participants, self.correlation_id ) if is_error(response): errors = response.get('errors') message = ('Error encountered setting' ' label participant onto track') self.logger.error(message, custom_fields={ 'raw_errors': errors, 'isrc': isrc, 'label_participants': label_participants}) raise ParticipantHandlerGraphQLError( message, errors)