"""Lambda function module for set_product_to_complete.""" from common.schemas.state_machine_schema import StateMachineSchema from lambdacommon.graphql import graphql import config from config import graphql_gateway from src.constants.queries import APPROVE_PRODUCT from src.exceptions import SetProductToCompleteException logger = config.app_logger def handler(event, context): """Set product to complete handler. Approves the product to set it to Complete status. Product ID is expected to be in the state machine context from the create_product step. """ logger.info(f'Triggered set_product_to_complete: {event}') sm_context = StateMachineSchema().load(event) correlation_id = sm_context.correlation_id graphql_gateway.set_headers( { 'Orchard-User-Id': config.OA_USER, 'Correlation-Id': correlation_id, } ) product_id = sm_context.product.product_id try: approve_product(product_id) except graphql.GraphQLError as err: message = str(err) logger.error(f'GraphQL error in set_product_to_complete: {message}') raise SetProductToCompleteException(message) from err return StateMachineSchema().dump(sm_context) def approve_product(product_id: int) -> dict: """Approve product to set complete state.""" logger.info(f'Approving product with product_id: {product_id}') result = graphql_gateway.execute( APPROVE_PRODUCT, {'productId': product_id} )['data']['approveProduct'] logger.info(f'Product approved for product_id: {product_id}') return result