diff --git a/src/hubspotConnector.js b/src/hubspotConnector.js index 6be798f..74fe11e 100644 --- a/src/hubspotConnector.js +++ b/src/hubspotConnector.js @@ -66,7 +66,7 @@ function mapFields(lead) { for (const [hsProperty, getValue] of Object.entries(fields)) { let keystoneValue = getValue(lead); - if (keystoneValue !== undefined && keystoneValue !== null) { + if (keystoneValue !== undefined && keystoneValue !== null && keystoneValue !== '') { obj.properties[hsProperty] = keystoneValue; } } @@ -105,29 +105,22 @@ async function createHubspotContacts(leadContacts) { const leadsArray = Array.isArray(leadContacts) ? leadContacts : [leadContacts]; const payloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead)); - let payLoadsTest = payloads.slice(0, 20); - const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payLoadsTest); - if (creationContacts) { - console.log('Batches successfully created!'); - console.log(JSON.stringify(creationContacts, null, 2)); - return creationContacts; - } else { - return 'Error in creating the contacts in Hubspot'; - } - /*for (let i = 0; i < leadContacts.length; i += 100) { + for (let i = 0; i < leadContacts.length; i += 100) { let payloadsBatch = payloads.slice(i, i + 100); - const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payLoadsTest); + let payloadTest = [payloads[0]]; + const creationContacts = await hubspot.upsertBatchCRMRecord('contacts', payloadTest); if (creationContacts) { - console.log('Batch successfully created!'); - console.log(JSON.stringify(creationContacts)); + console.log('Batch successfully created!'); + console.log(JSON.stringify(creationContacts, null, 2)); } else { - return null; + throw new Error(`Failure in sending batches from index ${ i }`); } - }*/ + } + return true; } catch (error) { - console.error('Hubspot Error 400:', JSON.stringify(error.response?.data, null, 2) || error.message); + console.error('Hubspot Error 400: ', JSON.stringify(error.response?.data, null, 2) || error.message); throw error; } } @@ -135,6 +128,7 @@ async function createHubspotContacts(leadContacts) { // this is the function that runs the connector async function runConnector() { const isDryRun = process.argv.includes('--dryRun') || process.argv.includes('--dryrun'); + const isTestMode = process.argv.includes('--isTest') || process.argv.includes('--istest'); // here we consider the last time we ran the connector let lastRunDate = ''; // we will use this to simplify the management of the leads @@ -154,7 +148,7 @@ async function runConnector() { try { console.log('Retrieving data from Keystone...'); // getLeads(lastRunDate, currentDate, unsent, test) - const leads = await getLeads(lastRunDate, currentDate, 1, 1); + const leads = await getLeads(lastRunDate, currentDate, 0, 1); if (!leads.length) { console.log('No data retrieved.'); @@ -184,16 +178,19 @@ async function runConnector() { console.log('HubSpot Batch Response:', hubspotContacts); // here we save the last time we ran the connector - /*if (hubspotContacts) { + 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!'); } } catch (error) { - console.error('Error in processing data: ', error); + console.error('Error in processing data: ', error.message); } }