From 35b10f87327161dd577e059949675b59ca41d900 Mon Sep 17 00:00:00 2001 From: Aanshi Lahoti Date: Mon, 9 Dec 2024 12:33:38 +0530 Subject: [PATCH] chore: product array changes --- src/v0/destinations/topsort/config.js | 10 ++++---- .../topsort/data/TopsortItemConfig.json | 8 +++---- src/v0/destinations/topsort/transform.js | 24 +++++++------------ src/v0/destinations/topsort/utils.js | 12 ++-------- 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/v0/destinations/topsort/config.js b/src/v0/destinations/topsort/config.js index 9a2c75d9a9..d8810ebd04 100644 --- a/src/v0/destinations/topsort/config.js +++ b/src/v0/destinations/topsort/config.js @@ -12,12 +12,12 @@ const ConfigCategories = { }; const ECOMM_EVENTS_WITH_PRODUCT_ARRAY = [ - 'Product Clicked', - 'Product added', - 'Product Viewed', - 'Product Removed', + 'Cart Viewed', 'Checkout Started', - 'Payment Info Entered', + 'Order Updated', + 'Order Completed', + 'Order Refunded', + 'Order Cancelled', ]; const mappingConfig = getMappingConfig(ConfigCategories, __dirname); diff --git a/src/v0/destinations/topsort/data/TopsortItemConfig.json b/src/v0/destinations/topsort/data/TopsortItemConfig.json index e6c273740c..d0bc8896ea 100644 --- a/src/v0/destinations/topsort/data/TopsortItemConfig.json +++ b/src/v0/destinations/topsort/data/TopsortItemConfig.json @@ -1,12 +1,12 @@ [ { - "destKey": "properties.position", - "sourceKeys": "position", + "destKey": "position", + "sourceKeys": ["properties.position", "position"], "required": false }, { - "destKey": "properties.product_id", - "sourceKeys": "productId", + "destKey": "productId", + "sourceKeys": ["properties.product_id", "product_id"], "required": false } ] diff --git a/src/v0/destinations/topsort/transform.js b/src/v0/destinations/topsort/transform.js index 6b72b9fdd2..15f1b3b153 100644 --- a/src/v0/destinations/topsort/transform.js +++ b/src/v0/destinations/topsort/transform.js @@ -1,7 +1,7 @@ const { InstrumentationError, ConfigurationError, - getHashFromArrayWithDuplicate, + getHashFromArray, } = require('@rudderstack/integrations-lib'); const { ConfigCategory, mappingConfig, ECOMM_EVENTS_WITH_PRODUCT_ARRAY } = require('./config'); const { constructPayload, simpleProcessRouterDest } = require('../../util'); @@ -19,13 +19,12 @@ const processProductArray = ({ basePayload, placementPayload, topsortEvent, - apiKey, finalPayloads, }) => { const itemPayloads = constructItemPayloads(products, mappingConfig[ConfigCategory.ITEM.name]); itemPayloads.forEach((itemPayload) => { const eventData = createEventData(basePayload, placementPayload, itemPayload, topsortEvent); - addFinalPayload(eventData, apiKey, finalPayloads); + addFinalPayload(eventData, finalPayloads); }); }; @@ -35,7 +34,6 @@ const processSingleProduct = ({ placementPayload, message, topsortEvent, - apiKey, finalPayloads, messageId, }) => { @@ -46,15 +44,16 @@ const processSingleProduct = ({ eventData.data.id = messageId; // Add final payload with appropriate ID and other headers - addFinalPayload(eventData, apiKey, finalPayloads); + addFinalPayload(eventData, finalPayloads); }; const responseBuilder = (message, { Config }) => { - const { apiKey, topsortEvents } = Config; + const { topsortEvents } = Config; const { event, properties } = message; + const { products, messageId } = properties; // Parse Topsort event mappings - const parsedTopsortEventMappings = getHashFromArrayWithDuplicate(topsortEvents); + const parsedTopsortEventMappings = getHashFromArray(topsortEvents); const mappedEventName = getMappedEventName(parsedTopsortEventMappings, event); if (!mappedEventName) { @@ -67,9 +66,6 @@ const responseBuilder = (message, { Config }) => { const basePayload = constructPayload(message, mappingConfig[ConfigCategory.TRACK.name]); const placementPayload = constructPayload(message, mappingConfig[ConfigCategory.PLACEMENT.name]); - // Destructure products from properties (if available) - const { products, messageId } = properties; - // Check if the event involves a product array (using ECOMM_EVENTS_WITH_PRODUCT_ARRAY) const isProductArrayAvailable = ECOMM_EVENTS_WITH_PRODUCT_ARRAY.includes(event) && isProductArrayValid(event, properties); @@ -80,7 +76,6 @@ const responseBuilder = (message, { Config }) => { basePayload, placementPayload, topsortEventName, - apiKey, finalPayloads, }; @@ -111,15 +106,12 @@ const processEvent = (message, destination) => { const messageType = message.type.toLowerCase(); - let response; // Handle 'track' event type - if (messageType === 'track') { - response = responseBuilder(message, destination); // Call responseBuilder to handle the event - } else { + if (messageType !== 'track') { throw new InstrumentationError('Only "track" events are supported. Dropping event.', 400); } - return response; + return responseBuilder(message, destination); }; // Process function that is called per event diff --git a/src/v0/destinations/topsort/utils.js b/src/v0/destinations/topsort/utils.js index 255dcdf29c..1b03b8a044 100644 --- a/src/v0/destinations/topsort/utils.js +++ b/src/v0/destinations/topsort/utils.js @@ -1,6 +1,5 @@ const { generateUUID } = require('@rudderstack/integrations-lib'); const { constructPayload } = require('../../util'); -const { BASE_URL } = require('./config'); // Function to check if a product array is valid const isProductArrayValid = (event, properties) => @@ -24,15 +23,8 @@ const createEventData = (basePayload, placementPayload, itemPayload, event) => ( }); // Function to add the structured event data to the final payloads array -const addFinalPayload = (eventData, apiKey, finalPayloads) => { - finalPayloads.push({ - ...eventData, - endpoint: BASE_URL, // Set the destination API URL - headers: { - 'Content-Type': 'application/json', - api_key: apiKey, // Add the API key here for authentication - }, - }); +const addFinalPayload = (eventData, finalPayloads) => { + finalPayloads.push(JSON.stringify(eventData)); // Only push the eventData as JSON }; // Function to retrieve mapped event name from Topsort event mappings.