-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
262 lines (240 loc) · 7.08 KB
/
index.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { Client } = require('pg');
const jsforce = require('jsforce');
// utilities
const { asyncMetadataRead } = require('./utils');
// instantiate express application
const server = express();
const port = process.env.PORT || 3000;
// create postgres connection
const pgClient = new Client({
connectionString: 'postgres://dxnmuevmlozhug:a364781679c64da401652538cefdcc567a12c5a7da820c170a52e00d90bba1a9@ec2-23-21-109-177.compute-1.amazonaws.com:5432/dcb5fr2bqduukd',
ssl: true
});
// create salesforce connection
console.log('Creating salesforce connections...');
const jsforceConn = new jsforce.Connection();
const jsforceAdminConn = new jsforce.Connection();
jsforceAdminConn.login('[email protected]', 'YouReallyCool1!QS58osVbH4Du7wQz9w2bvHQn', (error, userInfo) => {
if (error) return console.error('Admin failed to connected');
console.log('Admin successfully connected...');
})
// connect express with middleware
server.use(bodyParser.urlencoded({ extended: false }));
server.use(bodyParser.json());
server.use(cors());
// Attach routes to express server
server.get('/', (req, res) => { res.send('Wynger API') });
// User Login
server.post('/login', (req, res) => {
const username = req.body.username;
const passwordPlusToken = req.body.passwordPlusToken;
jsforceConn.login(username, passwordPlusToken, (error, userInfo) => {
// error logging user in
if (error) {
console.error(error);
res.status(400).json({
result: 'error',
data: null,
error: 'Failed to login'
});
}
pgClient
.query(
'SELECT * FROM salesforce.user WHERE sfid = $1',
[userInfo.id]
)
.then(userData => {
// user successfully logged in
res.status(200).json({
result: 'success',
data: {
user: userData.rows[0],
accessToken: jsforceConn.accessToken
},
error: null
});
})
.catch(e => {
res.status(400).json({
result: 'error',
data: null,
error: 'Failed to fetch user from DB'
});
});
});
});
// User Logout
server.post('/logout', (req, res) => {
jsforceConn.logout((error) => {
if (error) {
res.status(400).json({
result: 'error',
data: null,
error: 'Failed to log user out'
});
return console.error('Failed to log user out: ', error);
}
console.log('Logout successful');
res.status(200).json({
result: 'success',
data: null,
error: null
});
});
})
// Accounts Screen -- List Views, Fields
server.get('/accounts_screen', async (req, res) => {
try {
const metadata = await asyncMetadataRead(jsforceAdminConn, 'Account');
//TODO: Make accounts user specific (access/permissions)
const accountsData = await pgClient.query('SELECT * FROM salesforce.account');
res.status(200).json({
result: 'success',
data: {
metadata: {
fields: metadata.fields,
label: metadata.label,
listViews: metadata.listViews
},
accounts: accountsData.rows,
},
error: null
});
} catch (error) {
console.error('ERROR -- GET /accounts_screen: ', error);
res.status(400).json({
result: 'error',
data: null,
error: 'Failed to fetch account metadata'
});
}
});
// Account Details Screen -- Related Lists (Contacts, Cases, Opportunities)
server.get('/account_details_screen/:accountId', async (req, res) => {
try {
const accountId = req.params.accountId;
// get contacts for account
const contactsData = await pgClient.query(
'SELECT * FROM salesforce.contact WHERE accountid = $1',
[accountId]
);
// get cases for account
const casesData = await pgClient.query(
'SELECT * FROM salesforce.case WHERE accountid = $1',
[accountId]
);
// get opportunities for account
const opsData = await pgClient.query(
'SELECT * FROM salesforce.opportunity WHERE accountid = $1',
[accountId]
);
res.status(200).json({
status: 'success',
data: {
contacts: contactsData.rows,
cases: casesData.rows,
opportunities: opsData.rows,
},
error: null
});
} catch (error) {
console.error(error);
res.status(400).json({
result: 'error',
data: null,
error: 'Failed to fetch account details page data'
});
}
})
// Products Screen -- List Views, Fields
server.get('/products_screen', async (req, res) => {
try {
const metadata = await asyncMetadataRead(jsforceAdminConn, 'Product2');
//TODO: Make products user specific (access/permissions)
const productsData = await pgClient.query('SELECT * FROM salesforce.product2');
res.status(200).json({
result: 'success',
data: {
metadata: {
fields: metadata.fields,
label: metadata.label,
listViews: metadata.listViews
},
products: productsData.rows,
},
error: null
});
} catch (error) {
console.error('ERROR -- GET /products_screen: ', error);
res.status(400).json({
result: 'error',
data: null,
error: 'ERROR -- GET /products_screen'
});
}
});
// Product Details Screen -- Related Lists (Price Book Entries)
server.get('/product_details_screen/:productId', async (req, res) => {
try {
const productId = req.params.productId;
const pricebookEntryData = await pgClient.query(
'SELECT * FROM salesforce.pricebookentry WHERE product2id = $1',
[productId]
);
res.status(200).json({
result: 'success',
data: pricebookEntryData.rows,
error: null
});
} catch (error) {
console.error('ERROR -- GET /product_details_screen: ', error);
res.status(400).json({
result: 'error',
data: null,
error: 'Failed to fetch pricebook entries for product'
});
}
});
// Case Details Page -- Related Lists (Case History, Case Comments)
// TODO: Retrieve metadata (Fields)
server.get('/case_details_screen/:caseId', async (req, res) => {
try {
const caseId = req.params.caseId;
// get case history for account
const caseHistoryData = await pgClient.query(
'SELECT * from salesforce.casehistory WHERE caseid = $1',
[caseId]
);
// get solutions for account
const caseCommentData = await pgClient.query(
'SELECT * from salesforce.casecomment WHERE parentid = $1',
[caseId]
);
res.status(200).json({
result: 'success',
data: {
caseHistory: caseHistoryData.rows,
caseComments: caseCommentData.rows
},
error: null
});
} catch (error) {
console.error('ERROR -- GET /case_details_screen: ', error);
res.status(400).json({
result: 'error',
data: null,
error: ''
})
}
});
// Opportunity Details Page -- Retrieve metadata (Fields)
server.get('/opportunity_details_page/:opId', async (req, res) => {
try {} catch (error) {}
});
server.listen(port, () => {
pgClient.connect();
console.log(`Wynger API running on http://localhost:${port}`);
});