improvements in hubspotConnector and in the Keystone library
This commit is contained in:
1 parent
5a2022fd9e
commit
751a60a301
3 files changed
+52
-56
No files matched your search
@@ -1,37 +0,0 @@
|
|||||||
const hubspotApi = require('./hublib');
|
|
||||||
|
|
||||||
// this is the function where we create the Contacts in Hubspot
|
|
||||||
async function createHubspotContacts(leadContact) {
|
|
||||||
const payload = {
|
|
||||||
properties: {
|
|
||||||
firstname: leadContact.firstname,
|
|
||||||
lastname: leadContact.lastname,
|
|
||||||
email: leadContact.contact?.email || '',
|
|
||||||
phone: leadContact.contact?.phone || '',
|
|
||||||
leadsource: 'Riferimento esterno'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*const payloads = leadContacts.map(lead => ({
|
|
||||||
properties : {
|
|
||||||
firstname: lead.firstname,
|
|
||||||
lastname: lead.lastname,
|
|
||||||
email: lead.contact.email,
|
|
||||||
phone: lead.contact.phone,
|
|
||||||
lead_source: 'Keystone'
|
|
||||||
}
|
|
||||||
}))*/
|
|
||||||
|
|
||||||
try {
|
|
||||||
//const creationContact = await hubspotApi.createBatchCRMRecord('contacts', payloads);
|
|
||||||
const creationContact = await hubspotApi.createCRMRecord('contacts', payload);
|
|
||||||
console.log(`THIS CONTACT'S ID IS: ${ creationContact.id }`);
|
|
||||||
return creationContact;
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = createHubspotContacts;
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
|
||||||
|
|
||||||
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
|
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
|
||||||
|
|
||||||
@@ -24,7 +23,7 @@ async function getLeads(lastRunDate, currentDate, unsent, test) {
|
|||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
|
|
||||||
if (!response.ok || !Array.isArray(result?.data)) {
|
if (!response.ok || !Array.isArray(result?.data)) {
|
||||||
console.log('API response empty or invalid: ', result);
|
console.log('API response empty or invalid: ', result.message || response.statusText);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,8 +40,8 @@ async function getLeads(lastRunDate, currentDate, unsent, test) {
|
|||||||
return result.data;
|
return result.data;
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error("Error fetching the data: ", JSON.stringify(error.response?.data, null, 2) || error.message);
|
console.error("Error fetching the data: ", error.message);
|
||||||
return [];
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+49
-15
@@ -1,8 +1,8 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const getLeads = require('./fetchLeadsFromKeystone.js');
|
const getLeads = require('./fetchLeadsFromKeystone.js');
|
||||||
const createHubspotContacts = require('./createContacts.js');
|
|
||||||
const auth = require('../cfg/auth.js');
|
const auth = require('../cfg/auth.js');
|
||||||
const hubspot = require('./hublib.js');
|
const hubspot = require('./hublib.js');
|
||||||
|
const path = require('path');
|
||||||
const FILE_PATH = path.resolve(__dirname, './last-modified-date.json');
|
const FILE_PATH = path.resolve(__dirname, './last-modified-date.json');
|
||||||
|
|
||||||
// this initializes the Hubspot connection
|
// this initializes the Hubspot connection
|
||||||
@@ -29,13 +29,54 @@ async function initializeHubspotConnection() {
|
|||||||
console.log('Hubspot connection is ready!');
|
console.log('Hubspot connection is ready!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this config variable is used to map Hubspot fields to those from Keystone, so that when we have to add or
|
||||||
|
// remove fields sent to Hubspot, we only need to modify the varibale 'fields' without touching the mapping
|
||||||
|
// logic or the main function
|
||||||
|
let fields = {
|
||||||
|
firstname: (lead) => lead.firstname,
|
||||||
|
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 ?? '',
|
||||||
|
leadsource: () => 'keystone'
|
||||||
|
};
|
||||||
|
|
||||||
|
function mapFields(lead) {
|
||||||
|
let obj = { properties: {} };
|
||||||
|
|
||||||
|
for (const [hsProperty, getValue] of Object.entries(fields)) {
|
||||||
|
let keystoneValue = getValue(lead);
|
||||||
|
if (keystoneValue !== undefined && keystoneValue !== null) {
|
||||||
|
obj.properties[hsProperty] = keystoneValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 creationContact = await hubspot.createBatchCRMRecord('contacts', payloads);
|
||||||
|
return creationContact;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// this is the function that runs the connector
|
// this is the function that runs the connector
|
||||||
async function runConnector() {
|
async function runConnector() {
|
||||||
const isDryRun = process.argv.includes('--dryRun') || process.argv.includes('--dryrun');
|
const isDryRun = process.argv.includes('--dryRun') || process.argv.includes('--dryrun');
|
||||||
|
|
||||||
// here we consider the last time we ran the connector
|
// here we consider the last time we ran the connector
|
||||||
let lastRunDate = ''; // we will use this to simplify the management of the leads
|
let lastRunDate = ''; // we will use this to simplify the management of the leads
|
||||||
let currentDate = new Date().toISOString().split('T')[0]; // we consider only the date
|
let currentDate = new Date().toISOString().split('T')[0]; // we consider only the date of the current datestamp
|
||||||
if (fs.existsSync(FILE_PATH)) {
|
if (fs.existsSync(FILE_PATH)) {
|
||||||
const file = fs.readFileSync(FILE_PATH, 'utf8');
|
const file = fs.readFileSync(FILE_PATH, 'utf8');
|
||||||
lastRunDate = JSON.parse(file).lastRun;
|
lastRunDate = JSON.parse(file).lastRun;
|
||||||
@@ -54,19 +95,14 @@ async function runConnector() {
|
|||||||
|
|
||||||
if (!leads.length) {
|
if (!leads.length) {
|
||||||
console.log('No data retrieved.');
|
console.log('No data retrieved.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
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 or JSON ---');
|
||||||
console.log('Preview Lead 1:');
|
console.log('Preview Lead 1:');
|
||||||
console.log('ID: ', leads[0].id);
|
console.log(mapFields(leads[0]));
|
||||||
console.log('First Name: ', leads[0].firstname);
|
|
||||||
console.log('Last Name: ', leads[0].lastname);
|
|
||||||
console.log('Email: ', leads[0].contact?.email ?? 'N/D');
|
|
||||||
console.log('Phone: ', leads[0].contact?.phone ?? 'N/D');
|
|
||||||
console.log('Program: ', leads[0].program?.name ?? 'N/D');
|
|
||||||
|
|
||||||
console.log('Process DRY-RUN completed!')
|
console.log('Process DRY-RUN completed!')
|
||||||
return;
|
return;
|
||||||
@@ -78,15 +114,13 @@ async function runConnector() {
|
|||||||
await initializeHubspotConnection();
|
await initializeHubspotConnection();
|
||||||
console.log('Connection successful!');
|
console.log('Connection successful!');
|
||||||
|
|
||||||
// contacts creation
|
// Hubspot contacts creation
|
||||||
const hubspotContacts = await createHubspotContacts(leads[0]);
|
const hubspotContacts = await createHubspotContacts(leads[1]);
|
||||||
console.log('HubSpot Batch Response:', hubspotContacts);
|
console.log('HubSpot Batch Response:', hubspotContacts);
|
||||||
console.log('Creation successful!');
|
|
||||||
|
|
||||||
// here we save the last time we ran the connector
|
// here we save the last time we ran the connector
|
||||||
//const currentDateTime = new Date().toISOString().split('T')[0];
|
/*fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2));
|
||||||
//fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDateTime }, null, 2));
|
console.log('DateTime saved successfully in file last-modified-date.json');*/
|
||||||
//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!');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user