"""This module contains the task_get_participant_id() function.""" import json from connectors.logging import log from utils.concurrency_utils import prepare_errors_dict from logic.graphql_logic import get_or_create_label_participant from config import REPORT_FAILED_PAYLOADS def task_get_participant_id( artist_name: str, vendor_id: int, subaccount_id: int, task_id: int = None) -> tuple: """Get or create all label participant id's for a list of artist names and a given vendor_id. Args: artist (dict): A dict of artist data. vendor_id (int): The vendor ID. subaccount_id (int): The subaccount ID. task_id (int, optional): The task ID if running in a multi-task environment. Defaults to None. Returns: tuple: (list, dict, dict): The artist data, the list of failed artists, and the cached participant ID's """ errors = {} error_dict = {} msg = f'Collecting participation ID for {artist_name} on {vendor_id}' msg = task_id and f'Task {task_id}: {msg}' or msg log.debug(msg) part_id, _, _, _, error_dict = get_or_create_label_participant( artist_name, vendor_id, subaccount_id, task_id=task_id) if not part_id: msg = 'Error fetching participant ID for artist ' f'{artist_name} on label {vendor_id}' if task_id: msg = f'Task {task_id}: {msg}' log.error(msg) error = { 'artist': artist_name, 'vendor_id': vendor_id, 'subaccount_id': subaccount_id, 'error': error_dict, 'message': msg, 'task_id': task_id, 'calling_func': 'task_get_participant_id()' } errors = prepare_errors_dict( errors, 'vendor', vendor_id, subaccount_id) errors['vendor'][vendor_id][subaccount_id].append(error) # If the payload is not empty, print it to the log if REPORT_FAILED_PAYLOADS and error_dict.get('payload'): # Notify user of offending payload msg = f'Offending payload:\n' \ f"{json.dumps(error_dict['payload'], indent=4)}" if task_id: msg = f'Task {task_id}: {msg}' log.error(msg) participant_result = {} else: participant_result = { 'name': artist_name, 'id': part_id } return participant_result, errors