createContacts.js created

This commit is contained in:
Denise_Datinguinoo committed 2026-08-20 09:49:30 +02:00
1 parent 4895f4df8b
commit 00553e77fb
4 files changed
+38 -31

No files matched your search

+26
View File
@@ -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;
+2 -1
View File
@@ -15,6 +15,7 @@ async function getLeads() {
body: JSON.stringify({ 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) 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 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 }; module.exports = getLeads;
+3 -3
View File
@@ -2,13 +2,13 @@ var fs = require('fs');
var axios = require('axios'); var axios = require('axios');
var express = require('express'); var express = require('express');
const auth = require('../cfg/auth.js'); const auth = require('../cfg/auth.js');
const FILE_PATH = require('./last-modified-date.json');
const app = express(); const app = express();
const port = 3000; const port = 3000;
app.use(express.json()); 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 // used to initiate the authorization flow to connect the app with Hubspot
app.get('/install', async (req, res) => { app.get('/install', async (req, res) => {
@@ -69,7 +69,7 @@ app.get('/oauth-callback', async (req, res) => {
} }
token.issued_at=Date.now(); token.issued_at=Date.now();
fs.writeFileSync( FILE_PATH, JSON.stringify({ lastRun: token }, null, 2)); fs.writeFileSync( configPath, JSON.stringify(token));
console.log(token); console.log(token);
res.send(`ok`); res.send(`ok`);
+7 -27
View File
@@ -1,5 +1,6 @@
const fs = require('fs'); 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 FILE_PATH = './last-modified-date.json';
const auth = require('../cfg/auth.js'); const auth = require('../cfg/auth.js');
const hubspotApi = require('./hublib.js'); const hubspotApi = require('./hublib.js');
@@ -24,30 +25,6 @@ async function initializeHubspotConnection() {
console.log('Hubspot connection is ready!'); 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 // this is the function that runs the connector
async function runConnector() { async function runConnector() {
try { try {
@@ -58,10 +35,13 @@ async function runConnector() {
console.log('No data retrieved.'); console.log('No data retrieved.');
} }
console.log(leads);
console.log('Data retrieved successfully! Connecting to Hubspot...'); console.log('Data retrieved successfully! Connecting to Hubspot...');
// connection to Hubspot // connection to Hubspot
await initializeHubspotConnection(); await initializeHubspotConnection();
/*for (const lead of leads) { console.log('Connection successful!');
for (const lead of leads) {
try { try {
const hubspotContact = await createHubspotContacts(lead); const hubspotContact = await createHubspotContacts(lead);
if (hubspotContact) { 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 // here we track the last time we ran the connector by passing it to a file json
if (fs.existsSync(FILE_PATH)) { if (fs.existsSync(FILE_PATH)) {