other changes
This commit is contained in:
1 parent
bb01ec92ce
commit
7b8ceccf49
3 files changed
+37
-27
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-20
@@ -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 };
|
||||
module.exports = getLeads;
|
||||
+28
-4
@@ -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();
|
||||
// Run automatically ONLY if executed directly from terminal
|
||||
if (require.main === module) {
|
||||
runConnector();
|
||||
}
|
||||
|
||||
module.exports = initializeHubspotConnection;
|
||||
Reference in new issue
Block a user