"""Lambda set_participants function module.""" from typing import Any from common.connectors.graphql import obo_graphql as graphql from common.schemas.ingestion import Event, ProductInfo from lambdacommon.common_config import logger import config from src.logic import get_artists_info, process_artist from src.schemas import ArtistResult def handle( product_info: ProductInfo, correlation_id: str, identity_uuid: str ) -> ProductInfo: """Get combined artists info from product_info, get or create artist for each item, update products_info artists with 'artist_id' and 'label_participant_uuid' from the found / created instances. Returns updated object. """ product = product_info.product vendor_id, subaccount_id = product.vendor_id, product.subaccount_id logger.info( f"set_participants has started for Product, " f"vendor_id={vendor_id}, subaccount_id={subaccount_id}." ) graphql.init_graphql_client( environment=config.ENVIRONMENT, service_name=config.M2M_APPLICATION_NAME, graphql_service_name=config.GRAPHQL_SERVICE_NAME, identity_id=identity_uuid, profile_id=config.PROFILE_ID, profile_type=config.PROFILE_TYPE, headers={ "Correlation-Id": correlation_id, "Orchard-Roles": config.ROLE, }, ) # Get combined artists info artist_info_list = get_artists_info(product_info) logger.info( f"set_participants detected {len(artist_info_list)} artists " f"for Product.\n{artist_info_list}." ) # Get or create artists name_to_artist_result: dict[str, ArtistResult] = {} for artist_info in artist_info_list: artist_result = process_artist(artist_info, vendor_id, subaccount_id) name_to_artist_result[artist_info.name] = artist_result logger.info( f"set_participants has processed all the artists " f"for Product.\n{name_to_artist_result}." ) # Set found / created artists data to the product_info project_artist = product_info.project.artist if project_artist: project_artist.set_base_artist_ids( **name_to_artist_result[project_artist.name].model_dump() ) for product_artist in product.display_artists or []: product_artist.set_base_artist_ids( **name_to_artist_result[product_artist.name].model_dump() ) for track in product_info.tracks or []: for track_artist in track.display_artists or []: track_artist.set_base_artist_ids( **name_to_artist_result[track_artist.name].model_dump() ) return product_info def handler(event_data: dict[str, Any], context: Any) -> dict[str, Any]: """ Lambda entry point. Args: event_data: Lambda event payload (should look like event.shadow.json) context: Lambda context. """ event = Event(**event_data) correlation_id = event.correlation_id identity_uuid = event.identity_uuid event.product_info = handle(event.product_info, correlation_id, identity_uuid) return event.model_dump()