From 1c77b6e8b206168bffc94685238f853ac9a36b21 Mon Sep 17 00:00:00 2001 From: Anant Jain Date: Wed, 12 Jun 2024 23:22:23 +0530 Subject: [PATCH] chore: add aliases from one signal intg object --- src/v0/destinations/one_signal/transformV2.js | 24 +++++++++++++++---- src/v0/destinations/one_signal/util.js | 11 ++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/v0/destinations/one_signal/transformV2.js b/src/v0/destinations/one_signal/transformV2.js index 747ce9f1da..cc8079c71a 100644 --- a/src/v0/destinations/one_signal/transformV2.js +++ b/src/v0/destinations/one_signal/transformV2.js @@ -13,7 +13,12 @@ const { defaultPostRequestConfig, removeUndefinedAndNullValues, } = require('../../util'); -const { populateTags, getProductPurchasesDetails, getSubscriptions } = require('./util'); +const { + populateTags, + getProductPurchasesDetails, + getSubscriptions, + getOneSignalAliases, +} = require('./util'); const { JSON_MIME_TYPE } = require('../../util/constant'); const responseBuilder = (payload, Config) => { @@ -46,7 +51,13 @@ const identifyResponseBuilder = (message, { Config }) => { const tags = populateTags(message); const payload = constructPayload(message, mappingConfig[ConfigCategory.IDENTIFY_V2.name]); - + if (!payload?.identity?.external_id) { + const alias = getOneSignalAliases(message); + if (!alias.identifier) { + throw new InstrumentationError('userId or any other alias is required for identify'); + } + payload.identity = alias; + } // Following check is to intialise properties object in case we don't get properties object from construct payload if (!payload.properties) { payload.properties = {}; @@ -87,9 +98,14 @@ const trackOrGroupResponseBuilder = (message, { Config }, msgtype) => { const externalUserId = getFieldValueFromMessage(message, 'userIdOnly'); if (!externalUserId) { - throw new InstrumentationError('userId is required for track events/updating a device'); + const alias = getOneSignalAliases(message); + if (!alias.identifier) { + throw new InstrumentationError('userId or any other alias is required for track and group'); + } + payload.identity = alias; + } else { + payload.identity.external_id = externalUserId; } - /* Populating event as true in tags. eg. tags: { "event_name": true diff --git a/src/v0/destinations/one_signal/util.js b/src/v0/destinations/one_signal/util.js index 263e21eba9..93338406bf 100644 --- a/src/v0/destinations/one_signal/util.js +++ b/src/v0/destinations/one_signal/util.js @@ -192,10 +192,19 @@ const getSubscriptions = (message, Config) => { } return subscriptions.length > 0 ? subscriptions : undefined; }; - +const getOneSignalAliases = (message) => { + const integrationsObj = getIntegrationsObj(message, 'one_signal'); + const alias = {}; + if (integrationsObj && integrationsObj.aliasName && integrationsObj.aliasIdentifier) { + alias.type = integrationsObj.aliasName; + alias.identifier = integrationsObj.aliasIdentifier; + } + return alias; +}; module.exports = { populateDeviceType, populateTags, getProductPurchasesDetails, getSubscriptions, + getOneSignalAliases, };