improvements about dryRun mode
This commit is contained in:
1 parent
83ccf3ed04
commit
835b7eb918
2 files changed
+39
-40
No files matched your search
+38
-39
@@ -103,8 +103,7 @@ async function createHubspotContacts(leadContactsPayload) {
|
||||
try {
|
||||
for (let i = 0; i < leadContactsPayload.length; i += 100) {
|
||||
let payloadsBatch = leadContactsPayload.slice(i, i + 100);
|
||||
let payloadTest = [leadContactsPayload[0]];
|
||||
const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payloadTest);
|
||||
const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payloadsBatch);
|
||||
if (creationContacts) {
|
||||
console.log('Batch successfully created!');
|
||||
console.log(JSON.stringify(creationContacts, null, 2));
|
||||
@@ -128,7 +127,7 @@ async function runConnector() {
|
||||
|
||||
// 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 of the current datestamp
|
||||
let currentDate = new Date().toISOString(); // we consider only the date of the current datestamp
|
||||
if (fs.existsSync(FILE_PATH)) {
|
||||
const file = fs.readFileSync(FILE_PATH, 'utf8');
|
||||
lastRunDate = JSON.parse(file).lastRun;
|
||||
@@ -150,6 +149,16 @@ async function runConnector() {
|
||||
console.log('No data retrieved.');
|
||||
return;
|
||||
}
|
||||
|
||||
// here we save the last time we ran the connector
|
||||
if (!isTestMode) {
|
||||
// default : 2026-08-18;
|
||||
fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2));
|
||||
console.log('DateTime saved successfully in file last-modified-date.json');
|
||||
} else {
|
||||
console.log('TEST/DRY MODE: Date not modified on JSON file.');
|
||||
}
|
||||
|
||||
console.log('Data retrieved successfully!');
|
||||
|
||||
console.log('Now Connecting to Hubspot...');
|
||||
@@ -157,50 +166,40 @@ async function runConnector() {
|
||||
await initializeHubspotConnection();
|
||||
console.log('Connection successful!');
|
||||
|
||||
// leadsArray accepts either a single lead object or multiple leads(array)
|
||||
const leadsArray = Array.isArray(leads) ? leads : [leads];
|
||||
let allPayloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead));
|
||||
|
||||
if (isDryRun) {
|
||||
console.log('--- DRY RUN MODE: No changes will be saved to HubSpot ---');
|
||||
console.log('Preview Lead 1:');
|
||||
const leadsTest = aggregateLeadsByEmail(leads);
|
||||
console.log(`ID: ${ leadsTest[0].id }`);
|
||||
console.log(mapFields(leadsTest[0]));
|
||||
const leadTest = [allPayloads[0]];
|
||||
console.log(leadTest);
|
||||
|
||||
console.log('Process DRY-RUN completed!');
|
||||
return;
|
||||
}
|
||||
else {
|
||||
/*let nationalityOptions = '';
|
||||
try {
|
||||
const contactsProperties = await hubspot.getObjectProperties('contacts');
|
||||
const fieldName = contactsProperties.results.find(prop => prop.name === 'nazionalita__c');
|
||||
if (fieldName) {
|
||||
nationalityOptions = fieldName?.options.map(prop => prop.value);
|
||||
//console.log(nationalityOptions);
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.error('Error: ', e.message);
|
||||
throw new Error('Error in getting contacts properties');
|
||||
}*/
|
||||
|
||||
// leadsArray accepts either a single lead object or multiple leads(array)
|
||||
const leadsArray = Array.isArray(leads) ? leads : [leads];
|
||||
const allPayloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead));
|
||||
|
||||
// Hubspot contacts creation
|
||||
const hubspotContacts = await createHubspotContacts(allPayloads);
|
||||
console.log('HubSpot Batch Response:', hubspotContacts);
|
||||
|
||||
// here we save the last time we ran the connector
|
||||
if (!isTestMode) {
|
||||
// default : 2026-08-18;
|
||||
fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2));
|
||||
console.log('DateTime saved successfully in file last-modified-date.json');
|
||||
} else {
|
||||
console.log('TEST/DRY MODE: Date not modified on JSON file.');
|
||||
}
|
||||
|
||||
console.log('All contacts have been created!');
|
||||
|
||||
/*let nationalityOptions = '';
|
||||
try {
|
||||
const contactsProperties = await hubspot.getObjectProperties('contacts');
|
||||
const fieldName = contactsProperties.results.find(prop => prop.name === 'nazionalita__c');
|
||||
if (fieldName) {
|
||||
nationalityOptions = fieldName?.options.map(prop => prop.value);
|
||||
//console.log(nationalityOptions);
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.error('Error: ', e.message);
|
||||
throw new Error('Error in getting contacts properties');
|
||||
}*/
|
||||
|
||||
allPayloads = [allPayloads[0]];
|
||||
// Hubspot contacts creation
|
||||
const hubspotContacts = await createHubspotContacts(allPayloads);
|
||||
console.log('HubSpot Batch Response:', hubspotContacts);
|
||||
|
||||
console.log('All contacts have been created!');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error in processing data: ', error.message);
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"lastRun": "2026-08-18"
|
||||
"lastRun": "2026-08-25T14:25:46.866Z"
|
||||
}
|
||||
Reference in new issue
Block a user