aggregateLeadsByEmail() function added in runConnector()
This commit is contained in:
1 parent
c040f40b04
commit
1b7cd4899e
2 files changed
+34
-26
No files matched your search
+1
-1
@@ -377,7 +377,7 @@ var hubspot = {
|
|||||||
const response = await axios.get(`${this.baseurl}/crm/v3/properties/${object}/?archived=false`, {
|
const response = await axios.get(`${this.baseurl}/crm/v3/properties/${object}/?archived=false`, {
|
||||||
headers: { Authorization: `Bearer ${this.cachedToken}` }
|
headers: { Authorization: `Bearer ${this.cachedToken}` }
|
||||||
});
|
});
|
||||||
this.logresult(response.data);
|
//this.logresult(response.data);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error fetching properties:', e.response?.data);
|
console.error('Error fetching properties:', e.response?.data);
|
||||||
|
|||||||
+33
-25
@@ -41,7 +41,7 @@ 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) => {
|
/*nazionalita__c: (lead) => {
|
||||||
const country = lead.contact?.nationality_country?.country_name;
|
const country = lead.contact?.nationality_country?.country_name;
|
||||||
if (!country || typeof country !== 'string') {
|
if (!country || typeof country !== 'string') {
|
||||||
return undefined;
|
return undefined;
|
||||||
@@ -49,7 +49,7 @@ let fields = {
|
|||||||
|
|
||||||
const targetCountry = countryMapping[country] || country;
|
const targetCountry = countryMapping[country] || country;
|
||||||
return targetCountry.toUpperCase();
|
return targetCountry.toUpperCase();
|
||||||
},
|
},*/
|
||||||
corso_keystone: (lead) => lead.programList?.join('; ') ?? '',
|
corso_keystone: (lead) => lead.programList?.join('; ') ?? '',
|
||||||
leadsource: () => 'keystone'
|
leadsource: () => 'keystone'
|
||||||
};
|
};
|
||||||
@@ -73,7 +73,7 @@ function mapFields(lead) {
|
|||||||
return obj;
|
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'
|
// and we put the programs' name into a single field named 'programList'
|
||||||
function aggregateLeadsByEmail(leads) {
|
function aggregateLeadsByEmail(leads) {
|
||||||
const aggregatedLeads = new Map();
|
const aggregatedLeads = new Map();
|
||||||
@@ -99,16 +99,12 @@ function aggregateLeadsByEmail(leads) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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(leadContactsPayload) {
|
||||||
try {
|
try {
|
||||||
// leadsArray accepts either a single lead object or multiple leads(array)
|
for (let i = 0; i < leadContactsPayload.length; i += 100) {
|
||||||
const leadsArray = Array.isArray(leadContacts) ? leadContacts : [leadContacts];
|
let payloadsBatch = leadContactsPayload.slice(i, i + 100);
|
||||||
const payloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead));
|
let payloadTest = [leadContactsPayload[0]];
|
||||||
|
const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payloadsBatch);
|
||||||
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);
|
|
||||||
if (creationContacts) {
|
if (creationContacts) {
|
||||||
console.log('Batch successfully created!');
|
console.log('Batch successfully created!');
|
||||||
console.log(JSON.stringify(creationContacts, null, 2));
|
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 }`);
|
throw new Error(`Failure in sending batches from index ${ i }`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return 'Contact/s created successfully!';
|
||||||
}
|
}
|
||||||
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);
|
||||||
@@ -148,7 +144,7 @@ async function runConnector() {
|
|||||||
try {
|
try {
|
||||||
console.log('Retrieving data from Keystone...');
|
console.log('Retrieving data from Keystone...');
|
||||||
// getLeads(lastRunDate, currentDate, unsent, test)
|
// getLeads(lastRunDate, currentDate, unsent, test)
|
||||||
const leads = await getLeads(lastRunDate, currentDate, 0, 1);
|
const leads = await getLeads(lastRunDate, currentDate, 1, 1);
|
||||||
|
|
||||||
if (!leads.length) {
|
if (!leads.length) {
|
||||||
console.log('No data retrieved.');
|
console.log('No data retrieved.');
|
||||||
@@ -156,6 +152,11 @@ async function runConnector() {
|
|||||||
}
|
}
|
||||||
console.log('Data retrieved successfully!');
|
console.log('Data retrieved successfully!');
|
||||||
|
|
||||||
|
console.log('Now Connecting to Hubspot...');
|
||||||
|
// connection to Hubspot
|
||||||
|
await initializeHubspotConnection();
|
||||||
|
console.log('Connection successful!');
|
||||||
|
|
||||||
if (isDryRun) {
|
if (isDryRun) {
|
||||||
console.log('--- DRY RUN MODE: No changes will be saved to HubSpot ---');
|
console.log('--- DRY RUN MODE: No changes will be saved to HubSpot ---');
|
||||||
console.log('Preview Lead 1:');
|
console.log('Preview Lead 1:');
|
||||||
@@ -167,14 +168,26 @@ async function runConnector() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
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
|
// leadsArray accepts either a single lead object or multiple leads(array)
|
||||||
await initializeHubspotConnection();
|
const leadsArray = Array.isArray(leads) ? leads : [leads];
|
||||||
console.log('Connection successful!');
|
const allPayloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead));
|
||||||
|
|
||||||
// Hubspot contacts creation
|
// Hubspot contacts creation
|
||||||
const hubspotContacts = await createHubspotContacts(leads);
|
const hubspotContacts = await createHubspotContacts(allPayloads);
|
||||||
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
|
||||||
@@ -194,9 +207,4 @@ async function runConnector() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run automatically ONLY if executed directly from terminal
|
runConnector();
|
||||||
if (require.main === module) {
|
|
||||||
runConnector();
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = initializeHubspotConnection;
|
|
||||||
Reference in new issue
Block a user