"""import-store-links-ext-ddex.""" import uuid from ddex_ingester_common.helpers.s3_ddex import load_ddex_json 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 config import graphql_gateway from constants import queries logger = logging_utils.get_logger(config.app_logger) def handler(event, context): """Lambda entry point.""" 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}') graphql_gateway.set_headers( { 'Orchard-User-Id': config.OA_USER, 'Correlation-Id': correlation_id, } ) parsed_ddex = S3Schema().load(load_ddex_json(event)) if not parsed_ddex.product.stores: return True product_id = context.product.product_id upc = str(context.product.upc) if not product_id: raise ValueError('Product ID has no value.') if not upc: raise ValueError('UPC has no value.') payload = parse_store_links( parsed_ddex.product.stores, product_id, upc, context.product.sale_start_date.strftime('%Y-%m-%d') ) try: graphql_gateway.execute( queries.import_store_links, {'data': {'storeLinks': payload}} ) except graphql.GraphQLError as err: raise Exception('Graphql error') from err except Exception as exp: raise Exception(f'Error importing store links.\n{exp}') from exp return True def construct_item(product_id, upc, store_link, store_id, sales_start_date): """Construct item for payload. Args: product_id (int): Product ID upc (str): UPC store_link (str): Store link store_id (int): Store ID sales_start_date (str): Sales start date Returns: dict """ return { 'productId': product_id, 'upc': upc, 'storeId': store_id, 'storeInternalId': store_link, 'status': 'live', 'salesStartDate': sales_start_date, 'storeInternalStatus': '', 'forcePolling': 0, 'receivedForPollingDate': 0, 'deliveryDate': 0, 'countryCodes': '', } def parse_store_links(store_links, product_id, upc, sale_start_date): """Parse store links. Example store links: [ { "store_id": "spotify", "link": "spotify:album:368TKDyiyzFTwCyHJ0uWqc", "delivery_date": "2020-12-03" }, { "store_id": "apple", "link": "186235393", "delivery_date": "2020-12-02" }, { "store_id": "deezer", "link": "www.deezer.com/album/186235392", "delivery_date": null }, { "store_id": "vevo", "link": null, "delivery_date": "2020-12-01" } ] Args: store_links (list): Store links data product_id (int): Product ID upc (str): UPC sale_start_date (str): Sale start date Returns: list """ items = [] for store in store_links: if store.link: store_id = config.STORES[store.store_id] store_link = str(store.link) if store_id == config.SPOTIFY_STORE_ID: store_link = store.link.split(':')[-1] elif store_id == config.DEEZER_STORE_ID: store_link = store.link.split('/')[-1] items.append( construct_item( product_id, upc, store_link, store_id, sale_start_date ) ) return items