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 Jan 24, 2024
1 parent 181d0b1 commit 00fdf9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
38 changes: 23 additions & 15 deletions src/v0/destinations/hs/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ const getObjectAndIdentifierType = (firstMessage) => {
* @param {*} inputs
* @returns
*/
const extractUniqueValues = (inputs) => {
const extractIDsForSearchAPI = (inputs) => {
const values = inputs.map((input) => {
const { message } = input;
const { destinationExternalId } = getDestinationExternalIDInfoForRetl(message, DESTINATION);
Expand All @@ -503,27 +503,35 @@ const extractUniqueValues = (inputs) => {
* @param {*} requestOptions
* @param {*} objectType
* @param {*} identifierType
* @param {*} Config
* @param {*} destination
* @returns
*/
const performHubSpotSearch = async (rqdata, requestOptions, objectType, identifierType, Config) => {
const performHubSpotSearch = async (
rqdata,
requestOptions,
objectType,
identifierType,
destination,
) => {
let checkAfter = 1;
const searchResults = [];
const requestData = rqdata;
const { Config } = destination;

const endpoint = IDENTIFY_CRM_SEARCH_ALL_OBJECTS.replace(':objectType', objectType);
const endpointPath = `objects/:objectType/search`;

const url =
Config.authorizationType === 'newPrivateAppApi'
? endpoint
: `${endpoint}?hapikey=${Config.apiKey}`;

/* *
* This is needed for processing paginated response when searching hubspot.
* we can't avoid await in loop as response to the request contains the pagination details
* */

while (checkAfter) {
const endpoint = IDENTIFY_CRM_SEARCH_ALL_OBJECTS.replace(':objectType', objectType);
const endpointPath = `objects/:objectType/search`;

const url =
Config.authorizationType === 'newPrivateAppApi'
? endpoint
: `${endpoint}?hapikey=${Config.apiKey}`;
const searchResponse =
Config.authorizationType === 'newPrivateAppApi'
? await httpPOST(url, requestData, requestOptions, {
Expand Down Expand Up @@ -572,7 +580,7 @@ const performHubSpotSearch = async (rqdata, requestOptions, objectType, identifi
* @param {*} inputs
* @param {*} destination
*/
const getExistingData = async (inputs, destination) => {
const getExistingContactsData = async (inputs, destination) => {
const { Config } = destination;
const updateHubspotIds = [];
const firstMessage = inputs[0].message;
Expand All @@ -583,7 +591,7 @@ const getExistingData = async (inputs, destination) => {

const { objectType, identifierType } = getObjectAndIdentifierType(firstMessage);

const values = extractUniqueValues(inputs);
const values = extractIDsForSearchAPI(inputs);
const valuesChunk = lodash.chunk(values, MAX_CONTACTS_PER_REQUEST);

// eslint-disable-next-line no-restricted-syntax
Expand Down Expand Up @@ -616,7 +624,7 @@ const getExistingData = async (inputs, destination) => {
requestOptions,
objectType,
identifierType,
Config,
destination,
);
if (searchResults.length > 0) {
updateHubspotIds.push(...searchResults);
Expand Down Expand Up @@ -652,7 +660,7 @@ const setHsSearchId = (input, id) => {

const splitEventsForCreateUpdate = async (inputs, destination) => {
// get all the id and properties of already existing objects needed for update.
const updateHubspotIds = await getExistingData(inputs, destination);
const updateHubspotIds = await getExistingContactsData(inputs, destination);

const resultInput = inputs.map((input) => {
const { message } = input;
Expand Down Expand Up @@ -732,5 +740,5 @@ module.exports = {
getUTCMidnightTimeStampValue,
populateTraits,
getObjectAndIdentifierType,
extractUniqueValues,
extractIDsForSearchAPI,
};
6 changes: 3 additions & 3 deletions src/v0/destinations/hs/util.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {
extractUniqueValues,
extractIDsForSearchAPI,
validatePayloadDataTypes,
getObjectAndIdentifierType,
} = require('./util');
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('extractUniqueValues utility test cases', () => {
},
];

const result = extractUniqueValues(inputs);
const result = extractIDsForSearchAPI(inputs);

expect(result).toEqual([
'[email protected]',
Expand All @@ -196,7 +196,7 @@ describe('extractUniqueValues utility test cases', () => {

it('Should return an empty array when the input is empty', () => {
const inputs = [];
const result = extractUniqueValues(inputs);
const result = extractIDsForSearchAPI(inputs);
expect(result).toEqual([]);
});
});

0 comments on commit 00fdf9a

Please sign in to comment.