Skip to content

Commit

Permalink
chore: purchase function updated
Browse files Browse the repository at this point in the history
  • Loading branch information
aanshi07 committed Dec 11, 2024
1 parent 11a7fc7 commit 915bab9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 43 deletions.
7 changes: 1 addition & 6 deletions src/v0/destinations/topsort/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,11 @@ const responseBuilder = (message, { Config }) => {
placementPayload, // Only pass placementPayload for impressions and clicks
});
} else if (topsortEventName === 'purchases') {
const purchasePayload = constructPayload(
message,
mappingConfig[ConfigCategory.PURCHASE_ITEM.name],
);
processPurchaseEventUtility.processPurchaseEvent({

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L59 was not covered by tests
...commonArgs,
purchasePayload, // Only pass purchasePayload for purchase events
});
} else {
throw new InstrumentationError(`Unknown event type: ${topsortEventName}`);
throw new InstrumentationError(`Event not mapped: ${topsortEventName}`);
}

return finalPayloads;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L66 was not covered by tests
Expand Down
63 changes: 26 additions & 37 deletions src/v0/destinations/topsort/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const isProductArrayValid = (event, properties) =>
Array.isArray(properties?.products) && properties?.products.length > 0;

// Function to construct item payloads for each product
const constructItemPayloads = (products, mappingConfigs) =>
const getItemPayloads = (products, mappingConfigs) =>
products.map((product) => constructPayload(product, mappingConfigs));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L11 was not covered by tests

// Function to add the structured event data to the final payloads array
Expand Down Expand Up @@ -70,7 +70,7 @@ const processImpressionsAndClicksUtility = {
topsortEventName,
finalPayloads,
}) {
const itemPayloads = constructItemPayloads(products, mappingConfig[ConfigCategory.ITEM.name]);
const itemPayloads = getItemPayloads(products, mappingConfig[ConfigCategory.ITEM.name]);
itemPayloads.forEach((itemPayload) => {
const eventData = this.createEventData(

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

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L72-L75

Added lines #L72 - L75 were not covered by tests
basePayload,
Expand Down Expand Up @@ -138,46 +138,33 @@ const processImpressionsAndClicksUtility = {

const processPurchaseEventUtility = {
// Create event data object for purchase events
createEventData(basePayload, purchasePayload, itemPayload, event) {
createEventData(basePayload, items, event) {
return {

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

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L141-L142

Added lines #L141 - L142 were not covered by tests
topsortPayload: {
...basePayload,
items: {
...purchasePayload,
...itemPayload,
},
items,
id: generateUUID(),
},
event,
};
},

// Function to process events with a product array for purchase events
processProductArray({ products, basePayload, purchasePayload, topsortEventName, finalPayloads }) {
const itemPayloads = constructItemPayloads(
processProductArray(args) {
const { products, basePayload, topsortEventName, finalPayloads } = args;
const itemPayloads = getItemPayloads(

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

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L153-L155

Added lines #L153 - L155 were not covered by tests
products,
mappingConfig[ConfigCategory.PURCHASE_ITEM.name],
);
itemPayloads.forEach((itemPayload) => {
const eventData = this.createEventData(
basePayload,
purchasePayload,
itemPayload,
topsortEventName,
);
addFinalPayload(eventData, finalPayloads);
});
const eventData = this.createEventData(basePayload, itemPayloads, topsortEventName);
addFinalPayload(eventData, finalPayloads);

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

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L159-L160

Added lines #L159 - L160 were not covered by tests
},

// Function to process events with a single product for purchase events
processSingleProduct({ basePayload, purchasePayload, message, topsortEventName, finalPayloads }) {
processSingleProduct(args) {
const { basePayload, message, topsortEventName, finalPayloads } = args;
const itemPayload = constructPayload(message, mappingConfig[ConfigCategory.PURCHASE_ITEM.name]);
const eventData = this.createEventData(
basePayload,
purchasePayload,
itemPayload,
topsortEventName,
);
const eventData = this.createEventData(basePayload, [itemPayload], topsortEventName);

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

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/topsort/utils.js#L164-L167

Added lines #L164 - L167 were not covered by tests

// Ensure messageId is used instead of generating a UUID for single product events
eventData.topsortPayload.id = message.messageId;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L170 was not covered by tests
Expand All @@ -187,17 +174,19 @@ const processPurchaseEventUtility = {
},

// Function to process purchase events (either with a product array or single product)
processPurchaseEvent({
isProductArrayAvailable,
basePayload,
topsortEventName,
finalPayloads,
products,
message,
purchasePayload,
}) {
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;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L186 was not covered by tests

if (isProductArrayAvailable) {
// If product array is available, process the purchase event with multiple products
// Process the event with multiple products (product array)
this.processProductArray({

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L190 was not covered by tests
basePayload,
topsortEventName,
Expand All @@ -206,7 +195,7 @@ const processPurchaseEventUtility = {
purchasePayload,
});
} else {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L197 was not covered by tests
// Otherwise, process the purchase event with a single product
// Process the event with a single product
this.processSingleProduct({

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L199 was not covered by tests
basePayload,
topsortEventName,
Expand All @@ -220,7 +209,7 @@ const processPurchaseEventUtility = {

module.exports = {
isProductArrayValid,
constructItemPayloads,
getItemPayloads,
addFinalPayload,
getMappedEventName,
processImpressionsAndClicksUtility,
Expand Down

0 comments on commit 915bab9

Please sign in to comment.