From 0d3c0426571f14e88b5b8a703065935713ce8198 Mon Sep 17 00:00:00 2001 From: Anant Jain <62471433+anantjain45823@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:43:11 +0530 Subject: [PATCH] fix: shopify: incorporate new shopify cart token format (#3626) * 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 --- src/util/prometheus.js | 2 +- src/util/redis/testData/shopify_source.json | 4 ++-- src/v0/sources/shopify/transform.js | 21 ++++++++++++++++++--- src/v0/sources/shopify/util.js | 10 ++++++---- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/util/prometheus.js b/src/util/prometheus.js index cddeb80f31..27747faf4b 100644 --- a/src/util/prometheus.js +++ b/src/util/prometheus.js @@ -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', diff --git a/src/util/redis/testData/shopify_source.json b/src/util/redis/testData/shopify_source.json index 2120475baf..9c09de4643 100644 --- a/src/util/redis/testData/shopify_source.json +++ b/src/util/redis/testData/shopify_source.json @@ -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"] }, @@ -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"] }, diff --git a/src/v0/sources/shopify/transform.js b/src/v0/sources/shopify/transform.js index bc2135d215..b55fc61327 100644 --- a/src/v0/sources/shopify/transform.js +++ b/src/v0/sources/shopify/transform.js @@ -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; } @@ -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', @@ -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', @@ -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', diff --git a/src/v0/sources/shopify/util.js b/src/v0/sources/shopify/util.js index 6aea0d19bd..6d13d13bdf 100644 --- a/src/v0/sources/shopify/util.js +++ b/src/v0/sources/shopify/util.js @@ -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', @@ -40,6 +40,7 @@ const getDataFromRedis = async (key, metricMetadata) => { stats.increment('shopify_redis_no_val', { writeKey: metricMetadata.writeKey, source: metricMetadata.source, + event, }); } return redisData; @@ -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; @@ -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)) {