Skip to content

Commit

Permalink
chore: build finalPayload
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Dec 11, 2024
1 parent 915bab9 commit 8b3887a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/v0/destinations/topsort/config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -26,6 +26,6 @@ const mappingConfig = getMappingConfig(ConfigCategory, __dirname);
module.exports = {
mappingConfig,
ConfigCategory,
BASE_URL,
ENDPOINT,
ECOMM_EVENTS_WITH_PRODUCT_ARRAY,
};
5 changes: 1 addition & 4 deletions src/v0/destinations/topsort/data/TopsortTrackConfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
[
{
"destKey": "occurredAt",
"sourceKeys": ["originalTimestamp", "timestamp"],
"metadata": {
"type": "timestamp"
},
"sourceKeys": ["timestamp", "originalTimestamp"],
"required": true
},
{
Expand Down
77 changes: 63 additions & 14 deletions src/v0/destinations/topsort/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -66,7 +71,15 @@ const responseBuilder = (message, { Config }) => {
return finalPayloads;

Check warning on line 71 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L71

Added line #L71 was not covered by tests
};

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);

Check warning on line 85 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L85

Added line #L85 was not covered by tests
Expand All @@ -82,17 +95,53 @@ const processEvent = (message, destination) => {
throw new InstrumentationError('Only "track" events are supported. Dropping event.', 400);

Check warning on line 95 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L95

Added line #L95 was not covered by tests
}

return responseBuilder(message, destination);
processTopsortEvents(message, destination, finalPayloads);

// prepare the finalPayload and then return the finalPyaload
const response = defaultRequestConfig();
const { apiKey } = destination.Config;

Check warning on line 102 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L101-L102

Added lines #L101 - L102 were not covered by tests

response.method = defaultPostRequestConfig.requestMethod;
response.body.JSON = finalPayloads;
response.headers = {

Check warning on line 106 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L104-L106

Added lines #L104 - L106 were not covered by tests
'content-type': JSON_MIME_TYPE,
api_key: apiKey,
};

response.endpoint = ENDPOINT;

Check warning on line 111 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L111

Added line #L111 was not covered by tests

return response;

Check warning on line 113 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L113

Added line #L113 was not covered by tests
};

// Process function that is called per event
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 = {

Check warning on line 122 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L122

Added line #L122 was not covered by tests
impressions: [],
clicks: [],
purchases: [],
};

const errors = [];
const successMetadatas = [];

Check warning on line 129 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L128-L129

Added lines #L128 - L129 were not covered by tests

inputs.forEach((input) => {
try {

Check warning on line 132 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L131-L132

Added lines #L131 - L132 were not covered by tests
// Process the event
processEvent(input.message, input.destination, finalPayloads);

Check warning on line 134 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L134

Added line #L134 was not covered by tests
// Add to successMetadatas array
successMetadatas.append(input.metadata);

Check warning on line 136 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L136

Added line #L136 was not covered by tests
} catch (error) {
// Handle error and store the error details
const err = handleRtTfSingleEventError(input, error, reqMetadata);
errors.append(err);

Check warning on line 140 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L139-L140

Added lines #L139 - L140 were not covered by tests
}
});

return finalPayloads;

Check warning on line 144 in src/v0/destinations/topsort/transform.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L144

Added line #L144 was not covered by tests
};

module.exports = { process, processRouterDest };
28 changes: 3 additions & 25 deletions src/v0/destinations/topsort/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,34 +175,12 @@ const processPurchaseEventUtility = {

// Function to process purchase events (either with a product array or single product)
processPurchaseEvent(args) {

Check warning on line 177 in src/v0/destinations/topsort/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L177

Added line #L177 was not covered by tests
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 {

Check warning on line 181 in src/v0/destinations/topsort/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L180-L181

Added lines #L180 - L181 were not covered by tests
// Process the event with a single product
this.processSingleProduct({
basePayload,
topsortEventName,
finalPayloads,
message,
purchasePayload,
});
this.processSingleProduct(args);

Check warning on line 183 in src/v0/destinations/topsort/utils.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L183

Added line #L183 was not covered by tests
}
},
};
Expand Down

0 comments on commit 8b3887a

Please sign in to comment.