From 8b3887aa3f22e4e5f2d0176f875a7d33cc72bd03 Mon Sep 17 00:00:00 2001 From: Aanshi Lahoti Date: Wed, 11 Dec 2024 14:15:50 +0530 Subject: [PATCH] chore: build finalPayload --- src/v0/destinations/topsort/config.js | 4 +- .../topsort/data/TopsortTrackConfig.json | 5 +- src/v0/destinations/topsort/transform.js | 77 +++++++++++++++---- src/v0/destinations/topsort/utils.js | 28 +------ 4 files changed, 69 insertions(+), 45 deletions(-) diff --git a/src/v0/destinations/topsort/config.js b/src/v0/destinations/topsort/config.js index 20becd6398..5d4060f526 100644 --- a/src/v0/destinations/topsort/config.js +++ b/src/v0/destinations/topsort/config.js @@ -1,6 +1,6 @@ const { getMappingConfig } = require('../../util'); -const BASE_URL = 'https://api.topsort.com/v2/events'; +const ENDPOINT = 'https://api.topsort.com/v2/events'; const ConfigCategory = { TRACK: { @@ -26,6 +26,6 @@ const mappingConfig = getMappingConfig(ConfigCategory, __dirname); module.exports = { mappingConfig, ConfigCategory, - BASE_URL, + ENDPOINT, ECOMM_EVENTS_WITH_PRODUCT_ARRAY, }; diff --git a/src/v0/destinations/topsort/data/TopsortTrackConfig.json b/src/v0/destinations/topsort/data/TopsortTrackConfig.json index f5762751ea..fcc9c57d1b 100644 --- a/src/v0/destinations/topsort/data/TopsortTrackConfig.json +++ b/src/v0/destinations/topsort/data/TopsortTrackConfig.json @@ -1,10 +1,7 @@ [ { "destKey": "occurredAt", - "sourceKeys": ["originalTimestamp", "timestamp"], - "metadata": { - "type": "timestamp" - }, + "sourceKeys": ["timestamp", "originalTimestamp"], "required": true }, { diff --git a/src/v0/destinations/topsort/transform.js b/src/v0/destinations/topsort/transform.js index cbbfa41157..b464839f7b 100644 --- a/src/v0/destinations/topsort/transform.js +++ b/src/v0/destinations/topsort/transform.js @@ -3,16 +3,27 @@ const { ConfigurationError, getHashFromArray, } = require('@rudderstack/integrations-lib'); -const { mappingConfig, ECOMM_EVENTS_WITH_PRODUCT_ARRAY, ConfigCategory } = require('./config'); -const { constructPayload, simpleProcessRouterDest } = require('../../util'); +const { + mappingConfig, + ECOMM_EVENTS_WITH_PRODUCT_ARRAY, + ConfigCategory, + ENDPOINT, +} = require('./config'); +const { + constructPayload, + handleRtTfSingleEventError, + defaultRequestConfig, + defaultPostRequestConfig, +} = require('../../util'); const { isProductArrayValid, getMappedEventName, processImpressionsAndClicksUtility, processPurchaseEventUtility, } = require('./utils'); +const { JSON_MIME_TYPE } = require('../../util/constant'); -const responseBuilder = (message, { Config }) => { +const processTopsortEvents = (message, { Config }, finalPayloads) => { const { topsortEvents } = Config; const { event, properties } = message; const { products } = properties; @@ -29,12 +40,6 @@ const responseBuilder = (message, { Config }) => { // Construct base and placement payloads const basePayload = constructPayload(message, mappingConfig[ConfigCategory.TRACK.name]); - const finalPayloads = { - impressions: [], - clicks: [], - purchases: [], - }; - const commonArgs = { basePayload, topsortEventName, @@ -66,7 +71,15 @@ const responseBuilder = (message, { Config }) => { return finalPayloads; }; -const processEvent = (message, destination) => { +const processEvent = ( + message, + destination, + finalPayloads = { + impressions: [], + clicks: [], + purchases: [], + }, +) => { // Check for missing API Key or missing Advertiser ID if (!destination.Config.apiKey) { throw new ConfigurationError('API Key is missing. Aborting message.', 400); @@ -82,7 +95,22 @@ const processEvent = (message, destination) => { throw new InstrumentationError('Only "track" events are supported. Dropping event.', 400); } - return responseBuilder(message, destination); + processTopsortEvents(message, destination, finalPayloads); + + // prepare the finalPayload and then return the finalPyaload + const response = defaultRequestConfig(); + const { apiKey } = destination.Config; + + response.method = defaultPostRequestConfig.requestMethod; + response.body.JSON = finalPayloads; + response.headers = { + 'content-type': JSON_MIME_TYPE, + api_key: apiKey, + }; + + response.endpoint = ENDPOINT; + + return response; }; // Process function that is called per event @@ -90,9 +118,30 @@ const process = (event) => processEvent(event.message, event.destination); // Router destination handler to process a batch of events const processRouterDest = async (inputs, reqMetadata) => { - // Process all events through the simpleProcessRouterDest utility - const respList = await simpleProcessRouterDest(inputs, process, reqMetadata); - return respList; + + const finalPayloads = { + impressions: [], + clicks: [], + purchases: [], + }; + + const errors = []; + const successMetadatas = []; + + inputs.forEach((input) => { + try { + // Process the event + processEvent(input.message, input.destination, finalPayloads); + // Add to successMetadatas array + successMetadatas.append(input.metadata); + } catch (error) { + // Handle error and store the error details + const err = handleRtTfSingleEventError(input, error, reqMetadata); + errors.append(err); + } + }); + + return finalPayloads; }; module.exports = { process, processRouterDest }; diff --git a/src/v0/destinations/topsort/utils.js b/src/v0/destinations/topsort/utils.js index f3d7855e35..c6fb5fc203 100644 --- a/src/v0/destinations/topsort/utils.js +++ b/src/v0/destinations/topsort/utils.js @@ -175,34 +175,12 @@ const processPurchaseEventUtility = { // Function to process purchase events (either with a product array or single product) processPurchaseEvent(args) { - const { - isProductArrayAvailable, - basePayload, - topsortEventName, - finalPayloads, - products, - message, - purchasePayload, - } = args; - - if (isProductArrayAvailable) { + if (args.isProductArrayAvailable) { // Process the event with multiple products (product array) - this.processProductArray({ - basePayload, - topsortEventName, - finalPayloads, - products, - purchasePayload, - }); + this.processProductArray(args); } else { // Process the event with a single product - this.processSingleProduct({ - basePayload, - topsortEventName, - finalPayloads, - message, - purchasePayload, - }); + this.processSingleProduct(args); } }, };