-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalesforce.js
143 lines (124 loc) · 4.07 KB
/
salesforce.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
const jsforce = require('jsforce');
const jwt = require('jsonwebtoken');
const fs = require('fs');
const path = require('path');
const request = require('request-promise');
require('dotenv').config();
// Load the private key
const privateKey = fs.readFileSync(path.resolve(process.env.PRIVATE_KEY_PATH));
// Construct the JWT
const token = jwt.sign({
// issued at time
"iat": Math.floor(Date.now() / 1000),
// JWT expiration time (10 minute maximum)
"exp": Math.floor(Date.now() / 1000) + (10 * 60),
// issuer
"iss": process.env.SALESFORCE_CLIENT_ID,
// audience
"aud": 'https://login.salesforce.com',
// subject
"sub": process.env.SALESFORCE_USERNAME,
// JWT ID
"jti": Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
}, privateKey, { algorithm: 'RS256' });
console.log('JWT constructed successfully');
// Use jsforce's oauth2 services to get an access token
const oauth2 = new jsforce.OAuth2({
loginUrl: 'https://login.salesforce.com',
clientId: process.env.SALESFORCE_CLIENT_ID,
clientSecret: process.env.SALESFORCE_CLIENT_SECRET
});
console.log('Starting authentication');
const options = {
url: `${oauth2.loginUrl}/services/oauth2/token`,
method: 'POST',
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: token
}
};
let conn;
const authenticate = async () => {
try {
console.log('Starting authentication');
const response = await request(options);
console.log('Authentication successful');
const userInfo = JSON.parse(response);
conn = new jsforce.Connection({
instanceUrl: userInfo.instance_url,
accessToken: userInfo.access_token
});
return conn; // Return the connection object
} catch (error) {
console.error('Error during authentication:', error);
throw error;
}
};
// Function to find a company in Salesforce
const findCompany = async (companyName) => {
try {
let result = await conn.query(`SELECT Id, Name FROM Account WHERE Name='${companyName}'`);
return result.records;
} catch (error) {
console.error(error);
throw error;
}
};
// Function to find contacts for a company in Salesforce
const findContacts = async (companyId) => {
try {
let result = await conn.query(`SELECT Id, Name FROM Contact WHERE AccountId='${companyId}'`);
return result.records;
} catch (error) {
console.error(error);
throw error;
}
};
// Function to create a company in Salesforce,
const createCompany = async (companyName, companyDomain) => {
try {
let account = {
Name: companyName,
Website: companyDomain
};
let result = await conn.sobject("Account").create(account);
return result;
} catch (error) {
console.error(error);
throw error;
}
};
// Function to create a contact in Salesforce
const createContact = async (contactData, companyId) => {
try {
let contact = {
AccountId: companyId,
FirstName: contactData.firstName,
LastName: contactData.lastName ? contactData.lastName : contactData.firstName,
Title: contactData.title,
Email: contactData.email,
LinkedIn_Profile_URL__c: contactData.linkedin_url
};
let result = await conn.sobject("Contact").create(contact);
return result;
} catch (error) {
console.error(error);
throw error;
}
};
// Function to update a company in Salesforce
const updateCompany = async (companyId, description) => {
try {
let account = {
Id: companyId,
Description: description
};
let result = await conn.sobject("Account").update(account);
console.log("Update Result", result)
return result;
} catch (error) {
console.error(error);
throw error;
}
};
module.exports = { authenticate, findCompany, findContacts, createCompany, createContact, updateCompany };