From 75e9f462b0ff9b9a8abab3c78dc7d147926e9e5e Mon Sep 17 00:00:00 2001 From: AASHISH MALIK Date: Tue, 27 Feb 2024 19:32:38 +0530 Subject: [PATCH] fix: convert to string from null in hs (#3136) --- src/v0/destinations/hs/util.js | 5 +- src/v0/util/index.js | 2 + .../destinations/hs/processor/data.ts | 104 ++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/v0/destinations/hs/util.js b/src/v0/destinations/hs/util.js index 32ee923f5f..359c93dc1a 100644 --- a/src/v0/destinations/hs/util.js +++ b/src/v0/destinations/hs/util.js @@ -19,6 +19,7 @@ const { getHashFromArray, getDestinationExternalIDInfoForRetl, getValueFromMessage, + isNull, } = require('../../util'); const { CONTACT_PROPERTY_MAP_ENDPOINT, @@ -223,7 +224,9 @@ const getTransformedJSON = async (message, destination, propertyMap) => { // lowercase and replace ' ' & '.' with '_' const hsSupportedKey = formatKey(traitsKey); if (!rawPayload[traitsKey] && propertyMap[hsSupportedKey]) { - let propValue = traits[traitsKey]; + // HS accepts empty string to remove the property from contact + // https://community.hubspot.com/t5/APIs-Integrations/Clearing-values-of-custom-properties-in-Hubspot-contact-using/m-p/409156 + let propValue = isNull(traits[traitsKey]) ? '' : traits[traitsKey]; if (propertyMap[hsSupportedKey] === 'date') { propValue = getUTCMidnightTimeStampValue(propValue); } diff --git a/src/v0/util/index.js b/src/v0/util/index.js index 1d952693f2..9792401241 100644 --- a/src/v0/util/index.js +++ b/src/v0/util/index.js @@ -52,6 +52,7 @@ const removeUndefinedAndNullAndEmptyValues = (obj) => lodash.pickBy(obj, isDefinedAndNotNullAndNotEmpty); const isBlank = (value) => lodash.isEmpty(lodash.toString(value)); const flattenMap = (collection) => lodash.flatMap(collection, (x) => x); +const isNull = (x) => lodash.isNull(x); // ======================================================================== // GENERIC UTLITY // ======================================================================== @@ -2266,6 +2267,7 @@ module.exports = { isDefinedAndNotNullAndNotEmpty, isEmpty, isNotEmpty, + isNull, isEmptyObject, isHttpStatusRetryable, isHttpStatusSuccess, diff --git a/test/integrations/destinations/hs/processor/data.ts b/test/integrations/destinations/hs/processor/data.ts index 03ad9d0a3b..f45f3a719b 100644 --- a/test/integrations/destinations/hs/processor/data.ts +++ b/test/integrations/destinations/hs/processor/data.ts @@ -1,3 +1,45 @@ +import { Destination } from '../../../../../src/types'; +import { generateMetadata, generateSimplifiedIdentifyPayload } from '../../../testUtils'; + +const commonOutputHeaders = { + 'Content-Type': 'application/json', + Authorization: 'Bearer dummy-access-token', +}; + +const destination: Destination = { + Config: { + authorizationType: 'newPrivateAppApi', + accessToken: 'dummy-access-token', + hubID: 'dummy-hubId', + apiKey: 'dummy-apikey', + apiVersion: 'newApi', + lookupField: 'email', + hubspotEvents: [], + eventFilteringOption: 'disable', + blacklistedEvents: [ + { + eventName: '', + }, + ], + whitelistedEvents: [ + { + eventName: '', + }, + ], + }, + Enabled: true, + ID: '123', + Name: 'hs', + DestinationDefinition: { + ID: '123', + Name: 'hs', + DisplayName: 'Hubspot', + Config: {}, + }, + WorkspaceID: '123', + Transformations: [], +}; + export const data = [ { name: 'hs', @@ -5269,4 +5311,66 @@ export const data = [ }, }, }, + { + name: 'hs', + description: 'Test coversion of null to string values', + feature: 'processor', + module: 'destination', + version: 'v0', + input: { + request: { + body: [ + { + destination, + message: generateSimplifiedIdentifyPayload({ + userId: '12345', + context: { + traits: { + email: 'noname@email.com', + firstname: null, + gender: '', + lookupField: 'email', + }, + }, + }), + metadata: generateMetadata(1), + }, + ], + }, + }, + output: { + response: { + status: 200, + body: [ + { + output: { + version: '1', + type: 'REST', + userId: '', + method: 'POST', + endpoint: 'https://api.hubapi.com/crm/v3/objects/contacts', + files: {}, + headers: commonOutputHeaders, + operation: 'createContacts', + params: {}, + body: { + FORM: {}, + JSON: { + properties: { + email: 'noname@email.com', + firstname: '', + gender: '', + }, + }, + JSON_ARRAY: {}, + XML: {}, + }, + }, + statusCode: 200, + metadata: generateMetadata(1), + }, + ], + }, + }, + }, ];