diff --git a/src/v0/destinations/hs/HSTransform-v1.js b/src/v0/destinations/hs/HSTransform-v1.js index 54b39b4317..5cb80f10f5 100644 --- a/src/v0/destinations/hs/HSTransform-v1.js +++ b/src/v0/destinations/hs/HSTransform-v1.js @@ -34,8 +34,7 @@ const { getEmailAndUpdatedProps, formatPropertyValueForIdentify, getHsSearchId, - getUTCMidnightTimeStampValue, - getProperties, + populateTraits, } = require('./util'); const { JSON_MIME_TYPE } = require('../../util/constant'); @@ -54,7 +53,7 @@ const { JSON_MIME_TYPE } = require('../../util/constant'); */ const processLegacyIdentify = async (message, destination, propertyMap) => { const { Config } = destination; - const traits = getFieldValueFromMessage(message, 'traits'); + let traits = getFieldValueFromMessage(message, 'traits'); const mappedToDestination = get(message, MappedToDestinationKey); const operation = get(message, 'context.hubspotOperation'); // if mappedToDestination is set true, then add externalId to traits @@ -83,20 +82,7 @@ const processLegacyIdentify = 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); - } - }) - + traits = await populateTraits(propertyMap, traits, destination); response.body.JSON = removeUndefinedAndNullValues({ properties: traits }); response.source = 'rETL'; response.operation = operation; diff --git a/src/v0/destinations/hs/HSTransform-v2.js b/src/v0/destinations/hs/HSTransform-v2.js index e7ad44f58a..26c12d3eea 100644 --- a/src/v0/destinations/hs/HSTransform-v2.js +++ b/src/v0/destinations/hs/HSTransform-v2.js @@ -41,8 +41,7 @@ const { searchContacts, getEventAndPropertiesFromConfig, getHsSearchId, - getUTCMidnightTimeStampValue, - getProperties, + populateTraits, } = require('./util'); const { JSON_MIME_TYPE } = require('../../util/constant'); @@ -71,7 +70,7 @@ const addHsAuthentication = (response, Config) => { */ const processIdentify = async (message, destination, propertyMap) => { const { Config } = destination; - const traits = getFieldValueFromMessage(message, 'traits'); + let traits = getFieldValueFromMessage(message, 'traits'); const mappedToDestination = get(message, MappedToDestinationKey); const operation = get(message, 'context.hubspotOperation'); const externalIdObj = getDestinationExternalIDObjectForRetl(message, 'HS'); @@ -126,20 +125,7 @@ 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); - } - }) - + traits = await populateTraits(propertyMap, traits, destination); response.body.JSON = removeUndefinedAndNullValues({ properties: traits }); response.source = 'rETL'; response.operation = operation; diff --git a/src/v0/destinations/hs/util.js b/src/v0/destinations/hs/util.js index e1a1b8c846..5d7a01da74 100644 --- a/src/v0/destinations/hs/util.js +++ b/src/v0/destinations/hs/util.js @@ -158,16 +158,14 @@ const validatePayloadDataTypes = (propertyMap, hsSupportedKey, value, traitsKey) if (propertyMap[hsSupportedKey] === 'bool' && typeof propValue === 'object') { throw new InstrumentationError( - `Property ${traitsKey} data type ${typeof propValue} is not matching with Hubspot property data type ${ - propertyMap[hsSupportedKey] + `Property ${traitsKey} data type ${typeof propValue} is not matching with Hubspot property data type ${propertyMap[hsSupportedKey] }`, ); } if (propertyMap[hsSupportedKey] === 'number' && typeof propValue !== 'number') { throw new InstrumentationError( - `Property ${traitsKey} data type ${typeof propValue} is not matching with Hubspot property data type ${ - propertyMap[hsSupportedKey] + `Property ${traitsKey} data type ${typeof propValue} is not matching with Hubspot property data type ${propertyMap[hsSupportedKey] }`, ); } @@ -487,13 +485,7 @@ const getExistingData = async (inputs, destination) => { inputs.map(async (input) => { const { message } = input; const { destinationExternalId } = getDestinationExternalIDInfoForRetl(message, DESTINATION); - - if(typeof destinationExternalId === 'string'){ - values.push(destinationExternalId.toLowerCase()); - } else{ - const value = typeof destinationExternalId === 'object' ? JSON.stringify(destinationExternalId).toLowerCase() : destinationExternalId.toString(); - values.push(value); - } + values.push(destinationExternalId.toString().toLowerCase()); }); values = Array.from(new Set(values)); @@ -540,15 +532,15 @@ const getExistingData = async (inputs, destination) => { searchResponse = Config.authorizationType === 'newPrivateAppApi' ? await httpPOST(url, requestData, requestOptions, { - destType: 'hs', - feature: 'transformation', - endpointPath, - }) + destType: 'hs', + feature: 'transformation', + endpointPath, + }) : await httpPOST(url, requestData, { - destType: 'hs', - feature: 'transformation', - endpointPath, - }); + destType: 'hs', + feature: 'transformation', + endpointPath, + }); searchResponse = processAxiosResponse(searchResponse); if (searchResponse.status !== 200) { @@ -643,6 +635,31 @@ const getHsSearchId = (message) => { return { hsSearchId }; }; +/** + * returns updated traits + * @param {*} propertyMap + * @param {*} traits + * @param {*} destination + */ +const populateTraits = async (propertyMap, traits, destination) => { + const populatedTraits = traits; + let propertyToTypeMap = propertyMap; + if (!propertyToTypeMap) { + // fetch HS properties + propertyToTypeMap = await getProperties(destination); + } + + const keys = Object.keys(populatedTraits); + keys.forEach((key) => { + const value = populatedTraits[key]; + if (propertyToTypeMap[key] === 'date') { + populatedTraits[key] = getUTCMidnightTimeStampValue(value); + } + }) + + return populatedTraits; +} + module.exports = { validateDestinationConfig, formatKey, @@ -657,4 +674,5 @@ module.exports = { getHsSearchId, validatePayloadDataTypes, getUTCMidnightTimeStampValue, + populateTraits, };