From 751a60a301374d13478685890e3af70d16a10624 Mon Sep 17 00:00:00 2001 From: "Denise D." Date: Fri, 21 Aug 2026 17:48:30 +0200 Subject: [PATCH] improvements in hubspotConnector and in the Keystone library --- src/createContacts.js | 37 -------------------- src/fetchLeadsFromKeystone.js | 7 ++-- src/hubspotConnector.js | 64 +++++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 56 deletions(-) delete mode 100644 src/createContacts.js diff --git a/src/createContacts.js b/src/createContacts.js deleted file mode 100644 index 2d116c3..0000000 --- a/src/createContacts.js +++ /dev/null @@ -1,37 +0,0 @@ -const hubspotApi = require('./hublib'); - -// this is the function where we create the Contacts in Hubspot -async function createHubspotContacts(leadContact) { - const payload = { - properties: { - firstname: leadContact.firstname, - lastname: leadContact.lastname, - email: leadContact.contact?.email || '', - phone: leadContact.contact?.phone || '', - leadsource: 'Riferimento esterno' - } - } - - /*const payloads = leadContacts.map(lead => ({ - properties : { - firstname: lead.firstname, - lastname: lead.lastname, - email: lead.contact.email, - phone: lead.contact.phone, - lead_source: 'Keystone' - } - }))*/ - - try { - //const creationContact = await hubspotApi.createBatchCRMRecord('contacts', payloads); - const creationContact = await hubspotApi.createCRMRecord('contacts', payload); - console.log(`THIS CONTACT'S ID IS: ${ creationContact.id }`); - return creationContact; - } - catch (error) { - console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message); - return null; - } -} - -module.exports = createHubspotContacts; \ No newline at end of file diff --git a/src/fetchLeadsFromKeystone.js b/src/fetchLeadsFromKeystone.js index 9ca4f8d..eba2341 100644 --- a/src/fetchLeadsFromKeystone.js +++ b/src/fetchLeadsFromKeystone.js @@ -1,5 +1,4 @@ const path = require('path'); -const fs = require('fs'); require('dotenv').config({ path: path.resolve(__dirname, '../.env') }); @@ -24,7 +23,7 @@ async function getLeads(lastRunDate, currentDate, unsent, test) { const result = await response.json(); if (!response.ok || !Array.isArray(result?.data)) { - console.log('API response empty or invalid: ', result); + console.log('API response empty or invalid: ', result.message || response.statusText); return []; } @@ -41,8 +40,8 @@ async function getLeads(lastRunDate, currentDate, unsent, test) { return result.data; } catch (error) { - console.error("Error fetching the data: ", JSON.stringify(error.response?.data, null, 2) || error.message); - return []; + console.error("Error fetching the data: ", error.message); + throw error; } } diff --git a/src/hubspotConnector.js b/src/hubspotConnector.js index 3837381..155e6d9 100644 --- a/src/hubspotConnector.js +++ b/src/hubspotConnector.js @@ -1,8 +1,8 @@ const fs = require('fs'); const getLeads = require('./fetchLeadsFromKeystone.js'); -const createHubspotContacts = require('./createContacts.js'); const auth = require('../cfg/auth.js'); const hubspot = require('./hublib.js'); +const path = require('path'); const FILE_PATH = path.resolve(__dirname, './last-modified-date.json'); // this initializes the Hubspot connection @@ -29,13 +29,54 @@ async function initializeHubspotConnection() { console.log('Hubspot connection is ready!'); } +// this config variable is used to map Hubspot fields to those from Keystone, so that when we have to add or +// remove fields sent to Hubspot, we only need to modify the varibale 'fields' without touching the mapping +// logic or the main function +let fields = { + firstname: (lead) => lead.firstname, + lastname: (lead) => lead.lastname, + email: (lead) => lead.contact?.email ?? '', + phone: (lead) => lead.contact?.phone ?? '', + nazionalita__c: (lead) => lead.contact?.nationality_country?.country_name?.toUpperCase() ?? '', + corso_keystone: (lead) => lead.program?.name ?? '', + leadsource: () => 'keystone' +}; + +function mapFields(lead) { + let obj = { properties: {} }; + + for (const [hsProperty, getValue] of Object.entries(fields)) { + let keystoneValue = getValue(lead); + if (keystoneValue !== undefined && keystoneValue !== null) { + obj.properties[hsProperty] = keystoneValue; + } + } + return obj; +} + +// this is the function where we create the Contacts in Hubspot +async function createHubspotContacts(leadContacts) { + try { + // leadsArray accepts either a single lead object or multiple leads(array) + const leadsArray = Array.isArray(leadContacts) ? leadContacts : [leadContacts]; + const payloads = leadsArray.map(lead => mapFields(lead)); + + const creationContact = await hubspot.createBatchCRMRecord('contacts', payloads); + return creationContact; + } + catch (error) { + console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message); + throw error; + } +} + // this is the function that runs the connector async function runConnector() { const isDryRun = process.argv.includes('--dryRun') || process.argv.includes('--dryrun'); // here we consider the last time we ran the connector let lastRunDate = ''; // we will use this to simplify the management of the leads - let currentDate = new Date().toISOString().split('T')[0]; // we consider only the date + let currentDate = new Date().toISOString().split('T')[0]; // we consider only the date of the current datestamp if (fs.existsSync(FILE_PATH)) { const file = fs.readFileSync(FILE_PATH, 'utf8'); lastRunDate = JSON.parse(file).lastRun; @@ -54,19 +95,14 @@ async function runConnector() { if (!leads.length) { console.log('No data retrieved.'); - return; + return; } console.log('Data retrieved successfully!'); if (isDryRun) { console.log('--- DRY RUN MODE: No changes will be saved to HubSpot or JSON ---'); console.log('Preview Lead 1:'); - console.log('ID: ', leads[0].id); - console.log('First Name: ', leads[0].firstname); - console.log('Last Name: ', leads[0].lastname); - console.log('Email: ', leads[0].contact?.email ?? 'N/D'); - console.log('Phone: ', leads[0].contact?.phone ?? 'N/D'); - console.log('Program: ', leads[0].program?.name ?? 'N/D'); + console.log(mapFields(leads[0])); console.log('Process DRY-RUN completed!') return; @@ -78,15 +114,13 @@ async function runConnector() { await initializeHubspotConnection(); console.log('Connection successful!'); - // contacts creation - const hubspotContacts = await createHubspotContacts(leads[0]); + // Hubspot contacts creation + const hubspotContacts = await createHubspotContacts(leads[1]); console.log('HubSpot Batch Response:', hubspotContacts); - console.log('Creation successful!'); // here we save the last time we ran the connector - //const currentDateTime = new Date().toISOString().split('T')[0]; - //fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDateTime }, null, 2)); - //console.log('DateTime saved successfully in file last-modified-date.json'); + /*fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2)); + console.log('DateTime saved successfully in file last-modified-date.json');*/ //console.log('All contacts have been created!'); }