changes
This commit is contained in:
1 parent
00553e77fb
commit
d037acaa15
5 files changed
+111
-59
No files matched your search
+28
-8
@@ -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);
|
||||
console.log("Error fetching the data: ", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = getLeads;
|
||||
module.exports = { getLeads, FILE_PATH };
|
||||
Reference in new issue
Block a user