This commit is contained in:
Denise_Datinguinoo committed 2026-08-20 12:49:12 +02:00
1 parent 00553e77fb
commit d037acaa15
5 files changed
+109 -57

No files matched your search

+15 -5
View File
@@ -2,8 +2,7 @@ const hubspotApi = require('./hublib');
// this is the function where we create the Contacts in Hubspot
async function createHubspotContacts(leadContact) {
console.log('SUCCESSFUL!');
/*const payload = {
const payload = {
properties: {
firstname: leadContact.firstname,
lastname: leadContact.lastname,
@@ -11,16 +10,27 @@ async function createHubspotContacts(leadContact) {
phone: leadContact.contact.phone,
lead_source: 'Keystone'
}
};
}
/*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(creationContact);
return creationContact;
}
catch (error) {
console.log(`Error in creating the contact with id ${ leadContact.id }`);
}*/
console.log('Error in creating the contacts.');
}
}
module.exports = createHubspotContacts;
+27 -7
View File
@@ -1,10 +1,27 @@
const path = require('path');
const fs = require('fs');
const FILE_PATH = path.resolve(__dirname, './last-modified-date.json');
require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
// this function retrieve the users' data from Keystone
async function getLeads() {
try {
// here we consider tha last time we ran the connector
let lastRunDate = ''; // we will use this to simplify the management of the leads
let todayDate = new Date().toISOString().split('T')[0]; // we consider only the date
if (fs.existsSync(FILE_PATH)) {
const file = fs.readFileSync(FILE_PATH, 'utf8');
lastRunDate = JSON.parse(file).lastRun;
}
else {
// fallback date in case the file doesn't exist
console.log('File not found.');
const defaultDate = new Date();
defaultDate.setDate(defaultDate.getDate() - 7);
lastRunDate = defaultDate.toISOString().split('T')[0];
}
const response = await fetch('https://smarthub.keystoneacademic.com/api/rest/getLead', {
method: 'POST',
headers: {
@@ -13,20 +30,21 @@ async function getLeads() {
'Content-type': 'application/json'
},
body: JSON.stringify({
after: lastRunDate,
before: todayDate,
unsent: 1, // If this variable is set to 0, then it will show leads that have been marked as seen (old leads)
test: 1, // If this is removed or set to 0, then all leads that are returned would be marked as seen. This allows you to only fetch new leads
limit: 2
})
});
const result = await response.json();
if (!Array.isArray(result?.data)) {
if (!response.ok || !Array.isArray(result?.data)) {
console.log('API response empty or invalid: ', result);
return;
return [];
}
for (const lead of result.data) {
/*for (const lead of result.data) {
console.log('ID: ', lead.id);
console.log('First Name: ', lead.firstname);
console.log('Last Name: ', lead.lastname);
@@ -34,12 +52,14 @@ async function getLeads() {
console.log('Phone: ', lead.contact?.phone ?? 'N/D');
console.log('Program: ', lead.program?.name ?? 'N/D');
console.log('--------------------------');
}
return result.data || [];
}*/
return result.data;
}
catch (error) {
console.log("Error fetching the data: ", error);
return [];
}
}
module.exports = getLeads;
module.exports = { getLeads, FILE_PATH };
+20 -2
View File
@@ -8,7 +8,7 @@ const app = express();
const port = 3000;
app.use(express.json());
//var server = app.listen(3000, () => { console.log('Please your hubspot instance and next click on http://localhost:3000/install or http://localhost:3000/renew') })
//app.listen(3000, () => { console.log('Please your hubspot instance and next click on http://localhost:3000/install or http://localhost:3000/renew') })
// used to initiate the authorization flow to connect the app with Hubspot
app.get('/install', async (req, res) => {
@@ -611,8 +611,26 @@ var hubspot = {
console.error(`Error searching ${objectType}:`, e.response?.data);
throw e;
}
}
},
newCache: function(maxrecords=100) {
return {
records:[],
maxrecords:maxrecords,
add: function(record) {
this.records.push(record);
if (this.maxrecords>= this.records.length) return false;
return true;
},
isEmpty: function() { return (this.records.length==0); },
content: function() { return this.records; },
reset: function() { this.records=[]; }
}
}
};
module.exports = hubspot;
+46 -42
View File
@@ -1,78 +1,82 @@
const fs = require('fs');
const getLeads = require('./fetchLeads.js');
const { getLeads, FILE_PATH } = require('./fetchLeads.js');
const createHubspotContacts = require('./createContacts.js');
const FILE_PATH = './last-modified-date.json';
const auth = require('../cfg/auth.js');
const hubspotApi = require('./hublib.js');
const hubspot = require('./hublib.js');
// this initializes the Hubspot connection
async function initializeHubspotConnection() {
hubspotApi.setTokenFile(auth.hToken);
hubspotApi.getTokenFromFile();
hubspot.setTokenFile(auth.hToken);
hubspotApi.setClientId(auth.hClientId);
hubspotApi.setClientSecret(auth.hClientSecret);
hubspotApi.setRedirectUri(auth.hCallback);
if (fs.existsSync(auth.hToken)) {
await hubspot.getTokenFromFile();
}
else {
console.log('File auth.hToken not found');
}
if (!await hubspotApi.getAccessToken()) {
hubspot.setClientId(auth.hClientId);
hubspot.setClientSecret(auth.hClientSecret);
hubspot.setRedirectUri(auth.hCallback);
if (!await hubspot.getAccessToken()) {
console.log('No token found.')
}
else {
console.log('Token loaded successfully');
}
await hubspotApi.checkToken();
await hubspot.checkToken();
console.log('Hubspot connection is ready!');
}
// this is the function that runs the connector
async function runConnector() {
const isDryRun = process.argv.includes('--dryRun') || process.argv.includes('--dryrun');
try {
console.log('Retrieving data from Keystone...');
const leads = await getLeads();
if (!leads.length) {
console.log('No data retrieved.');
return;
}
console.log('Data retrieved successfully!');
console.log(leads);
console.log('Data retrieved successfully! Connecting to Hubspot...');
if (isDryRun) {
console.log('--- DRY RUN MODE: No changes will be saved to HubSpot or JSON ---');
console.log('Preview Lead 1:');
console.log('ID: ', leads[0].id);
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');
// connection to Hubspot
await initializeHubspotConnection();
console.log('Connection successful!');
for (const lead of leads) {
try {
const hubspotContact = await createHubspotContacts(lead);
if (hubspotContact) {
console.log(`Contact '${ hubspotContact.firstname } ${ hubspotContact.lastname }' with ID '${ hubspotContact.id }' craeted successfully!`);
}
else {
console.log(`Contact with ID '${ lead.id }' not created.`);
}
}
catch (error) {
console.log('Something went wrong: ', error);
}
}
console.log('All contacts have been created!');
// here we track the last time we ran the connector by passing it to a file json
if (fs.existsSync(FILE_PATH)) {
let lastRunTime = '';
const file = fs.readFileSync(FILE_PATH);
lastRunTime = JSON.parse(file).lastRun; // we will use this to simplify the management of the accounts
console.log('Process DRY-RUN completed!')
return;
}
else {
console.log('File not found.');
}
console.log('Now Connecting to Hubspot...');
const currentTimeStamp = new Date().toISOString();
fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentTimeStamp }, null, 2));
console.log('DateTime saved in file last-modified-date.json');
// connection to Hubspot
await initializeHubspotConnection();
console.log('Connection successful!');
const hubspotContacts = await createHubspotContacts(leads[0]);
console.log('HubSpot Batch Response:', hubspotContacts);
console.log('Creation successful!');
// 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: currentDateTime }, 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);
}
}
runConnector();
+1 -1
View File
@@ -1,3 +1,3 @@
{
"lastRun": "2026-08-10T10:00:00.000Z"
"lastRun": "2026-08-18"
}