From 00553e77fb15599b49406cb5572dad9e63573c1d Mon Sep 17 00:00:00 2001 From: "Denise D." Date: Thu, 20 Aug 2026 09:49:30 +0200 Subject: [PATCH] createContacts.js created --- src/createContacts.js | 26 ++++++++++++++++++++++++++ src/fetchLeads.js | 3 ++- src/hublib.js | 6 +++--- src/hubspotConnector.js | 34 +++++++--------------------------- 4 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 src/createContacts.js diff --git a/src/createContacts.js b/src/createContacts.js new file mode 100644 index 0000000..a35521e --- /dev/null +++ b/src/createContacts.js @@ -0,0 +1,26 @@ +const hubspotApi = require('./hublib'); + +// this is the function where we create the Contacts in Hubspot +async function createHubspotContacts(leadContact) { + console.log('SUCCESSFUL!'); + /*const payload = { + properties: { + firstname: leadContact.firstname, + lastname: leadContact.lastname, + email: leadContact.contact.email, + phone: leadContact.contact.phone, + lead_source: 'Keystone' + } + }; + + try { + const creationContact = await hubspotApi.createCRMRecord('contacts', payload); + console.log(creationContact); + return creationContact; + } + catch (error) { + console.log(`Error in creating the contact with id ${ leadContact.id }`); + }*/ +} + +module.exports = createHubspotContacts; \ No newline at end of file diff --git a/src/fetchLeads.js b/src/fetchLeads.js index 1eac922..cdb0fc2 100644 --- a/src/fetchLeads.js +++ b/src/fetchLeads.js @@ -15,6 +15,7 @@ async function getLeads() { body: JSON.stringify({ unsent: 1, // If this variable is set to 0, then it will show leads that have been marked as seen (old leads) test: 1, // If this is removed or set to 0, then all leads that are returned would be marked as seen. This allows you to only fetch new leads + limit: 2 }) }); @@ -41,4 +42,4 @@ async function getLeads() { } } -module.exports = { getLeads }; \ No newline at end of file +module.exports = getLeads; \ No newline at end of file diff --git a/src/hublib.js b/src/hublib.js index 3080e92..bf7e881 100644 --- a/src/hublib.js +++ b/src/hublib.js @@ -2,13 +2,13 @@ var fs = require('fs'); var axios = require('axios'); var express = require('express'); const auth = require('../cfg/auth.js'); -const FILE_PATH = require('./last-modified-date.json'); + const app = express(); const port = 3000; app.use(express.json()); -var server = app.listen(3000, () => { console.log('Please your hubspot instance and next click on http://localhost:3000/install or http://localhost:3000/renew') }) +//var server = app.listen(3000, () => { console.log('Please your hubspot instance and next click on http://localhost:3000/install or http://localhost:3000/renew') }) // used to initiate the authorization flow to connect the app with Hubspot app.get('/install', async (req, res) => { @@ -69,7 +69,7 @@ app.get('/oauth-callback', async (req, res) => { } token.issued_at=Date.now(); - fs.writeFileSync( FILE_PATH, JSON.stringify({ lastRun: token }, null, 2)); + fs.writeFileSync( configPath, JSON.stringify(token)); console.log(token); res.send(`ok`); diff --git a/src/hubspotConnector.js b/src/hubspotConnector.js index d222f38..aabb687 100644 --- a/src/hubspotConnector.js +++ b/src/hubspotConnector.js @@ -1,5 +1,6 @@ const fs = require('fs'); -const { getLeads } = require('./fetchLeads.js'); +const getLeads = require('./fetchLeads.js'); +const createHubspotContacts = require('./createContacts.js'); const FILE_PATH = './last-modified-date.json'; const auth = require('../cfg/auth.js'); const hubspotApi = require('./hublib.js'); @@ -24,30 +25,6 @@ async function initializeHubspotConnection() { console.log('Hubspot connection is ready!'); } -// this is the function where we create the Contacts in Hubspot -async function createHubspotContacts(leadContact) { - console.log('Connection successful!'); - - /*const payload = { - properties: { - firstname: leadContact.firstname, - lastname: leadContact.lastname, - email: leadContact.contact.email, - phone: leadContact.contact.phone, - lead_source: 'Keystone' - } - }; - - try { - const creationContact = await hubspotApi.createCRMRecord('contacts', payload); - console.log(creationContact); - return creationContact; - } - catch (error) { - console.log(`Error in creating the contact with id ${ leadContact.id }`); - }*/ -} - // this is the function that runs the connector async function runConnector() { try { @@ -58,10 +35,13 @@ async function runConnector() { console.log('No data retrieved.'); } + console.log(leads); console.log('Data retrieved successfully! Connecting to Hubspot...'); + // connection to Hubspot await initializeHubspotConnection(); - /*for (const lead of leads) { + console.log('Connection successful!'); + for (const lead of leads) { try { const hubspotContact = await createHubspotContacts(lead); if (hubspotContact) { @@ -76,7 +56,7 @@ async function runConnector() { } } - console.log('All contacts have been created!');*/ + console.log('All contacts have been created!'); // here we track the last time we ran the connector by passing it to a file json if (fs.existsSync(FILE_PATH)) {