From 835b7eb9181b33594043898e9de4418f3105b7d1 Mon Sep 17 00:00:00 2001 From: "Denise D." Date: Tue, 25 Aug 2026 16:42:30 +0200 Subject: [PATCH] improvements about dryRun mode --- src/hubspotConnector.js | 77 ++++++++++++++++++------------------- src/last-modified-date.json | 2 +- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/src/hubspotConnector.js b/src/hubspotConnector.js index f578b77..58512a3 100644 --- a/src/hubspotConnector.js +++ b/src/hubspotConnector.js @@ -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); diff --git a/src/last-modified-date.json b/src/last-modified-date.json index 98b5c40..c1e3dce 100644 --- a/src/last-modified-date.json +++ b/src/last-modified-date.json @@ -1,3 +1,3 @@ { - "lastRun": "2026-08-18" + "lastRun": "2026-08-25T14:25:46.866Z" } \ No newline at end of file