diff --git a/src/createContacts.js b/src/createContacts.js index 0834c1b..4e1face 100644 --- a/src/createContacts.js +++ b/src/createContacts.js @@ -6,9 +6,8 @@ async function createHubspotContacts(leadContact) { properties: { firstname: leadContact.firstname, lastname: leadContact.lastname, - email: leadContact.contact.email, - phone: leadContact.contact.phone, - lead_source: 'Keystone' + email: leadContact.contact?.email || '', + phone: leadContact.contact?.phone || '' } } @@ -29,7 +28,8 @@ async function createHubspotContacts(leadContact) { return creationContact; } catch (error) { - console.log('Error in creating the contacts.'); + console.error('Dettaglio Errore 400 HubSpot:', JSON.stringify(error.response?.data, null, 2) || error.message); + return null; } } diff --git a/src/fetchLeads.js b/src/fetchLeads.js index fbd8458..38d8046 100644 --- a/src/fetchLeads.js +++ b/src/fetchLeads.js @@ -19,7 +19,7 @@ async function getLeads() { console.log('File not found.'); const defaultDate = new Date(); defaultDate.setDate(defaultDate.getDate() - 7); - lastRunDate = defaultDate.toISOString().split('T')[0]; + lastRunDate = defaultDate.toISOString().split('T')[0]; } const response = await fetch('https://smarthub.keystoneacademic.com/api/rest/getLead', { diff --git a/src/hubspotConnector.js b/src/hubspotConnector.js index b20de77..0bbad5d 100644 --- a/src/hubspotConnector.js +++ b/src/hubspotConnector.js @@ -19,11 +19,9 @@ async function initializeHubspotConnection() { hubspot.setClientSecret(auth.hClientSecret); hubspot.setRedirectUri(auth.hCallback); - if (!await hubspot.getAccessToken()) { - console.log('No token found.') - } - else { - console.log('Token loaded successfully'); + const currentToken = await hubspot.getAccessToken(); + if (!currentToken) { + throw new Error('Refresh token not found in cfg/hubspot.json. Complete the authentication OAuth first via browser.'); } await hubspot.checkToken(); @@ -67,9 +65,9 @@ async function runConnector() { console.log('Creation successful!'); // here we save the last time we ran the connector - const currentDateTime = new Date().toISOString().split('T')[0]; - fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDateTime }, null, 2)); - console.log('DateTime saved successfully in file last-modified-date.json'); + //const currentDateTime = new Date().toISOString().split('T')[0]; + //fs.writeFileSync(FILE_PATH, JSON.stringify({ lastRun: currentDateTime }, null, 2)); + //console.log('DateTime saved successfully in file last-modified-date.json'); console.log('All contacts have been created!'); }