From 13fd1612d4ee27de8b1df093e1a3ff6b597226b8 Mon Sep 17 00:00:00 2001 From: Gauravudia Date: Thu, 1 Feb 2024 20:32:09 +0530 Subject: [PATCH] refactor: moved transform.js code logic to rtWorkflow --- .../the_trade_desk/rtWorkflow.yaml | 39 ++++++++++++++++-- .../destinations/the_trade_desk/transform.js | 41 ------------------- .../the_trade_desk/transformConversion.js | 3 ++ .../the_trade_desk/transformRecord.js | 4 ++ .../the_trade_desk/router/data.ts | 30 +++++++------- 5 files changed, 57 insertions(+), 60 deletions(-) delete mode 100644 src/cdk/v2/destinations/the_trade_desk/transform.js diff --git a/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml b/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml index b64c599363..faea953d51 100644 --- a/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml +++ b/src/cdk/v2/destinations/the_trade_desk/rtWorkflow.yaml @@ -1,6 +1,14 @@ bindings: - - name: processRouterDest - path: ./transform + - name: EventType + path: ../../../../constants + - name: processRecordInputs + path: ./transformRecord + - name: processConversionInputs + path: ./transformConversion + - name: handleRtTfSingleEventError + path: ../../../../v0/util/index + - name: InstrumentationError + path: '@rudderstack/integrations-lib' steps: - name: validateConfig @@ -11,7 +19,30 @@ steps: $.assertConfig(config.trackerId, "Tracking Tag ID is not present. Aborting") config.ttlInDays ? $.assertConfig(config.ttlInDays >=0 && config.ttlInDays <= 180, "TTL is out of range. Allowed values are 0 to 180 days") - - name: processRouterDest + - name: validateInput template: | $.assert(Array.isArray(^) && ^.length > 0, "Invalid event array") - $.processRouterDest(^) + + - name: processRecordEvents + template: | + $.processRecordInputs(^.{.message.type === $.EventType.RECORD}[], ^[0].destination) + + - name: processConversionEvents + template: | + $.processConversionInputs(^.{.message.type === $.EventType.TRACK}[]) + + - name: failOtherEvents + template: | + const otherEvents = ^.{.message.type !== $.EventType.TRACK && .message.type !== $.EventType.RECORD}[] + let failedEvents = otherEvents.map( + function(event) { + const error = new $.InstrumentationError("Event type " + event.message.type + " is not supported"); + $.handleRtTfSingleEventError(event, error, {}) + } + ) + + failedEvents ?? [] + + - name: finalPayload + template: | + [...$.outputs.processRecordEvents, ...$.outputs.processConversionEvents, ...$.outputs.failOtherEvents] diff --git a/src/cdk/v2/destinations/the_trade_desk/transform.js b/src/cdk/v2/destinations/the_trade_desk/transform.js deleted file mode 100644 index 6e4a3d8707..0000000000 --- a/src/cdk/v2/destinations/the_trade_desk/transform.js +++ /dev/null @@ -1,41 +0,0 @@ -const lodash = require('lodash'); -const { InstrumentationError } = require('@rudderstack/integrations-lib'); -const { processRecordInputs } = require('./transformRecord'); -const { processConversionInputs } = require('./transformConversion'); -const { handleRtTfSingleEventError } = require('../../../../v0/util'); -const { EventType } = require('../../../../constants'); - -const processRouterDest = async (inputs) => { - const respList = []; - const { destination } = inputs[0]; - const groupedInputs = lodash.groupBy(inputs, (input) => input.message.type); - - await Promise.all( - Object.keys(groupedInputs).map(async (type) => { - switch (type) { - case EventType.RECORD: { - const transformedRecordEvent = processRecordInputs(groupedInputs.record, destination); - respList.push(...transformedRecordEvent); - break; - } - - case EventType.TRACK: { - const transformedConversionEvent = await processConversionInputs(groupedInputs.track); - respList.push(...transformedConversionEvent); - break; - } - default: { - const error = new InstrumentationError(`Event type "${type}" is not supported`); - const errorResponses = groupedInputs[type].map((input) => - handleRtTfSingleEventError(input, error, {}), - ); - respList.push(...errorResponses); - } - } - }), - ); - - return respList; -}; - -module.exports = { processRouterDest }; diff --git a/src/cdk/v2/destinations/the_trade_desk/transformConversion.js b/src/cdk/v2/destinations/the_trade_desk/transformConversion.js index 1ca1cb5532..acf04d8215 100644 --- a/src/cdk/v2/destinations/the_trade_desk/transformConversion.js +++ b/src/cdk/v2/destinations/the_trade_desk/transformConversion.js @@ -83,6 +83,9 @@ const processEvent = (message, destination) => { const process = (event) => processEvent(event.message, event.destination); const processConversionInputs = async (inputs, reqMetadata) => { + if (!inputs || inputs.length === 0) { + return []; + } const respList = await simpleProcessRouterDest(inputs, process, reqMetadata); return respList; }; diff --git a/src/cdk/v2/destinations/the_trade_desk/transformRecord.js b/src/cdk/v2/destinations/the_trade_desk/transformRecord.js index d00bf4a674..ff55c615d5 100644 --- a/src/cdk/v2/destinations/the_trade_desk/transformRecord.js +++ b/src/cdk/v2/destinations/the_trade_desk/transformRecord.js @@ -46,6 +46,10 @@ const processRecordInputs = (inputs, destination) => { const successMetadata = []; const errorResponseList = []; + if (!inputs || inputs.length === 0) { + return []; + } + if (!Config.audienceId) { const segmentNameError = new ConfigurationError( 'Segment name/Audience ID is not present. Aborting', diff --git a/test/integrations/destinations/the_trade_desk/router/data.ts b/test/integrations/destinations/the_trade_desk/router/data.ts index faecca09ac..03f96bcf2d 100644 --- a/test/integrations/destinations/the_trade_desk/router/data.ts +++ b/test/integrations/destinations/the_trade_desk/router/data.ts @@ -1750,21 +1750,6 @@ export const data = [ statusCode: 200, destination: sampleDestination, }, - { - batched: false, - metadata: [{ jobId: 3 }], - statusCode: 400, - error: 'Event type "identify" is not supported', - statTags: { - errorCategory: 'dataValidation', - errorType: 'instrumentation', - destType: 'THE_TRADE_DESK', - module: 'destination', - implementation: 'cdkV2', - feature: 'router', - }, - destination: sampleDestination, - }, { batchedRequest: { version: '1', @@ -1803,6 +1788,21 @@ export const data = [ statusCode: 200, destination: sampleDestination, }, + { + batched: false, + metadata: [{ jobId: 3 }], + statusCode: 400, + error: 'Event type identify is not supported', + statTags: { + errorCategory: 'dataValidation', + errorType: 'instrumentation', + destType: 'THE_TRADE_DESK', + module: 'destination', + implementation: 'cdkV2', + feature: 'router', + }, + destination: sampleDestination, + }, ], }, },