-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
164 lines (135 loc) · 5.58 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const express = require('express');
const bodyParser = require('body-parser');
const salesforce = require('./salesforce');
const apollo = require('./apollo');
const slack = require('./slack');
const predictLeads = require('./predict_leads');
const app = express();
app.use(bodyParser.json());
app.post('/koala-webhook', async (req, res) => {
try {
await koalaWebhook(req.body);
res.status(200).end();
} catch (error) {
console.error(error);
res.status(500).end();
}
});
const koalaWebhook = async (data) => {
// Authenticate with Salesforce
await salesforce.authenticate();
const webhookData = data[0].notification.ctx;
const companyName = webhookData.account.name;
const companyDomain = webhookData.account.domain;
console.log(companyName)
console.log(companyDomain)
// Follow company in Predict Leads
try {
await predictLeads.followCompany(companyDomain);
} catch (error) {
console.error('Error following company in Predict Leads:', error);
// Handle the error gracefully
// For example, you can return an error response or throw a custom error
}
let companyList;
try {
companyList = await salesforce.findCompany(companyName, companyDomain);
} catch (error) {
console.error('Error finding company in Salesforce:', error);
// Handle the error gracefully
// For example, you can return an error response or throw a custom error
}
// If the company doesn't exist in Salesforce, create a new one
if (companyList.length === 0) {
console.log(`Company ${companyName} not found in Salesforce, creating new company.`);
const newCompany = await salesforce.createCompany(companyName, companyDomain);
companyId = newCompany.id;
} else {
companyId = companyList[0].Id;
console.log(`Company ${companyName} found in Salesforce.`);
}
// Get the Salesforce URL for the company
const salesforceUrl = `${process.env.SALESFORCE_INSTANCE_URL}/${companyId}`;
// Fetch technologies from Predict Leads
// Add technologies to Salesforce
let technologies
try {
technologies = await predictLeads.getTechnologies(companyDomain);
console.log(`Got technologies: `, JSON.stringify(technologies));
const technologyTitles = Object.keys(technologies);
if (technologyTitles.length > 0) {
const technologyText = `*Technologies used*: ${technologyTitles.join(', ')}`;
await salesforce.updateCompany(companyId, technologyText);
}
} catch (error) {
console.error('Error fetching technologies from Predict Leads:', error);
// Handle the error gracefully
// For example, you can return an error response or throw a custom error
}
// Find contacts in Salesforce
let contactList;
try {
contactList = await salesforce.findContacts(companyId);
} catch (error) {
console.error(`Error finding contacts in Salesforce:`, error);
// Handle the error gracefully
// For example, you can return an error response or throw a custom error
}
// If the company has no contacts in Salesforce, fetch contact data from the Apollo API and create new contacts
if (contactList.length === 0) {
console.log(`No contacts found for company ${companyName}, creating new contacts.`);
// Fetch contacts from Apollo API
const apolloContacts = await apollo.getContacts(companyDomain);
for (const contact of apolloContacts) {
try {
await salesforce.createContact(contact, companyId);
} catch (error) {
console.error(`Error creating contact in Salesforce:`, error);
// Handle the error gracefully
// For example, you can return an error response or throw a custom error
}
}
try {
contactList = await salesforce.findContacts(companyId);
} catch (error) {
console.error(`Error finding newly created contacts in Salesforce:`, error);
// Handle the error gracefully
// For example, you can return an error response or throw a custom error
}
}
// Sending Slack alert
const slackChannel = '#bag-getters';
// await slack.sendAlert(slackChannel, companyName, contactList, salesforceUrl, technologies);
console.log(`Successfully handled webhook for ${companyName}.`);
};
app.post('/predict-leads-webhook', async (req, res) => {
try {
await predictLeadsWebhook(req.body);
res.status(200).end();
} catch (error) {
console.error(error);
res.status(500).end();
}
});
const predictLeadsWebhook = async (data) => {
// Authenticate with Salesforce
await salesforce.authenticate();
// Code for handling the webhook data
// Find company
let companyList;
try {
companyList = await salesforce.findCompany(companyName, companyDomain);
} catch (error) {
console.error('Error finding company in Salesforce:', error);
// Handle the error gracefully
// For example, you can return an error response or throw a custom error
}
// Fetch buying intent from Salesforce
// Add buying intent to Salesforce
// Sending Slack alert
// const slackChannel = '#bag-getters';
// await slack.sendAlert(slackChannel, companyName, contactList, salesforceUrl, technologies);
console.log(`Successfully handled webhook for predict leads.`);
};
app.listen(3000, () => console.log('Server running on port 3000'));
module.exports = app;