"""Lambda set_project function module.""" from typing import Any from common.connectors.graphql import GraphQLException from common.schemas.ingestion import Event, ProductInfo from lambdacommon.common_config import logger from src import logic from src.connectors import graphql from src.exceptions import DuplicateProjectCodeException def handle( bulk_session_ingestion_id: str, product_info: ProductInfo, correlation_id: str, identity_uuid: str, ) -> ProductInfo: vendor_id = product_info.product.vendor_id subaccount_id = product_info.product.subaccount_id project = product_info.project graphql.init(correlation_id, identity_uuid) # project_ingestion yields a typed IngestionContext object with logic.project_ingestion( bulk_session_ingestion_id=bulk_session_ingestion_id, vendor_uuid=product_info.product.vendor_uuid, project_code=project.project_code, ) as ctx: check_project_result = graphql.check_for_project( project, vendor_id, subaccount_id ) if check_project_result: project.project_id = check_project_result.project_id logger.info( f"Project code '{project.project_code}' already exists, project_id={project.project_id}. Skipped." ) else: try: create_project_result = graphql.create_project( project, vendor_id, subaccount_id ) except GraphQLException as ex: # Retry if two product loops clashed when trying to create the same project if f"Project code '{project.project_code}' already exists" in str(ex): raise DuplicateProjectCodeException from ex raise project.project_id = create_project_result.project_id ctx.project_id = project.project_id logger.info(f"Project {project.project_id} was created.") product_info.project.bulk_session_ingestion_project_id = ( ctx.bulk_session_ingestion_project_id ) logger.info( f"Attached bulk_session_ingestion_project_id={ctx.bulk_session_ingestion_project_id} to product_info" ) 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.bulk_session_ingestion_id, event.product_info, correlation_id, identity_uuid, ) return event.model_dump()