"""approve-product.""" import uuid import config from config import graphql_gateway from constants import graphql_queries from ddex_ingester_common.constants.status import IN_CONTENT from ddex_ingester_common.lambda_exceptions import ApproveProductGraphQLException from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.schemas.state_machine_schema import ( StateMachineSchema) from lambdacommon.graphql import graphql logger = logging_utils.get_logger(config.app_logger) def handler(event, context): """Approve product handler.""" logger.info( f'Triggered approve_product: {event}') context = StateMachineSchema().load(event) correlation_id = context.correlation_id or str(uuid.uuid4()) context.correlation_id = correlation_id logging_utils.update_logger_correlation_id(logger, correlation_id) logging_utils.update_logger_with_message_ids( logger, context.message_id, context.message_thread_id, context.execution_name ) graphql_gateway.set_headers( { 'Orchard-User-Id': config.OA_USER, 'Correlation-Id': correlation_id, } ) try: product_id = int(context.product.product_id) result = config.graphql_gateway.execute( graphql_queries.APPROVE_AUDIO_PRODUCT, {'productId': product_id} )['data']['approveProduct'] context.product.status = IN_CONTENT logger.info(f'Ran approveProduct for {context.product.upc} with ID' f' {product_id} and got the result: {result}') except graphql.GraphQLError as err: raise ApproveProductGraphQLException('Graphql error') from err return StateMachineSchema().dump(context)