Skip to content

Commit

Permalink
fix: support different lookup fields and custom_attributes for rETL e…
Browse files Browse the repository at this point in the history
…vents
  • Loading branch information
ItsSudip committed Sep 23, 2024
1 parent ede31ef commit 6c5afd1
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/cdk/v2/destinations/intercom/procWorkflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ steps:
template: |
const payload = .message.context.mappedToDestination ? $.outputs.rEtlPayload : $.outputs.identifyTransformationForLatestVersion;
payload.name = $.getName(.message);
payload.custom_attributes = .message.context.traits || {};
payload.custom_attributes = $.filterCustomAttributes(payload, "user", .destination);
payload.custom_attributes = !.message.context.mappedToDestination ? .message.context.traits || {} : .message.traits.custom_attributes || {};
payload.custom_attributes = $.filterCustomAttributes(payload, "user", .destination, .message);
payload.external_id = !payload.external_id && .destination.Config.sendAnonymousId && .message.anonymousId ? .message.anonymousId : payload.external_id;
$.context.payload = payload;
$.assert($.context.payload.external_id || $.context.payload.email, "Either email or userId is required for Identify call");
Expand Down Expand Up @@ -114,7 +114,7 @@ steps:
update_last_request_at: typeof .destination.Config.updateLastRequestAt === 'boolean' ? .destination.Config.updateLastRequestAt : true
}
payload.companies = $.getCompaniesList(payload);
payload.custom_attributes = !.message.context.mappedToDestination ? $.filterCustomAttributes(payload, "user", .destination);
payload.custom_attributes = !.message.context.mappedToDestination ? $.filterCustomAttributes(payload, "user", .destination,.message);
payload.user_id = !payload.user_id && .destination.Config.sendAnonymousId && .message.anonymousId ? .message.anonymousId : payload.user_id;
$.context.payload = payload;
$.assert($.context.payload.user_id || $.context.payload.email, "Either of `email` or `userId` is required for Identify call");
Expand Down Expand Up @@ -175,7 +175,7 @@ steps:
$.assert(.message.groupId, "groupId is required for group call");
const payload = .message.context.mappedToDestination ? $.outputs.rEtlPayload : $.outputs.groupTransformation;
payload.custom_attributes = .message.traits || {};
payload.custom_attributes = $.filterCustomAttributes(payload, "company", .destination);
payload.custom_attributes = $.filterCustomAttributes(payload, "company", .destination,.message);
$.context.payload = payload;
- name: whenSearchContactFound
condition: $.isDefinedAndNotNull($.outputs.searchContact)
Expand Down Expand Up @@ -214,7 +214,7 @@ steps:
...payload,
custom_attributes : $.getFieldValueFromMessage(.message, "traits") || {}
}
payload.custom_attributes = $.filterCustomAttributes(payload, "company", .destination);
payload.custom_attributes = $.filterCustomAttributes(payload, "company", .destination, .message);
response.body.JSON = $.removeUndefinedAndNullValues(payload);
response.endpoint = $.getBaseEndpoint(.destination) + "/" + "companies";
response.headers = $.getHeaders(.destination, $.outputs.apiVersion);
Expand Down
25 changes: 17 additions & 8 deletions src/cdk/v2/destinations/intercom/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,26 @@ const attachUserAndCompany = (message, Config) => {
* @param {*} type
* @returns
*/
const filterCustomAttributes = (payload, type, destination) => {
const filterCustomAttributes = (payload, type, destination, message) => {
let ReservedAttributesList;
let { apiVersion } = destination.Config;
apiVersion = isDefinedAndNotNull(apiVersion) ? apiVersion : 'v2';
// we are discarding the lookup field from custom attributes
const lookupField = getLookUpField(message);
if (type === 'user') {
ReservedAttributesList =
apiVersion === 'v1'
ReservedAttributesList = [
...(apiVersion === 'v1'
? ReservedAttributes.v1UserAttributes
: ReservedAttributes.v2UserAttributes;
: ReservedAttributes.v2UserAttributes),
lookupField,
];
} else {
ReservedAttributesList =
apiVersion === 'v1'
ReservedAttributesList = [
...(apiVersion === 'v1'
? ReservedAttributes.v1CompanyAttributes
: ReservedAttributes.v2CompanyAttributes;
: ReservedAttributes.v2CompanyAttributes),
lookupField !== 'email' && lookupField,
];
}
let customAttributes = { ...get(payload, 'custom_attributes') };
if (customAttributes) {
Expand All @@ -270,7 +276,10 @@ const filterCustomAttributes = (payload, type, destination) => {
*/
const searchContact = async (message, destination, metadata) => {
const lookupField = getLookUpField(message);
const lookupFieldValue = getFieldValueFromMessage(message, lookupField);
let lookupFieldValue = getFieldValueFromMessage(message, lookupField);
if (!lookupFieldValue) {
lookupFieldValue = get(message, `context.traits.${lookupField}`);
}
const data = JSON.stringify({
query: {
operator: 'AND',
Expand Down
37 changes: 37 additions & 0 deletions test/integrations/destinations/intercom/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1041,5 +1041,42 @@ const deliveryCallsData = [
},
},
},
{
httpReq: {
method: 'post',
url: 'https://api.intercom.io/contacts/search',
data: {
query: {
operator: 'AND',
value: [{ field: 'external_id', operator: '=', value: '10156' }],
},
},
headers: { ...commonHeaders, 'Intercom-Version': '2.10', 'User-Agent': 'RudderStack' },
},
httpRes: {
status: 200,
statusText: 'ok',
data: {
type: 'list',
total_count: 1,
pages: {
type: 'pages',
page: 1,
per_page: 50,
total_pages: 1,
},
data: [
{
type: 'contact',
id: '7070129940741e45d040',
workspace_id: 'rudderWorkspace',
external_id: 'user@2',
role: 'user',
email: '[email protected]',
},
],
},
},
},
];
export const networkCallsData = [...deleteNwData, ...deliveryCallsData];
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ const user3Traits = {
name: 'Test Rudderlabs',
phone: '+91 9999999999',
email: '[email protected]',
custom_attributes: {
ca1: 'value1',
ca2: 'value2',
},
};

const user4Traits = {
Expand Down Expand Up @@ -170,6 +174,10 @@ const expectedUser3Traits = {
name: 'Test Rudderlabs',
phone: '+91 9999999999',
email: '[email protected]',
custom_attributes: {
ca1: 'value1',
ca2: 'value2',
},
};

const expectedUser4Traits = {
Expand Down Expand Up @@ -233,6 +241,17 @@ const expectedUser6Traits = {
],
};

const expectedUser7Traits = {
custom_attributes: {
anonymousId: '58b21c2d-f8d5-4410-a2d0-b268a26b7e33',
key1: 'value1',
},
email: '[email protected]',
name: 'Test Name',
phone: '9876543210',
signed_up_at: 1601493060,
};

const timestamp = '2023-11-22T10:12:44.757+05:30';
const originalTimestamp = '2023-11-10T14:42:44.724Z';

Expand Down Expand Up @@ -1024,4 +1043,63 @@ export const identifyTestData = [
},
},
},
{
id: 'intercom-identify-test-16',
name: 'intercom',
description: 'V1 version : Identify test with different lookup field than email',
scenario: 'Business',
successCriteria:
'Response status code should be 200 and response should contain update user payload with all traits',
feature: 'processor',
module: 'destination',
version: 'v0',
input: {
request: {
body: [
{
destination: v2Destination,
message: {
context: {
externalId: [
{
id: '10156',
type: 'INTERCOM-customer',
identifierType: 'user_id',
},
],
traits: { ...user5Traits, external_id: '10156' },
},
type: 'identify',
timestamp,
originalTimestamp,
integrations: {
INTERCOM: {
lookup: 'external_id',
},
},
},
metadata: generateMetadata(1),
},
],
},
},
output: {
response: {
status: 200,
body: [
{
output: transformResultBuilder({
userId: '',
endpoint: `${v2Endpoint}/7070129940741e45d040`,
headers: v2Headers,
method: 'PUT',
JSON: expectedUser7Traits,
}),
statusCode: 200,
metadata: generateMetadata(1),
},
],
},
},
},
];

0 comments on commit 6c5afd1

Please sign in to comment.