aggregateLeadsByEmail() function added in runConnector()

This commit is contained in:
Denise_Datinguinoo committed 2026-08-25 12:37:25 +02:00
1 parent c040f40b04
commit 1b7cd4899e
2 files changed
+33 -25

No files matched your search

+1 -1
View File
@@ -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);
+32 -24
View File
@@ -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,16 +99,12 @@ 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));
@@ -117,7 +113,7 @@ async function createHubspotContacts(leadContacts) {
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,14 +168,26 @@ 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);
const hubspotContacts = await createHubspotContacts(allPayloads);
console.log('HubSpot Batch Response:', hubspotContacts);
// here we save the last time we ran the connector
@@ -194,9 +207,4 @@ async function runConnector() {
}
}
// Run automatically ONLY if executed directly from terminal
if (require.main === module) {
runConnector();
}
module.exports = initializeHubspotConnection;