Skip to content

Commit

Permalink
chore: added step to check value of apiVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Jan 25, 2024
1 parent 95d114a commit 3522b58
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
31 changes: 18 additions & 13 deletions src/cdk/v2/destinations/intercom/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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")}}}},
Expand All @@ -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);
Expand All @@ -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")}}}},
Expand All @@ -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 = {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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: |
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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;
19 changes: 11 additions & 8 deletions src/cdk/v2/destinations/intercom/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/cdk/v2/destinations/intercom/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ describe('getBaseEndpoint utility test', () => {
const destination = {
Config: {
apiServer: 'eu',
apiVersion: 'latest',
},
};
const result = getBaseEndpoint(destination);
Expand All @@ -221,6 +222,7 @@ describe('getBaseEndpoint utility test', () => {
const destination = {
Config: {
apiServer: 'au',
apiVersion: 'latest',
},
};
const result = getBaseEndpoint(destination);
Expand Down Expand Up @@ -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);
});
});
Expand Down
2 changes: 0 additions & 2 deletions test/integrations/destinations/intercom/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3485,7 +3485,6 @@ export const data = [
},
Config: {
apiKey: 'abcd=',
apiVersion: '1.4',
appId: 'asdasdasd',
collectContext: false,
},
Expand Down Expand Up @@ -3632,7 +3631,6 @@ export const data = [
Config: {
apiKey: 'abcd=',
appId: 'asdasdasd',
apiVersion: '1.4',
collectContext: false,
sendAnonymousId: true,
},
Expand Down

0 comments on commit 3522b58

Please sign in to comment.