minor changes
This commit is contained in:
1 parent
e3f87ad7b5
commit
c040f40b04
1 file changed
+18
-21
+18
-21
@@ -66,7 +66,7 @@ function mapFields(lead) {
|
|||||||
|
|
||||||
for (const [hsProperty, getValue] of Object.entries(fields)) {
|
for (const [hsProperty, getValue] of Object.entries(fields)) {
|
||||||
let keystoneValue = getValue(lead);
|
let keystoneValue = getValue(lead);
|
||||||
if (keystoneValue !== undefined && keystoneValue !== null) {
|
if (keystoneValue !== undefined && keystoneValue !== null && keystoneValue !== '') {
|
||||||
obj.properties[hsProperty] = keystoneValue;
|
obj.properties[hsProperty] = keystoneValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,29 +105,22 @@ async function createHubspotContacts(leadContacts) {
|
|||||||
const leadsArray = Array.isArray(leadContacts) ? leadContacts : [leadContacts];
|
const leadsArray = Array.isArray(leadContacts) ? leadContacts : [leadContacts];
|
||||||
const payloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead));
|
const payloads = (aggregateLeadsByEmail(leadsArray)).map(lead => mapFields(lead));
|
||||||
|
|
||||||
let payLoadsTest = payloads.slice(0, 20);
|
for (let i = 0; i < leadContacts.length; i += 100) {
|
||||||
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) {
|
|
||||||
let payloadsBatch = payloads.slice(i, 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) {
|
if (creationContacts) {
|
||||||
console.log('Batch successfully created!');
|
console.log('Batch successfully created!');
|
||||||
console.log(JSON.stringify(creationContacts));
|
console.log(JSON.stringify(creationContacts, null, 2));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return null;
|
throw new Error(`Failure in sending batches from index ${ i }`);
|
||||||
}
|
}
|
||||||
}*/
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
catch (error) {
|
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;
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,6 +128,7 @@ async function createHubspotContacts(leadContacts) {
|
|||||||
// this is the function that runs the connector
|
// this is the function that runs the connector
|
||||||
async function runConnector() {
|
async function runConnector() {
|
||||||
const isDryRun = process.argv.includes('--dryRun') || process.argv.includes('--dryrun');
|
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
|
// here we consider the last time we ran the connector
|
||||||
let lastRunDate = ''; // we will use this to simplify the management of the leads
|
let lastRunDate = ''; // we will use this to simplify the management of the leads
|
||||||
@@ -154,7 +148,7 @@ async function runConnector() {
|
|||||||
try {
|
try {
|
||||||
console.log('Retrieving data from Keystone...');
|
console.log('Retrieving data from Keystone...');
|
||||||
// getLeads(lastRunDate, currentDate, unsent, test)
|
// getLeads(lastRunDate, currentDate, unsent, test)
|
||||||
const leads = await getLeads(lastRunDate, currentDate, 1, 1);
|
const leads = await getLeads(lastRunDate, currentDate, 0, 1);
|
||||||
|
|
||||||
if (!leads.length) {
|
if (!leads.length) {
|
||||||
console.log('No data retrieved.');
|
console.log('No data retrieved.');
|
||||||
@@ -184,16 +178,19 @@ async function runConnector() {
|
|||||||
console.log('HubSpot Batch Response:', hubspotContacts);
|
console.log('HubSpot Batch Response:', hubspotContacts);
|
||||||
|
|
||||||
// here we save the last time we ran the connector
|
// 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));
|
fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDate }, null, 2));
|
||||||
console.log('DateTime saved successfully in file last-modified-date.json');
|
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!');
|
console.log('All contacts have been created!');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.error('Error in processing data: ', error);
|
console.error('Error in processing data: ', error.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user