"""Lambda set_carveouts 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 carveouts as carveouts_logic def handle( product_info: ProductInfo, correlation_id: str, identity_uuid: str ) -> ProductInfo: upc, product_id = product_info.product.upc, product_info.product.product_id if not upc or not product_id: raise ValueError("Product UPC and ID are required to fetch carveouts.") 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, }, ) logger.info( f"Setting carveouts has started for Product." ) carveouts, carveouts_has_changed = carveouts_logic.get_carveouts(product_info) if carveouts_has_changed: graphql.set_territory_carveouts(upc, product_id, carveouts) product_info.product.carveouts = carveouts logger.info( f"Setting carveouts has finished for Product.\n" f"carveouts_has_changed={carveouts_has_changed}\ncarveouts: {carveouts}\n" ) 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()