"""Approve Releases in Bulk via GraphQL.""" from collections import defaultdict import loguru # GraphQL from logic import graphql_logic from utils import snowflake_utils import config log = logger = loguru.logger def main(): """Attach Label Participants to Products via GraphQL.""" # if a testing config is passed. if config.DEBUG_PRODUCT_ID: product_list = [config.DEBUG_PRODUCT_ID] elif config.SNOWFLAKE_SOURCE_TABLE: product_list = snowflake_utils.get_products_list( table=config.SNOWFLAKE_SOURCE_TABLE) else: raise Exception('Must include SNOWFLAKE_SOURCE_TABLE in config.') logger.info(f'Going to fix {len(product_list)} UPCs: {product_list}') # Make sure that the track list is not empty if not len(product_list): raise Exception('Track list fetch query produced no rows.') participants_info = snowflake_utils.get_products_participants_info(products=product_list) # noqa product_participants_map = defaultdict(list) for row in participants_info: upc = row['upc'] product_participants_map[upc].append(row) # mimic participant_report from participants_info participant_report_entries = [] # r.upc, # lp.name as participant_name, # lp.uuid as label_participant_uuid, # r.release_id as product_id, # p.vendor_id, -- ORCHLABELID # p.subaccount_id, for row in participants_info: participant_report_entry = { "request": { "data": { "name": row['participant_name'], "spotifyId": None, "appleMusicId": None }, "vendorId": row['vendor_id'], "subaccountId": row['subaccount_id'] }, "response": { "data": { "createLabelParticipant": { "uuid": row['label_participant_uuid'], "name": row['participant_name'] } } } } participant_report_entries.append(participant_report_entry) participant_report = {"responses": participant_report_entries} # mimic product_participants from participants_info product_participants = [] for row in participants_info: result_row = {} result_row['digital_upc'] = row['upc'] result_row['orchlabelid'] = row['vendor_id'] result_row['subaccount_id'] = row['subaccount_id'] result_row['participant_name'] = row['participant_name'] product_participants.append(result_row) # mimic upcs_and_product_ids from participants_info upcs_and_product_ids = [] for row in participants_info: upcs_and_product_ids.append((row['upc'], row['product_id'])) # now we are ready to call graphql_logic.fix_product_participants graphql_logic.fix_product_participants( product_participants=product_participants, upcs_and_product_ids=upcs_and_product_ids, participant_report=participant_report, ) log.info('Finished fixing product participants.') if __name__ == '__main__': main()