diff --git a/src/v0/destinations/airship/data/airshipTrackConfig.json b/src/v0/destinations/airship/data/airshipTrackConfig.json index 1f280f756b..4b09fdb943 100644 --- a/src/v0/destinations/airship/data/airshipTrackConfig.json +++ b/src/v0/destinations/airship/data/airshipTrackConfig.json @@ -22,7 +22,10 @@ { "destKey": "session_id", "sourceKeys": ["properties.sessionId", "context.sessionId"], - "required": false + "required": false, + "metadata": { + "type": "toString" + } }, { "destKey": "transaction", diff --git a/src/v0/destinations/airship/transform.js b/src/v0/destinations/airship/transform.js index dc8543fbc5..fcf18daa7e 100644 --- a/src/v0/destinations/airship/transform.js +++ b/src/v0/destinations/airship/transform.js @@ -22,12 +22,20 @@ const { extractCustomFields, isEmptyObject, simpleProcessRouterDest, + convertToUuid, } = require('../../util'); const { JSON_MIME_TYPE } = require('../../util/constant'); -const { transformSessionId } = require('./utils'); const DEFAULT_ACCEPT_HEADER = 'application/vnd.urbanairship+json; version=3'; +const transformSessionId = (rawSessionId) => { + try { + return convertToUuid(rawSessionId); + } catch (error) { + throw new InstrumentationError(`Failed to transform session ID: ${error.message}`); + } +}; + const identifyResponseBuilder = (message, { Config }) => { const tagPayload = constructPayload(message, identifyMapping); const { apiKey, dataCenter } = Config; @@ -129,6 +137,8 @@ const trackResponseBuilder = async (message, { Config }) => { name = name.toLowerCase(); const payload = constructPayload(message, trackMapping); + + // ref : https://docs.airship.com/api/ua/#operation-api-custom-events-post if (isDefinedAndNotNullAndNotEmpty(payload.session_id)) { payload.session_id = transformSessionId(payload.session_id); } diff --git a/src/v0/destinations/airship/utils.js b/src/v0/destinations/airship/utils.js deleted file mode 100644 index 0ef637245f..0000000000 --- a/src/v0/destinations/airship/utils.js +++ /dev/null @@ -1,12 +0,0 @@ -const { v5 } = require('uuid'); - -// ref : https://docs.airship.com/api/ua/#operation-api-custom-events-post -const transformSessionId = (rawSessionId) => { - const NAMESPACE = v5.DNS; - const uuidV5 = v5(rawSessionId, NAMESPACE); - return uuidV5; -}; - -module.exports = { - transformSessionId, -}; diff --git a/src/v0/util/index.js b/src/v0/util/index.js index 1676498fdb..ad08be448e 100644 --- a/src/v0/util/index.js +++ b/src/v0/util/index.js @@ -16,6 +16,7 @@ const uaParser = require('ua-parser-js'); const moment = require('moment-timezone'); const sha256 = require('sha256'); const crypto = require('crypto'); +const { v5 } = require('uuid'); const { InstrumentationError, BaseError, @@ -2330,6 +2331,29 @@ const isEventSentByVDMV1Flow = (event) => event?.message?.context?.mappedToDesti const isEventSentByVDMV2Flow = (event) => event?.connection?.config?.destination?.schemaVersion === VDM_V2_SCHEMA_VERSION; + +const convertToUuid = (input) => { + const NAMESPACE = v5.DNS; + + if (!isDefinedAndNotNull(input)) { + throw new InstrumentationError('Input is undefined or null.'); + } + + try { + // Stringify and trim the input + const trimmedInput = String(input).trim(); + + // Check for empty input after trimming + if (!trimmedInput) { + throw new InstrumentationError('Input is empty or invalid.'); + } + // Generate and return UUID + return v5(trimmedInput, NAMESPACE); + } catch (error) { + const errorMessage = `Failed to transform input to uuid: ${error.message}`; + throw new InstrumentationError(errorMessage); + } +}; // ======================================================================== // EXPORTS // ======================================================================== @@ -2456,4 +2480,5 @@ module.exports = { getRelativePathFromURL, removeEmptyKey, isAxiosError, + convertToUuid, }; diff --git a/src/v0/util/index.test.js b/src/v0/util/index.test.js index 0b05b6f2d6..cfdfefddee 100644 --- a/src/v0/util/index.test.js +++ b/src/v0/util/index.test.js @@ -2,6 +2,7 @@ const { InstrumentationError } = require('@rudderstack/integrations-lib'); const utilities = require('.'); const { getFuncTestData } = require('../../../test/testHelper'); const { FilteredEventsError } = require('./errorTypes'); +const { v5 } = require('uuid'); const { hasCircularReference, flattenJson, @@ -11,6 +12,7 @@ const { groupRouterTransformEvents, isAxiosError, removeHyphens, + convertToUuid, } = require('./index'); const exp = require('constants'); @@ -985,3 +987,65 @@ describe('removeHyphens', () => { }); }); }); + +describe('convertToUuid', () => { + const NAMESPACE = v5.DNS; + + test('should generate UUID for valid string input', () => { + const input = 'testInput'; + const expectedUuid = '7ba1e88f-acf9-5528-9c1c-0c897ed80e1e'; + const result = convertToUuid(input); + expect(result).toBe(expectedUuid); + }); + + test('should generate UUID for valid numeric input', () => { + const input = 123456; + const expectedUuid = 'a52b2702-9bcf-5701-852a-2f4edc640fe1'; + const result = convertToUuid(input); + expect(result).toBe(expectedUuid); + }); + + test('should trim spaces and generate UUID', () => { + const input = ' testInput '; + const expectedUuid = '7ba1e88f-acf9-5528-9c1c-0c897ed80e1e'; + const result = convertToUuid(input); + expect(result).toBe(expectedUuid); + }); + + test('should throw an error for empty input', () => { + const input = ''; + expect(() => convertToUuid(input)).toThrow(InstrumentationError); + expect(() => convertToUuid(input)).toThrow('Input is empty or invalid.'); + }); + + test('to throw an error for null input', () => { + const input = null; + expect(() => convertToUuid(input)).toThrow(InstrumentationError); + expect(() => convertToUuid(input)).toThrow('Input is undefined or null'); + }); + + test('to throw an error for undefined input', () => { + const input = undefined; + expect(() => convertToUuid(input)).toThrow(InstrumentationError); + expect(() => convertToUuid(input)).toThrow('Input is undefined or null'); + }); + + test('should throw an error for input that is whitespace only', () => { + const input = ' '; + expect(() => convertToUuid(input)).toThrow(InstrumentationError); + expect(() => convertToUuid(input)).toThrow('Input is empty or invalid.'); + }); + + test('should handle long string input gracefully', () => { + const input = 'a'.repeat(1000); + const expectedUuid = v5(input, NAMESPACE); + const result = convertToUuid(input); + expect(result).toBe(expectedUuid); + }); + + test('any invalid input if stringified does not throw error', () => { + const input = {}; + const result = convertToUuid(input); + expect(result).toBe('672ca00c-37f4-5d71-b8c3-6ae0848080ec'); + }); +}); diff --git a/test/integrations/destinations/airship/processor/data.ts b/test/integrations/destinations/airship/processor/data.ts index a72495d23d..3c6d827ce6 100644 --- a/test/integrations/destinations/airship/processor/data.ts +++ b/test/integrations/destinations/airship/processor/data.ts @@ -2296,7 +2296,7 @@ export const data = [ }, { name: 'airship', - description: 'Test 22 : session id gets converted to v5 uuid format', + description: 'Test 22 : session id from Web SDK gets converted to v5 uuid format', feature: 'processor', module: 'destination', version: 'v0', @@ -2381,6 +2381,267 @@ export const data = [ }, }, }, + { + name: 'airship', + description: 'Test 23 : session id from mobile SDK gets converted to v5 uuid format', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + channel: 'web', + context: { + app: { + build: '1.0.0', + name: 'RudderLabs JavaScript SDK', + namespace: 'com.rudderlabs.javascript', + version: '1.0.0', + }, + traits: { email: 'testone@gmail.com', firstName: 'test', lastName: 'one' }, + library: { name: 'RudderLabs JavaScript SDK', version: '1.0.0' }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', + locale: 'en-US', + ip: '0.0.0.0', + os: { name: '', version: '' }, + screen: { density: 2 }, + sessionId: 1731403898, + }, + type: 'track', + messageId: '84e26acc-56a5-4835-8233-591137fca468', + anonymousId: '123456', + event: 'Product Clicked', + userId: 'testuserId1', + properties: {}, + integrations: { All: true }, + }, + destination: { + Config: { + apiKey: 'dummyApiKey', + appKey: 'ffdf', + dataCenter: false, + }, + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://go.urbanairship.com/api/custom-events', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/vnd.urbanairship+json; version=3', + 'X-UA-Appkey': 'ffdf', + Authorization: 'Bearer dummyApiKey', + }, + params: {}, + body: { + JSON: { + user: { named_user_id: 'testuserId1' }, + body: { + name: 'product_clicked', + session_id: 'd5627eac-795d-5005-9bb4-2c7c0af6cab0', + }, + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: 'airship', + description: 'Test 24 : session id null gets ignored', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + channel: 'web', + context: { + app: { + build: '1.0.0', + name: 'RudderLabs JavaScript SDK', + namespace: 'com.rudderlabs.javascript', + version: '1.0.0', + }, + traits: { email: 'testone@gmail.com', firstName: 'test', lastName: 'one' }, + library: { name: 'RudderLabs JavaScript SDK', version: '1.0.0' }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', + locale: 'en-US', + ip: '0.0.0.0', + os: { name: '', version: '' }, + screen: { density: 2 }, + }, + type: 'track', + messageId: '84e26acc-56a5-4835-8233-591137fca468', + anonymousId: '123456', + event: 'Product Clicked', + userId: 'testuserId1', + properties: { + sessionId: null, + }, + integrations: { All: true }, + }, + destination: { + Config: { + apiKey: 'dummyApiKey', + appKey: 'ffdf', + dataCenter: false, + }, + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://go.urbanairship.com/api/custom-events', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/vnd.urbanairship+json; version=3', + 'X-UA-Appkey': 'ffdf', + Authorization: 'Bearer dummyApiKey', + }, + params: {}, + body: { + JSON: { + user: { named_user_id: 'testuserId1' }, + body: { + name: 'product_clicked', + }, + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, + { + name: 'airship', + description: 'Test 24 : session id undefined gets ignored', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + message: { + channel: 'web', + context: { + app: { + build: '1.0.0', + name: 'RudderLabs JavaScript SDK', + namespace: 'com.rudderlabs.javascript', + version: '1.0.0', + }, + traits: { email: 'testone@gmail.com', firstName: 'test', lastName: 'one' }, + library: { name: 'RudderLabs JavaScript SDK', version: '1.0.0' }, + userAgent: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36', + locale: 'en-US', + ip: '0.0.0.0', + os: { name: '', version: '' }, + screen: { density: 2 }, + }, + type: 'track', + messageId: '84e26acc-56a5-4835-8233-591137fca468', + anonymousId: '123456', + event: 'Product Clicked', + userId: 'testuserId1', + properties: { + sessionId: undefined, + }, + integrations: { All: true }, + }, + destination: { + Config: { + apiKey: 'dummyApiKey', + appKey: 'ffdf', + dataCenter: false, + }, + }, + }, + ], + method: 'POST', + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + method: 'POST', + endpoint: 'https://go.urbanairship.com/api/custom-events', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/vnd.urbanairship+json; version=3', + 'X-UA-Appkey': 'ffdf', + Authorization: 'Bearer dummyApiKey', + }, + params: {}, + body: { + JSON: { + user: { named_user_id: 'testuserId1' }, + body: { + name: 'product_clicked', + }, + }, + JSON_ARRAY: {}, + XML: {}, + FORM: {}, + }, + files: {}, + userId: '', + }, + statusCode: 200, + }, + ], + }, + }, + }, ].map((tc) => ({ ...tc, mockFns: (_) => {