Skip to content

Commit

Permalink
feat: supporting device token type using integrations object
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Jul 30, 2024
1 parent 02f8dbd commit 319a193
Show file tree
Hide file tree
Showing 6 changed files with 444 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/constants/destinationCanonicalNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ const DestCanonicalNames = {
emarsys: ['EMARSYS', 'Emarsys', 'emarsys'],
wunderkind: ['wunderkind', 'Wunderkind', 'WUNDERKIND'],
cordial: ['cordial', 'Cordial', 'CORDIAL'],
clevertap: ['clevertap', 'Clevertap', 'CleverTap', 'CLEVERTAP'],
};

module.exports = { DestHandlerMap, DestCanonicalNames };
2 changes: 2 additions & 0 deletions src/v0/destinations/clevertap/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const CLEVERTAP_DEFAULT_EXCLUSION = [
'ts',
'overrideFields',
];
const ALLOWED_DEVICE_TOKEN_TYPES = ['chrome', 'fcm', 'gcm', 'apns', 'wns', 'mpns'];
// ref : https://developer.clevertap.com/docs/disassociate-api
const DEL_MAX_BATCH_SIZE = 100;

Expand All @@ -54,4 +55,5 @@ module.exports = {
CONFIG_CATEGORIES,
CLEVERTAP_DEFAULT_EXCLUSION,
DESTINATION: 'CLEVERTAP',
ALLOWED_DEVICE_TOKEN_TYPES,
};
5 changes: 3 additions & 2 deletions src/v0/destinations/clevertap/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const {
batchMultiplexedEvents,
getSuccessRespEvents,
} = require('../../util');
const { generateClevertapBatchedPayload } = require('./utils');
const { generateClevertapBatchedPayload, deduceTokenType } = require('./utils');

const { JSON_MIME_TYPE } = require('../../util/constant');

Expand Down Expand Up @@ -268,7 +268,8 @@ const responseBuilderSimple = (message, category, destination) => {
deviceToken &&
(deviceOS === 'android' || isAppleFamily(deviceOS))
) {
const tokenType = deviceOS === 'android' ? 'fcm' : 'apns';
// const tokenType = deviceOS === 'android' ? 'fcm' : 'apns';
const tokenType = deduceTokenType(message, deviceOS);
const payloadForDeviceToken = {
d: [
{
Expand Down
22 changes: 20 additions & 2 deletions src/v0/destinations/clevertap/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { defaultBatchRequestConfig } = require('../../util');
const { defaultBatchRequestConfig, getIntegrationsObj } = require('../../util');

const { getEndpoint } = require('./config');
const { getEndpoint, ALLOWED_DEVICE_TOKEN_TYPES } = require('./config');
const { JSON_MIME_TYPE } = require('../../util/constant');

function generateClevertapBatchedPayload(events, destination) {
Expand Down Expand Up @@ -28,7 +28,25 @@ function generateClevertapBatchedPayload(events, destination) {

return batchedRequest;
}
/**
* Deduces the device token type based on the integration object and device operating system.
* Ref : https://developer.clevertap.com/docs/upload-device-tokens-api
* @param {Object} message - The message object containing integration details.
* @param {string} deviceOS - The device operating system ('android' or 'ios').
* @returns {string} The deduced device token type ('fcm' for Android or 'apns' for iOS).
*/
const deduceTokenType = (message, deviceOS) => {
const integrationObj = getIntegrationsObj(message, 'clevertap');
if (
integrationObj?.deviceTokenType &&
ALLOWED_DEVICE_TOKEN_TYPES.includes(integrationObj?.deviceTokenType)
) {
return integrationObj?.deviceTokenType;
}
return deviceOS === 'android' ? 'fcm' : 'apns';
};

module.exports = {
generateClevertapBatchedPayload,
deduceTokenType,
};
87 changes: 87 additions & 0 deletions src/v0/destinations/clevertap/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { deduceTokenType } = require('./utils');

describe('deduceTokenType', () => {
// deduces token type from integration object when valid token type is present
it('should return the token type from integration object when it is valid', () => {
const message = {
integrations: {
clevertap: {
deviceTokenType: 'fcm',
},
},
};
const deviceOS = 'android';
const result = deduceTokenType(message, deviceOS);
expect(result).toBe('fcm');
});

// handles null or undefined message input gracefully
it('should return default token type based on deviceOS when message is null', () => {
const message = null;
const deviceOS = 'android';
const result = deduceTokenType(message, deviceOS);
expect(result).toBe('fcm');
});

// returns 'fcm' when deviceOS is 'android' and no valid token type is present
it("should return 'fcm' when deviceOS is 'android' and no valid token type is present", () => {
const message = { integrations: { clevertap: { deviceTokenType: null } } };
const result = deduceTokenType(message, 'android');
expect(result).toBe('fcm');
});

// returns 'apns' when deviceOS is not 'android' and no valid token type is present
it("should return 'apns' when deviceOS is not 'android' and no valid token type is present", () => {
const message = { integrations: { clevertap: { deviceTokenType: null } } };
const result = deduceTokenType(message, 'ios');
expect(result).toBe('apns');
});

// handles null or undefined deviceOS input gracefully
it('should return default token type when deviceOS is null', () => {
const message = {};
const deviceOS = null;
const result = deduceTokenType(message, deviceOS);
expect(result).toEqual('apns');
});

// handles empty integration object in message
it('should return default token type when integration object is empty', () => {
const message = { integrations: {} };
const deviceOS = 'ios';
const result = deduceTokenType(message, deviceOS);
expect(result).toEqual('apns');
});

// handles integration object with invalid token type
it('should return default token type when integration object has an invalid token type', () => {
const message = { integrations: { clevertap: { deviceTokenType: 'invalidType' } } };
const deviceOS = 'ios';
const result = deduceTokenType(message, deviceOS);
expect(result).toEqual('apns');
});

// handles integration object with no token type
it('should return default token type when integration object has no token type', () => {
const message = { integrations: { clevertap: {} } };
const deviceOS = 'android';
const result = deduceTokenType(message, deviceOS);
expect(result).toEqual('fcm');
});

// verifies integration object retrieval with 'clevertap' as destination name
it('should retrieve correct device token type for CleverTap destination', () => {
const message = { integrations: { clevertap: { deviceTokenType: 'fcm' } } };
const deviceOS = 'ios';
const result = deduceTokenType(message, deviceOS);
expect(result).toBe('fcm');
});

// checks for case sensitivity in deviceOS values
it('should handle case sensitivity in deviceOS values', () => {
const message = { integrations: { clevertap: { deviceTokenType: 'fcm' } } };
const deviceOS = 'Android';
const result = deduceTokenType(message, deviceOS);
expect(result).toBe('fcm');
});
});
Loading

0 comments on commit 319a193

Please sign in to comment.