Files
hubspot_project/src/fetchLeadsFromKeystone.js
T

52 lines
2.0 KiB
JavaScript

const path = require('path');
const auth = require('../cfg/auth.js');
// this function retrieve the users' data from Keystone
async function getLeads(lastRunDate, currentDate, unsent, test) {
try {
const response = await fetch('https://smarthub.keystoneacademic.com/api/rest/getLead', {
method: 'POST',
headers: {
'x-api-key': auth.apiKey,
'Accept': 'application/json',
'Content-type': 'application/json'
},
body: JSON.stringify({
after: lastRunDate.substring(0, 10),
before: currentDate.substring(0, 10),
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
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Keystone HTTP Error ${response.status} (${response.statusText}): ${ errorText }`);
}
const result = await response.json();
if (!Array.isArray(result?.data)) {
console.log('API response empty or invalid: ', result?.message || 'No data array found');
return [];
}
/*for (const lead of result.data) {
console.log('ID: ', lead.id);
console.log('First Name: ', lead.firstname);
console.log('Last Name: ', lead.lastname);
console.log('Email: ', lead.contact?.email ?? 'N/D');
console.log('Phone: ', lead.contact?.phone ?? 'N/D');
console.log('Program: ', lead.program?.name ?? 'N/D');
console.log('--------------------------');
}*/
return result.data;
}
catch (error) {
console.error("Error fetching the data: ", error.message);
throw error;
}
}
module.exports = getLeads;