diff --git a/src/cdk/v2/destinations/intercom/procWorkflow.yaml b/src/cdk/v2/destinations/intercom/procWorkflow.yaml index a96bb0aae0..10b3396759 100644 --- a/src/cdk/v2/destinations/intercom/procWorkflow.yaml +++ b/src/cdk/v2/destinations/intercom/procWorkflow.yaml @@ -35,6 +35,11 @@ steps: $.assert(messageType in {{$.EventType.([.IDENTIFY, .TRACK, .GROUP])}}, "message type " + messageType + " is not supported"); $.assertConfig(.destination.Config.apiKey, "Access Token is not present. Aborting"); + - name: apiVersion + template: | + const version = $.isDefinedAndNotNull(.destination.Config.apiVersion) ? .destination.Config.apiVersion : "1.4"; + version; + - name: rEtlPayload condition: .message.context.mappedToDestination === true template: | @@ -43,13 +48,13 @@ steps: payload; - name: searchContact - condition: ($.outputs.messageType === {{$.EventType.IDENTIFY}} || $.outputs.messageType === {{$.EventType.GROUP}}) && .destination.Config.apiVersion === "latest" + condition: ($.outputs.messageType === {{$.EventType.IDENTIFY}} || $.outputs.messageType === {{$.EventType.GROUP}}) && $.outputs.apiVersion !== "1.4" template: | const contactId = await $.searchContact(.message, .destination); contactId; - name: identifyTransformationForLatestVersion - condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && .destination.Config.apiVersion === "latest" && !.message.context.mappedToDestination + condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && $.outputs.apiVersion !== "1.4" && !.message.context.mappedToDestination template: | const payload = .message.({ external_id: {{{{$.getGenericPaths("userIdOnly")}}}}, @@ -66,7 +71,7 @@ steps: payload; - name: identifyPayloadForLatestVersion - condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && .destination.Config.apiVersion === "latest" + condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && $.outputs.apiVersion !== "1.4" template: | const payload = .message.context.mappedToDestination ? $.outputs.rEtlPayload : $.outputs.identifyTransformationForLatestVersion; payload.name = $.getName(.message); @@ -81,7 +86,7 @@ steps: $.context.payload = $.removeUndefinedAndNullValues($.context.payload); - name: identifyTransformationForOlderVersion - condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && .destination.Config.apiVersion === "1.4" && !.message.context.mappedToDestination + condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && $.outputs.apiVersion === "1.4" && !.message.context.mappedToDestination template: | const payload = .message.({ user_id: {{{{$.getGenericPaths("userIdOnly")}}}}, @@ -94,7 +99,7 @@ steps: payload; - name: identifyPayloadForOlderVersion - condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && .destination.Config.apiVersion === "1.4" + condition: $.outputs.messageType === {{$.EventType.IDENTIFY}} && $.outputs.apiVersion === "1.4" template: | let payload = .message.context.mappedToDestination ? $.outputs.rEtlPayload : $.outputs.identifyTransformationForOlderVersion; payload = { @@ -124,9 +129,9 @@ steps: email: {{{{$.getGenericPaths("email")}}}}, metadata: .properties }); - .destination.Config.apiVersion === "latest" ? payload.id = .message.properties.id || .message.traits.id; - .destination.Config.apiVersion === "latest" ? payload.created_at = $.toSeconds(timestamp); - .destination.Config.apiVersion === "1.4" ? payload.created = $.toSeconds(timestamp); + $.outputs.apiVersion !== "1.4" ? payload.id = .message.properties.id || .message.traits.id; + $.outputs.apiVersion !== "1.4" ? payload.created_at = $.toSeconds(timestamp); + $.outputs.apiVersion === "1.4" ? payload.created = $.toSeconds(timestamp); !(payload.user_id) && .destination.Config.sendAnonymousId ? payload.user_id = .message.anonymousId; payload; @@ -158,7 +163,7 @@ steps: payload; - name: groupPayloadForLatestVersion - condition: $.outputs.messageType === {{$.EventType.GROUP}} && .destination.Config.apiVersion === "latest" + condition: $.outputs.messageType === {{$.EventType.GROUP}} && $.outputs.apiVersion !== "1.4" steps: - name: validateMessageAndPreparePayload template: | @@ -187,7 +192,7 @@ steps: $.removeUndefinedAndNullValues($.context.payload); - name: groupPayloadForOlderVersion - condition: $.outputs.messageType === {{$.EventType.GROUP}} && .destination.Config.apiVersion === "1.4" + condition: $.outputs.messageType === {{$.EventType.GROUP}} && $.outputs.apiVersion === "1.4" template: | $.context.response = []; const response = $.defaultRequestConfig(); @@ -199,7 +204,7 @@ steps: payload.custom_attributes = $.filterCustomAttributes(payload, "company", .destination); response.body.JSON = $.removeUndefinedAndNullValues(payload); response.endpoint = $.getBaseEndpoint(.destination) + "/" + "companies"; - response.headers = $.getHeaders(.destination); + response.headers = $.getHeaders(.destination, $.outputs.apiVersion); response.method = "POST"; response.userId = .message.anonymousId; $.context.response.push(response); @@ -220,6 +225,6 @@ steps: response.body.JSON = $.context.payload; response.endpoint = $.context.endpoint; response.method = $.context.requestMethod; - response.headers = $.getHeaders(.destination); - .destination.Config.apiVersion === "1.4" && $.outputs.messageType !== {{$.EventType.GROUP}} ? response.userId = .message.anonymousId; + response.headers = $.getHeaders(.destination, $.outputs.apiVersion); + $.outputs.apiVersion === "1.4" && $.outputs.messageType !== {{$.EventType.GROUP}} ? response.userId = .message.anonymousId; response; diff --git a/src/cdk/v2/destinations/intercom/utils.js b/src/cdk/v2/destinations/intercom/utils.js index 126fa6588d..8a26a1762a 100644 --- a/src/cdk/v2/destinations/intercom/utils.js +++ b/src/cdk/v2/destinations/intercom/utils.js @@ -32,13 +32,14 @@ const { /** * Returns destination request headers * @param {*} destination + * @param {*} apiVersion * @returns */ -const getHeaders = (destination) => ({ +const getHeaders = (destination, apiVersion) => ({ 'Content-Type': JSON_MIME_TYPE, Authorization: `Bearer ${destination.Config.apiKey}`, Accept: JSON_MIME_TYPE, - 'Intercom-Version': destination.Config.apiVersion === '1.4' ? '1.4' : '2.10', + 'Intercom-Version': apiVersion === '1.4' ? '1.4' : '2.10', }); /** @@ -47,7 +48,9 @@ const getHeaders = (destination) => ({ * @returns */ const getBaseEndpoint = (destination) => { - const { apiServer, apiVersion } = destination.Config; + const { apiServer } = destination.Config; + let { apiVersion } = destination.Config; + apiVersion = isDefinedAndNotNull(apiVersion) ? apiVersion : '1.4'; if (apiVersion === '1.4') return BASE_ENDPOINT; switch (apiServer) { @@ -191,14 +194,16 @@ const attachUserAndCompany = (message, Config) => { */ const filterCustomAttributes = (payload, type, destination) => { let ReservedAttributesList; + let { apiVersion } = destination.Config; + apiVersion = isDefinedAndNotNull(apiVersion) ? apiVersion : '1.4'; if (type === 'user') { ReservedAttributesList = - destination.Config.apiVersion === '1.4' + apiVersion === '1.4' ? ReservedAttributes.oldVersionUserAttributes : ReservedAttributes.newVersionUserAttributes; } else { ReservedAttributesList = - destination.Config.apiVersion === '1.4' + apiVersion === '1.4' ? ReservedAttributes.oldVersionCompanyAttributes : ReservedAttributes.newVersionCompanyAttributes; } @@ -209,9 +214,7 @@ const filterCustomAttributes = (payload, type, destination) => { }); if (isDefinedAndNotNull(customAttributes) && Object.keys(customAttributes).length > 0) { customAttributes = - destination.Config.apiVersion === 'latest' - ? flattenJson(customAttributes, '_') - : flattenJson(customAttributes); + apiVersion === '1.4' ? flattenJson(customAttributes) : flattenJson(customAttributes, '_'); } } return Object.keys(customAttributes).length === 0 ? undefined : customAttributes; diff --git a/src/cdk/v2/destinations/intercom/utils.test.js b/src/cdk/v2/destinations/intercom/utils.test.js index f2e07ab4d6..3f08fa147c 100644 --- a/src/cdk/v2/destinations/intercom/utils.test.js +++ b/src/cdk/v2/destinations/intercom/utils.test.js @@ -211,6 +211,7 @@ describe('getBaseEndpoint utility test', () => { const destination = { Config: { apiServer: 'eu', + apiVersion: 'latest', }, }; const result = getBaseEndpoint(destination); @@ -221,6 +222,7 @@ describe('getBaseEndpoint utility test', () => { const destination = { Config: { apiServer: 'au', + apiVersion: 'latest', }, }; const result = getBaseEndpoint(destination); @@ -252,7 +254,7 @@ describe('getHeaders utility test', () => { Accept: 'application/json', 'Intercom-Version': '2.10', }; - const headers = getHeaders(destination); + const headers = getHeaders(destination, 'latest'); expect(headers).toEqual(expectedHeaders); }); }); diff --git a/test/integrations/destinations/intercom/processor/data.ts b/test/integrations/destinations/intercom/processor/data.ts index 1dc89a1a61..ed78e273c6 100644 --- a/test/integrations/destinations/intercom/processor/data.ts +++ b/test/integrations/destinations/intercom/processor/data.ts @@ -3485,7 +3485,6 @@ export const data = [ }, Config: { apiKey: 'abcd=', - apiVersion: '1.4', appId: 'asdasdasd', collectContext: false, }, @@ -3632,7 +3631,6 @@ export const data = [ Config: { apiKey: 'abcd=', appId: 'asdasdasd', - apiVersion: '1.4', collectContext: false, sendAnonymousId: true, },