Skip to content

Commit

Permalink
chore: code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir-4116 committed Oct 20, 2023
1 parent 391c7cd commit 255e9aa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 53 deletions.
20 changes: 3 additions & 17 deletions src/v0/destinations/hs/HSTransform-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ const {
getEmailAndUpdatedProps,
formatPropertyValueForIdentify,
getHsSearchId,
getUTCMidnightTimeStampValue,
getProperties,
populateTraits,
} = require('./util');
const { JSON_MIME_TYPE } = require('../../util/constant');

Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 3 additions & 17 deletions src/v0/destinations/hs/HSTransform-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ const {
searchContacts,
getEventAndPropertiesFromConfig,
getHsSearchId,
getUTCMidnightTimeStampValue,
getProperties,
populateTraits,
} = require('./util');
const { JSON_MIME_TYPE } = require('../../util/constant');

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
56 changes: 37 additions & 19 deletions src/v0/destinations/hs/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}`,
);
}
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -657,4 +674,5 @@ module.exports = {
getHsSearchId,
validatePayloadDataTypes,
getUTCMidnightTimeStampValue,
populateTraits,
};

0 comments on commit 255e9aa

Please sign in to comment.