Skip to content

Commit

Permalink
fix: shopify: incorporate new shopify cart token format (#3626)
Browse files Browse the repository at this point in the history
* fix: shopify: incorporate new shopify cart token format

* chore: update test cases

* chore: address comments

* chore: address comments+1

* fix: cartToken fetching from input event
  • Loading branch information
anantjain45823 authored Aug 5, 2024
1 parent 98e9b00 commit 0d3c042
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/util/prometheus.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ class Prometheus {
name: 'shopify_redis_no_val',
help: 'shopify_redis_no_val',
type: 'counter',
labelNames: ['writeKey', 'source'],
labelNames: ['writeKey', 'source', 'event'],
},
{
name: 'invalid_shopify_event',
Expand Down
4 changes: 2 additions & 2 deletions src/util/redis/testData/shopify_source.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"input": {
"event": "rudderSessionIdentifier",
"sessionId": "session_id",
"cartToken": "shopify_test_set_map_fail",
"cartToken": "shopify_test_set_map_fail?key=abcd13234",
"query_parameters": {
"writeKey": ["Shopify Write Key"]
},
Expand Down Expand Up @@ -1298,7 +1298,7 @@
"input": {
"event": "rudderIdentifier",
"anonymousId": "b9993cc5-9e60-4e69-be0e-4e38c228314b",
"cartToken": "shopify_test1",
"cartToken": "shopify_test1?key=abcd1234",
"query_parameters": {
"writeKey": ["Shopify Write Key"]
},
Expand Down
21 changes: 18 additions & 3 deletions src/v0/sources/shopify/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,13 @@ const processEvent = async (inputEvent, metricMetadata) => {
break;
case 'carts_update':
if (useRedisDatabase) {
redisData = await getDataFromRedis(event.id || event.token, metricMetadata);
const isValidEvent = await checkAndUpdateCartItems(inputEvent, redisData, metricMetadata);
redisData = await getDataFromRedis(event.id || event.token, metricMetadata, 'Cart Update');
const isValidEvent = await checkAndUpdateCartItems(
inputEvent,
redisData,
metricMetadata,
shopifyTopic,
);
if (!isValidEvent) {
return NO_OPERATION_SUCCESS;
}
Expand Down Expand Up @@ -208,11 +213,20 @@ const isIdentifierEvent = (event) =>
['rudderIdentifier', 'rudderSessionIdentifier'].includes(event?.event);
const processIdentifierEvent = async (event, metricMetadata) => {
if (useRedisDatabase) {
const cartToken =
typeof event.cartToken === 'string' ? event.cartToken.split('?')[0] : event.cartToken;
logger.info(`{{SHOPIFY::}} writeKey: ${metricMetadata.writeKey}, cartToken: ${cartToken}`, {
type: 'set',
source: metricMetadata.source,
writeKey: metricMetadata.writeKey,
});
let value;
let field;
if (event.event === 'rudderIdentifier') {
field = 'anonymousId';
// eslint-disable-next-line unicorn/consistent-destructuring
const lineItemshash = getHashLineItems(event.cart);
// eslint-disable-next-line unicorn/consistent-destructuring
value = ['anonymousId', event.anonymousId, 'itemsHash', lineItemshash];
stats.increment('shopify_redis_calls', {
type: 'set',
Expand All @@ -227,6 +241,7 @@ const processIdentifierEvent = async (event, metricMetadata) => {
*/
} else {
field = 'sessionId';
// eslint-disable-next-line unicorn/consistent-destructuring
value = ['sessionId', event.sessionId];
/* cart_token: {
anonymousId:'anon_id1',
Expand All @@ -242,7 +257,7 @@ const processIdentifierEvent = async (event, metricMetadata) => {
source: metricMetadata.source,
writeKey: metricMetadata.writeKey,
});
await RedisDB.setVal(`${event.cartToken}`, value);
await RedisDB.setVal(`${cartToken}`, value);
} catch (e) {
logger.debug(`{{SHOPIFY::}} cartToken map set call Failed due redis error ${e}`, {
type: 'set',
Expand Down
10 changes: 6 additions & 4 deletions src/v0/sources/shopify/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const {
} = require('./config');
const logger = require('../../../logger');

const getDataFromRedis = async (key, metricMetadata) => {
const getDataFromRedis = async (key, metricMetadata, event) => {
try {
stats.increment('shopify_redis_calls', {
type: 'get',
Expand All @@ -40,6 +40,7 @@ const getDataFromRedis = async (key, metricMetadata) => {
stats.increment('shopify_redis_no_val', {
writeKey: metricMetadata.writeKey,
source: metricMetadata.source,
event,
});
}
return redisData;
Expand Down Expand Up @@ -187,8 +188,9 @@ const getAnonymousIdAndSessionId = async (message, metricMetadata, redisData = n
}
if (useRedisDatabase) {
if (!isDefinedAndNotNull(redisData)) {
const { event } = message;
// eslint-disable-next-line no-param-reassign
redisData = await getDataFromRedis(cartToken, metricMetadata);
redisData = await getDataFromRedis(cartToken, metricMetadata, event);
}
anonymousId = redisData?.anonymousId;
sessionId = redisData?.sessionId;
Expand Down Expand Up @@ -243,11 +245,11 @@ const updateCartItemsInRedis = async (cartToken, newCartItemsHash, metricMetadat
* @param {*} metricMetadata
* @returns boolean
*/
const checkAndUpdateCartItems = async (inputEvent, redisData, metricMetadata) => {
const checkAndUpdateCartItems = async (inputEvent, redisData, metricMetadata, shopifyTopic) => {
const cartToken = inputEvent.token || inputEvent.id;
if (!isDefinedAndNotNull(redisData)) {
// eslint-disable-next-line no-param-reassign
redisData = await getDataFromRedis(cartToken, metricMetadata);
redisData = await getDataFromRedis(cartToken, metricMetadata, SHOPIFY_TRACK_MAP[shopifyTopic]);
}
const itemsHash = redisData?.itemsHash;
if (isDefinedAndNotNull(itemsHash)) {
Expand Down

0 comments on commit 0d3c042

Please sign in to comment.