aggregateLeadsByEmail() function added
This commit is contained in:
1 parent
751a60a301
commit
a719252dd7
1 file changed
+68
-13
+68
-13
@@ -37,13 +37,23 @@ let fields = {
|
|||||||
lastname: (lead) => lead.lastname,
|
lastname: (lead) => lead.lastname,
|
||||||
email: (lead) => lead.contact?.email ?? '',
|
email: (lead) => lead.contact?.email ?? '',
|
||||||
phone: (lead) => lead.contact?.phone ?? '',
|
phone: (lead) => lead.contact?.phone ?? '',
|
||||||
nazionalita__c: (lead) => lead.contact?.nationality_country?.country_name?.toUpperCase() ?? '',
|
nazionalita__c: (lead) => {
|
||||||
corso_keystone: (lead) => lead.program?.name ?? '',
|
const country = lead.contact?.nationality_country?.country_name;
|
||||||
|
return typeof country === 'string' ? country.toUpperCase() : '';
|
||||||
|
},
|
||||||
|
corso_keystone: (lead) => lead.programList?.join('; ') ?? '',
|
||||||
leadsource: () => 'keystone'
|
leadsource: () => 'keystone'
|
||||||
};
|
};
|
||||||
|
|
||||||
function mapFields(lead) {
|
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)) {
|
for (const [hsProperty, getValue] of Object.entries(fields)) {
|
||||||
let keystoneValue = getValue(lead);
|
let keystoneValue = getValue(lead);
|
||||||
@@ -54,15 +64,55 @@ function mapFields(lead) {
|
|||||||
return obj;
|
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
|
// this is the function where we create the Contacts in Hubspot
|
||||||
async function createHubspotContacts(leadContacts) {
|
async function createHubspotContacts(leadContacts) {
|
||||||
try {
|
try {
|
||||||
// leadsArray accepts either a single lead object or multiple leads(array)
|
// leadsArray accepts either a single lead object or multiple leads(array)
|
||||||
const leadsArray = Array.isArray(leadContacts) ? leadContacts : [leadContacts];
|
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);
|
let payLoadsTest = payloads.slice(0, 20);
|
||||||
return creationContact;
|
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) {
|
catch (error) {
|
||||||
console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message);
|
console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message);
|
||||||
@@ -91,6 +141,7 @@ async function runConnector() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('Retrieving data from Keystone...');
|
console.log('Retrieving data from Keystone...');
|
||||||
|
// getLeads(lastRunDate, currentDate, unsent, test)
|
||||||
const leads = await getLeads(lastRunDate, currentDate, 1, 1);
|
const leads = await getLeads(lastRunDate, currentDate, 1, 1);
|
||||||
|
|
||||||
if (!leads.length) {
|
if (!leads.length) {
|
||||||
@@ -100,11 +151,13 @@ async function runConnector() {
|
|||||||
console.log('Data retrieved successfully!');
|
console.log('Data retrieved successfully!');
|
||||||
|
|
||||||
if (isDryRun) {
|
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('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;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -115,18 +168,20 @@ async function runConnector() {
|
|||||||
console.log('Connection successful!');
|
console.log('Connection successful!');
|
||||||
|
|
||||||
// Hubspot contacts creation
|
// Hubspot contacts creation
|
||||||
const hubspotContacts = await createHubspotContacts(leads[1]);
|
const hubspotContacts = await createHubspotContacts(leads);
|
||||||
console.log('HubSpot Batch Response:', hubspotContacts);
|
console.log('HubSpot Batch Response:', hubspotContacts);
|
||||||
|
|
||||||
// here we save the last time we ran the connector
|
// here we save the last time we ran the connector
|
||||||
/*fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2));
|
/*if (hubspotContacts) {
|
||||||
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!');
|
//console.log('All contacts have been created!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.log('Error in processing data: ', error);
|
console.error('Error in processing data: ', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user