Skip to content

Commit

Permalink
fix(hs): time stamp and contact not found issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Oct 20, 2023
1 parent 4bb71f4 commit 391c7cd
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/v0/destinations/hs/HSTransform-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const {
getEmailAndUpdatedProps,
formatPropertyValueForIdentify,
getHsSearchId,
getUTCMidnightTimeStampValue,
getProperties,
} = require('./util');
const { JSON_MIME_TYPE } = require('../../util/constant');

Expand Down Expand Up @@ -80,6 +82,21 @@ const processLegacyIdentify = async (message, destination, propertyMap) => {
)}/${hsSearchId}`;
response.method = defaultPatchRequestConfig.requestMethod;
}

if (!propertyMap) {
// fetch HS properties
// eslint-disable-next-line no-param-reassign
propertyMap = await getProperties(destination);
}

const keys = Object.keys(traits);
keys.forEach((key) => {
const value = traits[key];
if (propertyMap[key] === 'date') {
traits[key] = getUTCMidnightTimeStampValue(value);

Check warning on line 96 in src/v0/destinations/hs/HSTransform-v1.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/hs/HSTransform-v1.js#L96

Added line #L96 was not covered by tests
}
})

response.body.JSON = removeUndefinedAndNullValues({ properties: traits });
response.source = 'rETL';
response.operation = operation;
Expand Down
16 changes: 16 additions & 0 deletions src/v0/destinations/hs/HSTransform-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const {
searchContacts,
getEventAndPropertiesFromConfig,
getHsSearchId,
getUTCMidnightTimeStampValue,
getProperties,
} = require('./util');
const { JSON_MIME_TYPE } = require('../../util/constant');

Expand Down Expand Up @@ -124,6 +126,20 @@ const processIdentify = async (message, destination, propertyMap) => {
response.method = defaultPatchRequestConfig.requestMethod;
}

if (!propertyMap) {
// fetch HS properties
// eslint-disable-next-line no-param-reassign
propertyMap = await getProperties(destination);
}

const keys = Object.keys(traits);
keys.forEach((key) => {
const value = traits[key];
if(propertyMap[key] === 'date'){
traits[key] = getUTCMidnightTimeStampValue(value);
}
})

response.body.JSON = removeUndefinedAndNullValues({ properties: traits });
response.source = 'rETL';
response.operation = operation;
Expand Down
1 change: 1 addition & 0 deletions src/v0/destinations/hs/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const processRouterDest = async (inputs, reqMetadata) => {
if (mappedToDestination && GENERIC_TRUE_VALUES.includes(mappedToDestination?.toString())) {
// skip splitting the batches to inserts and updates if object it is an association
if (objectType.toLowerCase() !== 'association') {
propertyMap = await getProperties(destination);
// get info about existing objects and splitting accordingly.
tempInputs = await splitEventsForCreateUpdate(tempInputs, destination);
}
Expand Down
30 changes: 24 additions & 6 deletions src/v0/destinations/hs/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ const validatePayloadDataTypes = (propertyMap, hsSupportedKey, value, traitsKey)
return propValue;
};

/**
* Converts date to UTC Midnight TimeStamp
* @param {*} propValue
* @returns
*/
const getUTCMidnightTimeStampValue = (propValue) => {
const time = propValue;
const date = new Date(time);
date.setUTCHours(0, 0, 0, 0);
return date.getTime();
}

/**
* add addtional properties in the payload that is provided in traits
* only when it matches with HS properties (pre-defined/created from dashboard)
Expand Down Expand Up @@ -204,10 +216,7 @@ const getTransformedJSON = async (message, destination, propertyMap) => {
if (!rawPayload[traitsKey] && propertyMap[hsSupportedKey]) {
let propValue = traits[traitsKey];
if (propertyMap[hsSupportedKey] === 'date') {
const time = propValue;
const date = new Date(time);
date.setUTCHours(0, 0, 0, 0);
propValue = date.getTime();
propValue = getUTCMidnightTimeStampValue(propValue);

Check warning on line 219 in src/v0/destinations/hs/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/hs/util.js#L219

Added line #L219 was not covered by tests
}

rawPayload[hsSupportedKey] = validatePayloadDataTypes(
Expand Down Expand Up @@ -459,7 +468,7 @@ const getEventAndPropertiesFromConfig = (message, destination, payload) => {
*/
const getExistingData = async (inputs, destination) => {
const { Config } = destination;
const values = [];
let values = [];
let searchResponse;
let updateHubspotIds = [];
const firstMessage = inputs[0].message;
Expand All @@ -478,8 +487,16 @@ const getExistingData = async (inputs, destination) => {
inputs.map(async (input) => {
const { message } = input;
const { destinationExternalId } = getDestinationExternalIDInfoForRetl(message, DESTINATION);
values.push(destinationExternalId);

if(typeof destinationExternalId === 'string'){
values.push(destinationExternalId.toLowerCase());
} else{

Check warning on line 493 in src/v0/destinations/hs/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/hs/util.js#L493

Added line #L493 was not covered by tests
const value = typeof destinationExternalId === 'object' ? JSON.stringify(destinationExternalId).toLowerCase() : destinationExternalId.toString();
values.push(value);

Check warning on line 495 in src/v0/destinations/hs/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/hs/util.js#L495

Added line #L495 was not covered by tests
}
});

values = Array.from(new Set(values));
const requestData = {
filterGroups: [
{
Expand Down Expand Up @@ -639,4 +656,5 @@ module.exports = {
splitEventsForCreateUpdate,
getHsSearchId,
validatePayloadDataTypes,
getUTCMidnightTimeStampValue,
};
24 changes: 24 additions & 0 deletions test/__mocks__/data/hs/response.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
{ "name": "company_size", "type": "string" },
{ "name": "date_of_birth", "type": "string" },
{ "name": "days_to_close", "type": "number" },
{
"name": "date_submitted",
"type": "date"
},
{
"name": "days_create",
"type": "date"
},
{
"name": "days_closed",
"type": "date"
},
{ "name": "degree", "type": "string" },
{ "name": "field_of_study", "type": "string" },
{ "name": "first_conversion_date", "type": "datetime" },
Expand Down Expand Up @@ -192,6 +204,18 @@
{ "name": "company_size", "type": "string" },
{ "name": "date_of_birth", "type": "string" },
{ "name": "days_to_close", "type": "number" },
{
"name": "date_submitted",
"type": "date"
},
{
"name": "date_created",
"type": "date"
},
{
"name": "date_closed",
"type": "date"
},
{ "name": "degree", "type": "string" },
{ "name": "field_of_study", "type": "string" },
{ "name": "first_conversion_date", "type": "datetime" },
Expand Down
112 changes: 112 additions & 0 deletions test/__tests__/data/hs_router_rETL_input.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,117 @@
"metadata": {
"jobId": 3
}
},
{
"message": {
"channel": "web",
"context": {
"mappedToDestination": true,
"externalId": [
{
"identifierType": "email",
"id": "testhubspotdatetime@email.com",
"type": "HS-lead"
}
],
"sources": {
"job_id": "24c5HJxHomh6YCngEOCgjS5r1KX/Syncher",
"task_id": "vw_rs_mailchimp_mocked_hg_data",
"version": "v1.8.1",
"batch_id": "f252c69d-c40d-450e-bcd2-2cf26cb62762",
"job_run_id": "c8el40l6e87v0c4hkbl0",
"task_run_id": "c8el40l6e87v0c4hkblg"
}
},
"type": "identify",
"traits": {
"firstname": "Test Hubspot",
"anonymousId": "123451",
"country": "India",
"date_submitted": "2023-09-25T17:31:04.128251Z",
"date_created": "2023-03-30T01:02:03.05Z",
"date_closed": "2023-10-18T04:38:59.229347Z"
},
"messageId": "50360b9c-ea8d-409c-b672-c9230f91cce5",
"originalTimestamp": "2019-10-15T09:35:31.288Z",
"anonymousId": "00000000000000000000000000",
"userId": "12345",
"integrations": {
"All": true
},
"sentAt": "2019-10-14T09:03:22.563Z"
},
"destination": {
"Config": {
"authorizationType": "newPrivateAppApi",
"accessToken": "dummy-access-token",
"hubID": "dummy-hubId",
"apiKey": "dummy-apikey",
"apiVersion": "newApi",
"lookupField": "lookupField",
"hubspotEvents": [
{
"rsEventName": "Purchase",
"hubspotEventName": "pedummy-hubId_rs_hub_test",
"eventProperties": [
{
"from": "Revenue",
"to": "value"
},
{
"from": "Price",
"to": "cost"
}
]
},
{
"rsEventName": "Order Complete",
"hubspotEventName": "pedummy-hubId_rs_hub_chair",
"eventProperties": [
{
"from": "firstName",
"to": "first_name"
},
{
"from": "lastName",
"to": "last_name"
}
]
}
],
"eventFilteringOption": "disable",
"blacklistedEvents": [
{
"eventName": ""
}
],
"whitelistedEvents": [
{
"eventName": ""
}
]
},
"secretConfig": {},
"ID": "1mMy5cqbtfuaKZv1IhVQKnBdVwe",
"name": "Hubspot",
"enabled": true,
"workspaceId": "1TSN08muJTZwH8iCDmnnRt1pmLd",
"deleted": false,
"createdAt": "2020-12-30T08:39:32.005Z",
"updatedAt": "2021-02-03T16:22:31.374Z",
"destinationDefinition": {
"id": "1aIXqM806xAVm92nx07YwKbRrO9",
"name": "HS",
"displayName": "Hubspot",
"createdAt": "2020-04-09T09:24:31.794Z",
"updatedAt": "2021-01-11T11:03:28.103Z"
},
"transformations": [],
"isConnectionEnabled": true,
"isProcessorEnabled": true
},
"metadata": {
"jobId": 4
}
}
]
16 changes: 15 additions & 1 deletion test/__tests__/data/hs_router_rETL_output.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
"country": "India 1",
"email": "testhubspot@email.com"
}
},
{
"properties": {
"firstname": "Test Hubspot",
"anonymousId": "123451",
"country": "India",
"email": "testhubspotdatetime@email.com",
"date_closed": 1697587200000,
"date_created": 1680134400000,
"date_submitted": 1695600000000
}
}
]
},
Expand All @@ -32,6 +43,9 @@
"metadata": [
{
"jobId": 3
},
{
"jobId": 4
}
],
"batched": true,
Expand Down Expand Up @@ -214,4 +228,4 @@
"isProcessorEnabled": true
}
}
]
]

0 comments on commit 391c7cd

Please sign in to comment.