"""set-marketing-highlights.""" import uuid from ddex_ingester_common.constants.country_codes import WORLDWIDE from ddex_ingester_common.helpers.metadata import get_country_id from ddex_ingester_common.helpers.s3_ddex import load_ddex_json from ddex_ingester_common.lambda_exceptions import MarketingHighlightsException from ddex_ingester_common.logging import utils as logging_utils from ddex_ingester_common.schemas.s3_schema import S3Schema from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema from lambdacommon.graphql import graphql import config from constants import queries logger = logging_utils.get_logger(config.app_logger) def handler(event, context): """Lambda entry point.""" parsed_ddex = S3Schema().load(load_ddex_json(event)) if not parsed_ddex.project.marketing_highlights: return 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 ) logger.info(f'Received productId: {context.product.product_id}') config.graphql_gateway.set_headers( { 'Orchard-User-Id': config.OA_USER, 'Correlation-Id': correlation_id, } ) upsert_global_marketing_highlights( parsed_ddex.project.marketing_highlights, context.project_id ) upsert_territory_marketing_highlights( parsed_ddex.project.marketing_highlights, context.project_id ) return StateMachineSchema().dump(context) def upsert_global_marketing_highlights(marketing_highlights, project_id): """Upsert global marketing highlights. Args: marketing_highlights (list): List of marketing highlights project_id (int): Project ID Returns: boolean """ global_marketing_highlight = None for marketing_highlight in marketing_highlights: if not marketing_highlight.territory_codes: continue if WORLDWIDE in marketing_highlight.territory_codes: global_marketing_highlight = marketing_highlight.highlight break if not global_marketing_highlight: logger.info( f'Global project marketing highlight not found for project ID: ' f'{project_id}' ) return False try: config.graphql_gateway.execute( queries.upsert_project_marketing_highlight, { 'data': { 'projectId': project_id, 'data': [ { 'marketingHighlight': global_marketing_highlight } ] } } ) return True except graphql.GraphQLError as err: raise MarketingHighlightsException('Graphql error') from err except Exception as exp: raise Exception( f'Error processing marketing highlights.\n{exp}') from exp def upsert_territory_marketing_highlights(marketing_highlights, project_id): """Upsert territory marketing highlights. Args: marketing_highlights (list): List of marketing highlights project_id (int): Project ID Returns: boolean """ territory_marketing_highlights = [] for marketing_highlight in marketing_highlights: if not marketing_highlight.territory_codes: continue if WORLDWIDE in marketing_highlight.territory_codes: continue for country_code in marketing_highlight.territory_codes: territory_marketing_highlights.append( { 'marketingHighlight': marketing_highlight.highlight, 'countryId': get_country_id(country_code) } ) if not territory_marketing_highlights: logger.info( f'Territory marketing highlight not found for project ID: ' f'{project_id}' ) return False try: config.graphql_gateway.execute( queries.upsert_project_marketing_highlight, { 'data': { 'projectId': project_id, 'data': territory_marketing_highlights } } ) return True except graphql.GraphQLError as err: raise MarketingHighlightsException('Graphql error') from err except Exception as exp: raise Exception( f'Error processing marketing highlights.\n{exp}') from exp