"""Lambda function module.""" import uuid import config from ddex_ingester_common.constants.status import IN_CONTENT from ddex_ingester_common.constants.swb_deal_types import \ SME_ANALYTICS_NOT_FOR_DISTRIBUTION from ddex_ingester_common.lambda_exceptions import LambdaException from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.schemas.state_machine_schema import ( StateMachineSchema ) logger = logging_utils.get_logger(config.app_logger) def handler(event, context): """Process SME_ANALYTICS purged release.""" 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) logger.info(f'Triggered prep_sme_ddex: {event}') product = get_product_by_upc(context.product.upc) if product['deletions'] != 'N': logger.info(f'Product is already deleted. Product {product}.') return StateMachineSchema().dump(context) elif product['status'] != IN_CONTENT: raise LambdaException(f'It is only allowed to soft delete in_content ' f'products. Actual product: {product}') elif product['not_for_distribution'] != SME_ANALYTICS_NOT_FOR_DISTRIBUTION: raise LambdaException(f'It is only allowed to soft delete ' f'sme_analytics products.' f'Actual product: {product}') product_soft_delete(product['product_id']) return StateMachineSchema().dump(context) def get_product_by_upc(upc) -> dict: """Get product by upc.""" logger.info(f'Getting product by upc {upc}') get_product_by_upc_response = config. \ ows_client.get('ows-product', path=f'/product/upc/{upc}') if get_product_by_upc_response.status_code != 200: raise LambdaException(f'Unable get product by upc. ' f'Response : {get_product_by_upc_response}') return get_product_by_upc_response.json() def product_soft_delete(product_id): """Soft delete request by product_id.""" logger.info(f'Soft delete for product {product_id}') update_upc_response = config. \ ows_client.delete('ows-product-digital', path=f'/product/{product_id}/soft_delete') if update_upc_response.status_code != 200: raise LambdaException(f'Unable to soft delete product.' f'Response : {update_upc_response}')