aggregateLeadsByEmail() function added

This commit is contained in:
Denise_Datinguinoo committed 2026-08-24 12:51:27 +02:00
1 parent 751a60a301
commit a719252dd7
1 file changed
+68 -13
+68 -13
View File
@@ -37,13 +37,23 @@ let fields = {
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 ?? '',
nazionalita__c: (lead) => {
const country = lead.contact?.nationality_country?.country_name;
return typeof country === 'string' ? country.toUpperCase() : '';
},
corso_keystone: (lead) => lead.programList?.join('; ') ?? '',
leadsource: () => 'keystone'
};
function mapFields(lead) {
let obj = { properties: {} };
const email = lead.contact?.email;
if (!email) return null;
let obj = {
id: email,
idProperty: 'email',
properties: {}
};
for (const [hsProperty, getValue] of Object.entries(fields)) {
let keystoneValue = getValue(lead);
@@ -54,15 +64,55 @@ function mapFields(lead) {
return obj;
}
function aggregateLeadsByEmail(leads) {
const aggregatedLeads = new Map();
for (const lead of leads) {
const email = lead.contact?.email;
let programName = lead.program?.name;
if (!email) continue;
if (!aggregatedLeads.has(email)) {
aggregatedLeads.set(email, {
...lead,
programList: programName ? [programName] : []
})
} else {
const existingLead = aggregatedLeads.get(email);
if (programName && !existingLead.programList.includes(programName)) {
existingLead.programList.push(programName);
}
}
}
return Array.from(aggregatedLeads.values());
}
// 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 payloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead));
const creationContact = await hubspot.createBatchCRMRecord('contacts', payloads);
return creationContact;
let payLoadsTest = payloads.slice(0, 20);
const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payLoadsTest);
if (creationContacts) {
console.log('Batches successfully created!');
console.log(JSON.stringify(creationContacts, null, 2));
} else {
return 'Error in creating the contacts in Hubspot';
}
/*for (let i = 0; i < leadContacts.length; i += 100) {
let payloadsBatch = payloads.slice(i, i + 100);
const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payLoadsTest);
if (creationContacts) {
console.log('Batch successfully created!');
console.log(JSON.stringify(creationContacts));
}
else {
return null;
}
}*/
}
catch (error) {
console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message);
@@ -91,6 +141,7 @@ async function runConnector() {
try {
console.log('Retrieving data from Keystone...');
// getLeads(lastRunDate, currentDate, unsent, test)
const leads = await getLeads(lastRunDate, currentDate, 1, 1);
if (!leads.length) {
@@ -100,11 +151,13 @@ async function runConnector() {
console.log('Data retrieved successfully!');
if (isDryRun) {
console.log('--- DRY RUN MODE: No changes will be saved to HubSpot or JSON ---');
console.log('--- DRY RUN MODE: No changes will be saved to HubSpot ---');
console.log('Preview Lead 1:');
console.log(mapFields(leads[0]));
const leadsTest = aggregateLeadsByEmail(leads);
console.log(`ID: ${ leadsTest[0].id }`);
console.log(mapFields(leadsTest[0]));
console.log('Process DRY-RUN completed!')
console.log('Process DRY-RUN completed!');
return;
}
else {
@@ -115,18 +168,20 @@ async function runConnector() {
console.log('Connection successful!');
// Hubspot contacts creation
const hubspotContacts = await createHubspotContacts(leads[1]);
const hubspotContacts = await createHubspotContacts(leads);
console.log('HubSpot Batch Response:', hubspotContacts);
// here we save the last time we ran the connector
/*fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2));
console.log('DateTime saved successfully in file last-modified-date.json');*/
/*if (hubspotContacts) {
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!');
}
}
catch (error) {
console.log('Error in processing data: ', error);
console.error('Error in processing data: ', error);
}
}