get-top-countries-of-clients-hubspot
Version:
A demo package that allows you to match retrieve top countries of clients from IP Pilot's database and save it for every company in your Hubspot account.
49 lines (37 loc) • 2.47 kB
JavaScript
;
const { populateHubspotCompaniesWithId } = require('populate-hubspot-companies-with-id');
const { areProvidedCompaniesCorrectType } = require('are-provided-companies-correct-type');
const { bulkUpdateHubspotCompanies, getTopCountriesOfClients, getHubspotCompanies } = require('./services');
const getTopCountriesOfClientsHubspot = async ({ hubspotApiKey, ippqlKey, ipqqlHost, providedHsCompanies }) => {
console.log(`running getTopCountriesOfClientsHubspot at: ${new Date()}`);
try {
if (!hubspotApiKey) throw new Error('please provide a hubspotApiKey');
if (!ippqlKey) throw new Error('please provide a ippqlKey');
if (!ipqqlHost) throw new Error('please provide a ipqqlHost');
if (typeof hubspotApiKey !== 'string') throw new Error('hubspotApiKey must be a string');
if (typeof ippqlKey !== 'string') throw new Error('ippqlKey must be a string');
if (typeof ipqqlHost !== 'string') throw new Error('ipqqlHost must be a string');
if (providedHsCompanies && !areProvidedCompaniesCorrectType(providedHsCompanies))
throw new Error(
'providedHsCompanies must be an array of objects containing company as prop with a string and targets as prop with an array of strings'
);
const hsCompanies = providedHsCompanies
? await populateHubspotCompaniesWithId(hubspotApiKey, providedHsCompanies)
: await getHubspotCompanies(hubspotApiKey);
if (!hsCompanies || !hsCompanies.length) throw new Error('no companies found in Hubspot');
const results = await getTopCountriesOfClients(hsCompanies, ippqlKey, ipqqlHost);
if (!results) return;
const topCountriesOfClients = results.filter((el) => el.topCountriesOfClients.length);
if (!topCountriesOfClients || !topCountriesOfClients.length) throw new Error('no topCountriesOfClients found');
const noResults = results.filter((el) => !el.topCountriesOfClients.length);
console.log('noResults.length', noResults.length);
console.log('topCountriesOfClients.length', topCountriesOfClients.length);
const result = await bulkUpdateHubspotCompanies(topCountriesOfClients, hubspotApiKey);
if (!result) throw new Error('failed to update companies in Hubspot');
console.log(`updated ${result.updated} companies out of ${result.total} comapnies`);
} catch (error) {
console.error('error in getTopCountriesOfClientsHubspot', error);
return;
}
};
module.exports = { getTopCountriesOfClientsHubspot };