Skip to content

Commit

Permalink
chore: product array changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Dec 9, 2024
1 parent ae18c9a commit 35b10f8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
10 changes: 5 additions & 5 deletions src/v0/destinations/topsort/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/v0/destinations/topsort/data/TopsortItemConfig.json
Original file line number Diff line number Diff line change
@@ -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
}
]
24 changes: 8 additions & 16 deletions src/v0/destinations/topsort/transform.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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);

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

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/transform.js#L23-L27

Added lines #L23 - L27 were not covered by tests
});
};

Expand All @@ -35,7 +34,6 @@ const processSingleProduct = ({
placementPayload,
message,
topsortEvent,
apiKey,
finalPayloads,
messageId,
}) => {
Expand All @@ -46,15 +44,16 @@ const processSingleProduct = ({
eventData.data.id = messageId;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L44 was not covered by tests

// Add final payload with appropriate ID and other headers
addFinalPayload(eventData, apiKey, finalPayloads);
addFinalPayload(eventData, finalPayloads);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L47 was not covered by tests
};

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) {
Expand All @@ -67,9 +66,6 @@ const responseBuilder = (message, { Config }) => {
const basePayload = constructPayload(message, mappingConfig[ConfigCategory.TRACK.name]);
const placementPayload = constructPayload(message, mappingConfig[ConfigCategory.PLACEMENT.name]);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L67 was not covered by tests

// 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);
Expand All @@ -80,7 +76,6 @@ const responseBuilder = (message, { Config }) => {
basePayload,
placementPayload,
topsortEventName,
apiKey,
finalPayloads,
};

Expand Down Expand Up @@ -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);

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;
return responseBuilder(message, destination);
};

// Process function that is called per event
Expand Down
12 changes: 2 additions & 10 deletions src/v0/destinations/topsort/utils.js
Original file line number Diff line number Diff line change
@@ -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) =>
Expand All @@ -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

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L27 was not covered by tests
};

// Function to retrieve mapped event name from Topsort event mappings.
Expand Down

0 comments on commit 35b10f8

Please sign in to comment.