diff --git a/src/hublib.js b/src/hublib.js index 0609f09..de58822 100644 --- a/src/hublib.js +++ b/src/hublib.js @@ -377,7 +377,7 @@ var hubspot = { const response = await axios.get(`${this.baseurl}/crm/v3/properties/${object}/?archived=false`, { headers: { Authorization: `Bearer ${this.cachedToken}` } }); - this.logresult(response.data); + //this.logresult(response.data); return response.data; } catch (e) { console.error('Error fetching properties:', e.response?.data); diff --git a/src/hubspotConnector.js b/src/hubspotConnector.js index 74fe11e..3c09312 100644 --- a/src/hubspotConnector.js +++ b/src/hubspotConnector.js @@ -41,7 +41,7 @@ let fields = { lastname: (lead) => lead.lastname, email: (lead) => lead.contact?.email ?? '', phone: (lead) => lead.contact?.phone ?? '', - nazionalita__c: (lead) => { + /*nazionalita__c: (lead) => { const country = lead.contact?.nationality_country?.country_name; if (!country || typeof country !== 'string') { return undefined; @@ -49,7 +49,7 @@ let fields = { const targetCountry = countryMapping[country] || country; return targetCountry.toUpperCase(); - }, + },*/ corso_keystone: (lead) => lead.programList?.join('; ') ?? '', leadsource: () => 'keystone' }; @@ -73,7 +73,7 @@ function mapFields(lead) { return obj; } -// in Keystone there are duplicate leads but with different program name, so in here we aggregate the same leads +// in Keystone there are duplicate leads with different program name, so in here we aggregate the same leads // and we put the programs' name into a single field named 'programList' function aggregateLeadsByEmail(leads) { const aggregatedLeads = new Map(); @@ -99,25 +99,21 @@ function aggregateLeadsByEmail(leads) { } // this is the function where we create the Contacts in Hubspot -async function createHubspotContacts(leadContacts) { +async function createHubspotContacts(leadContactsPayload) { try { - // leadsArray accepts either a single lead object or multiple leads(array) - const leadsArray = Array.isArray(leadContacts) ? leadContacts : [leadContacts]; - const payloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead)); - - for (let i = 0; i < leadContacts.length; i += 100) { - let payloadsBatch = payloads.slice(i, i + 100); - let payloadTest = [payloads[0]]; - const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payloadTest); + for (let i = 0; i < leadContactsPayload.length; i += 100) { + let payloadsBatch = leadContactsPayload.slice(i, i + 100); + let payloadTest = [leadContactsPayload[0]]; + const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payloadsBatch); if (creationContacts) { console.log('Batch successfully created!'); - console.log(JSON.stringify(creationContacts, null, 2)); + console.log(JSON.stringify(creationContacts, null, 2)); } else { throw new Error(`Failure in sending batches from index ${ i }`); } } - return true; + return 'Contact/s created successfully!'; } catch (error) { console.error('Hubspot Error 400: ', JSON.stringify(error.response?.data, null, 2) || error.message); @@ -148,7 +144,7 @@ async function runConnector() { try { console.log('Retrieving data from Keystone...'); // getLeads(lastRunDate, currentDate, unsent, test) - const leads = await getLeads(lastRunDate, currentDate, 0, 1); + const leads = await getLeads(lastRunDate, currentDate, 1, 1); if (!leads.length) { console.log('No data retrieved.'); @@ -156,6 +152,11 @@ async function runConnector() { } console.log('Data retrieved successfully!'); + console.log('Now Connecting to Hubspot...'); + // connection to Hubspot + await initializeHubspotConnection(); + console.log('Connection successful!'); + if (isDryRun) { console.log('--- DRY RUN MODE: No changes will be saved to HubSpot ---'); console.log('Preview Lead 1:'); @@ -167,21 +168,33 @@ async function runConnector() { return; } else { - console.log('Now Connecting to Hubspot...'); + /*let nationalityOptions = ''; + try { + const contactsProperties = await hubspot.getObjectProperties('contacts'); + const fieldName = contactsProperties.results.find(prop => prop.name === 'nazionalita__c'); + if (fieldName) { + nationalityOptions = fieldName?.options.map(prop => prop.value); + //console.log(nationalityOptions); + } + } + catch (e) { + console.error('Error: ', e.message); + throw new Error('Error in getting contacts properties'); + }*/ - // connection to Hubspot - await initializeHubspotConnection(); - console.log('Connection successful!'); + // leadsArray accepts either a single lead object or multiple leads(array) + const leadsArray = Array.isArray(leads) ? leads : [leads]; + const allPayloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead)); // Hubspot contacts creation - const hubspotContacts = await createHubspotContacts(leads); - console.log('HubSpot Batch Response:', hubspotContacts); + const hubspotContacts = await createHubspotContacts(allPayloads); + console.log('HubSpot Batch Response:', hubspotContacts); // here we save the last time we ran the connector if (!isTestMode) { - // default : 2026-08-18; + // default : 2026-08-18; fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2)); - console.log('DateTime saved successfully in file last-modified-date.json'); + console.log('DateTime saved successfully in file last-modified-date.json'); } else { console.log('TEST/DRY MODE: Date not modified on JSON file.'); } @@ -194,9 +207,4 @@ async function runConnector() { } } -// Run automatically ONLY if executed directly from terminal -if (require.main === module) { - runConnector(); -} - -module.exports = initializeHubspotConnection; \ No newline at end of file +runConnector(); \ No newline at end of file