Skip to content

Commit

Permalink
fix(intercom): fix date conversion from unix timestamp to js date of …
Browse files Browse the repository at this point in the history
…intercom attributes (#742)
  • Loading branch information
jeffladiray authored Jun 15, 2021
1 parent a0eb563 commit 97fa090
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/integrations/intercom/serializers/intercom-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ const JSONAPISerializer = require('jsonapi-serializer').Serializer;
function serializeIntercomAttributes(attributes, collectionName, meta) {
const type = `${collectionName}_intercom_attributes`;

const unixTimestampToDateOrNull = (unixTimestamp) =>
unixTimestamp && new Date(unixTimestamp * 1000);

// Attributes keys ending with `_at` are unix timestamp
// thus they need to be converted to js Date
Object.entries(attributes).forEach(([attributeKey, attributeValue]) => {
if (attributeKey.endsWith('_at')) {
attributes[attributeKey] = unixTimestampToDateOrNull(attributeValue);
}
});

return new JSONAPISerializer(type, attributes, {
attributes: [
'email',
Expand Down
125 changes: 125 additions & 0 deletions test/integrations/intercom/serializers/intercom-attributes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const serializeAttributes = require('../../../../src/integrations/intercom/serializers/intercom-attributes');

describe('integrations > intercom > serializers > intercom-attributes', () => {
describe('on an empty object', () => {
it('should return a JSONAPI version of the empty object', () => {
expect.assertions(1);

const serializedIntercomAttributes = serializeAttributes({}, 'x', {});

expect(serializedIntercomAttributes).toStrictEqual({
data: {
type: 'x_intercom_attributes',
},
meta: {},
});
});
});

describe('with a classic intercom payload', () => {
it('should format the intercom response as JSONAPI', () => {
expect.assertions(1);

// Most of theses fields are "useless", but this is the look of
// a response sent by intercom/services/attributes-getter.js
const intercomAttributes = {
type: 'contact',
id: 'id',
workspace_id: 'workspace_id',
external_id: null,
role: 'user',
email: '[email protected]',
phone: null,
name: 'Username 1',
avatar: null,
owner_id: null,
social_profiles: { type: 'list', data: [] },
has_hard_bounced: false,
marked_email_as_spam: false,
unsubscribed_from_emails: false,
created_at: null,
updated_at: null,
signed_up_at: null,
last_seen_at: null,
last_replied_at: null,
last_contacted_at: null,
last_email_opened_at: null,
last_email_clicked_at: null,
language_override: null,
browser: null,
browser_version: null,
browser_language: null,
os: null,
location: {
type: 'location',
country: null,
region: null,
city: null,
},
android_app_name: null,
android_app_version: null,
android_device: null,
android_os_version: null,
android_sdk_version: null,
android_last_seen_at: null,
ios_app_name: null,
ios_app_version: null,
ios_device: null,
ios_os_version: null,
ios_sdk_version: null,
ios_last_seen_at: null,
custom_attributes: {},
city: null,
country: null,
};

const serializedIntercomAttributes = serializeAttributes(intercomAttributes, 'x', {});

expect(serializedIntercomAttributes).toStrictEqual({
data: {
type: 'x_intercom_attributes',
attributes: {
browser: null,
city: null,
country: null,
created_at: null,
email: '[email protected]',
last_contacted_at: null,
last_email_clicked_at: null,
last_email_opened_at: null,
last_replied_at: null,
last_seen_at: null,
name: 'Username 1',
role: 'user',
signed_up_at: null,
updated_at: null,
},
id: 'id',
},
meta: {},
});
});

describe('when there are dates in the response', () => {
it('should correctly format the date from unix timestamp', () => {
expect.assertions(6);

const intercomAttributes = {
updated_at: null,
created_at: 1586853350,
email: '[email protected]',
};
const serializedIntercomAttributes = serializeAttributes(intercomAttributes, 'x', {});

expect(serializedIntercomAttributes).toBeDefined();
expect(serializedIntercomAttributes.data).toBeDefined();
const serializedAttributes = serializedIntercomAttributes.data.attributes;
expect(serializedAttributes).toBeDefined();
expect(serializedAttributes.updated_at).toBeNull();
expect(serializedAttributes.created_at).toStrictEqual(new Date(1586853350000));
expect(serializedAttributes.email)
.toStrictEqual(intercomAttributes.email);
});
});
});
});

0 comments on commit 97fa090

Please sign in to comment.