From 7b8ceccf498c99eb62213d5de9cc15cda373a31b Mon Sep 17 00:00:00 2001 From: "Denise D." Date: Fri, 21 Aug 2026 12:46:22 +0200 Subject: [PATCH] other changes --- src/createContacts.js | 7 ++++--- src/fetchLeads.js | 25 +++++-------------------- src/hubspotConnector.js | 32 ++++++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/createContacts.js b/src/createContacts.js index 4e1face..2d116c3 100644 --- a/src/createContacts.js +++ b/src/createContacts.js @@ -7,7 +7,8 @@ async function createHubspotContacts(leadContact) { firstname: leadContact.firstname, lastname: leadContact.lastname, email: leadContact.contact?.email || '', - phone: leadContact.contact?.phone || '' + phone: leadContact.contact?.phone || '', + leadsource: 'Riferimento esterno' } } @@ -24,11 +25,11 @@ async function createHubspotContacts(leadContact) { try { //const creationContact = await hubspotApi.createBatchCRMRecord('contacts', payloads); const creationContact = await hubspotApi.createCRMRecord('contacts', payload); - console.log(creationContact); + console.log(`THIS CONTACT'S ID IS: ${ creationContact.id }`); return creationContact; } catch (error) { - console.error('Dettaglio Errore 400 HubSpot:', JSON.stringify(error.response?.data, null, 2) || error.message); + console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message); return null; } } diff --git a/src/fetchLeads.js b/src/fetchLeads.js index 38d8046..0d8d235 100644 --- a/src/fetchLeads.js +++ b/src/fetchLeads.js @@ -5,23 +5,8 @@ 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() { +async function getLeads(lastRunDate, currentDate, unsent, test) { 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: { @@ -31,9 +16,9 @@ async function getLeads() { }, 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 + before: currentDate, + unsent: unsent, // If this variable is set to 0, then it will show leads that have been marked as seen (old leads) + test: test, // 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 }) }); @@ -62,4 +47,4 @@ async function getLeads() { } } -module.exports = { getLeads, FILE_PATH }; \ No newline at end of file +module.exports = getLeads; \ No newline at end of file diff --git a/src/hubspotConnector.js b/src/hubspotConnector.js index 0bbad5d..b575158 100644 --- a/src/hubspotConnector.js +++ b/src/hubspotConnector.js @@ -1,8 +1,9 @@ const fs = require('fs'); -const { getLeads, FILE_PATH } = require('./fetchLeads.js'); +const getLeads = require('./fetchLeads.js'); const createHubspotContacts = require('./createContacts.js'); const auth = require('../cfg/auth.js'); const hubspot = require('./hublib.js'); +const FILE_PATH = path.resolve(__dirname, './last-modified-date.json'); // this initializes the Hubspot connection async function initializeHubspotConnection() { @@ -31,9 +32,25 @@ async function initializeHubspotConnection() { // this is the function that runs the connector async function runConnector() { const isDryRun = process.argv.includes('--dryRun') || process.argv.includes('--dryrun'); + + // here we consider the last time we ran the connector + 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 + 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]; + } + try { console.log('Retrieving data from Keystone...'); - const leads = await getLeads(); + const leads = await getLeads(lastRunDate, currentDate, 1, 1); if (!leads.length) { console.log('No data retrieved.'); @@ -60,6 +77,8 @@ async function runConnector() { // connection to Hubspot await initializeHubspotConnection(); console.log('Connection successful!'); + + // contacts creation const hubspotContacts = await createHubspotContacts(leads[0]); console.log('HubSpot Batch Response:', hubspotContacts); console.log('Creation successful!'); @@ -69,7 +88,7 @@ async function runConnector() { //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!'); + //console.log('All contacts have been created!'); } } catch (error) { @@ -77,4 +96,9 @@ async function runConnector() { } } -runConnector(); \ No newline at end of file +// Run automatically ONLY if executed directly from terminal +if (require.main === module) { + runConnector(); +} + +module.exports = initializeHubspotConnection; \ No newline at end of file