-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
323 lines (258 loc) · 7.58 KB
/
server.js
File metadata and controls
323 lines (258 loc) · 7.58 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import express from 'express'
import cors from 'cors'
import dotenv from 'dotenv'
import cities from './data/cities.js'
import visas from './data/visaTypes.js'
import validator from 'express-validator'
import axios from 'axios'
import { getUserId } from './helpers.js'
dotenv.config()
const { body, validationResult } = validator
const port = process.env.PORT || 8000
const baseURL = process.env.CAMUNDA_BASE_URL
const app = express()
app.use(cors())
app.use(express.json())
// GET /api/cities
app.get('/api/cities', (req, res) => {
res.status(200).json(cities)
})
// GET /api/cities/:id
app.get('/api/cities/:id', (req, res) => {
const foundCity = cities.filter((city) => city._id === req.params.id)[0]
if (!foundCity) {
res.status(404).json({
message: 'No city with the given id was found :/',
})
}
res.status(200).json(foundCity)
})
// GET /api/visas
app.get('/api/visas', (req, res) => {
res.status(200).json(visas)
})
// GET /api/visas/:id
app.get('/api/visas/:id', (req, res) => {
const foundVisa = visas.filter((visa) => visa._id === req.params.id)[0]
if (!foundVisa) {
res.status(404).json({
message: 'No visa type with the given id was found :/',
})
}
res.status(200).json(foundVisa)
})
// POST /api/signup
app.post(
'/api/signup',
[
body('email').isEmail().withMessage('Invalid email'),
body('firstName')
.isLength({ max: 15 })
.withMessage('Must be between 5-15 characters'),
body('lastName')
.isLength({ max: 15 })
.withMessage('Must be between 5-15 characters'),
body('password')
.isLength({ min: 8 })
.withMessage('Passsword must be atleast 8 characters long'),
body('city').exists().withMessage('Required Field!'),
body('visa').exists().withMessage('Required Field!'),
],
async (req, res) => {
const result = validationResult(req)
const { city, visa, email, firstName, lastName, password } = req.body
const userId = getUserId(email)
if (!result.isEmpty()) res.status(400).send(result)
// TODO: Make API calls to Camunda here
const variableRequest = {
variables: {
city: {
value: city,
type: 'String',
},
visa: {
value: visa,
type: 'String',
},
},
}
const userRequest = {
profile: {
id: userId,
email,
firstName,
lastName,
},
credentials: { password },
}
try {
// Start new Prcoess
const processResponse = await axios.post(
baseURL +
'/process-definition/key/TwoPageForm/tenant-id/TwoFormDiagram/start',
variableRequest
)
var processInstanceId = processResponse.data.id
// Create a new User
await axios.post(baseURL + '/user/create', userRequest)
// Fetch the latest pending task from the new process
const taskResponse = await axios.get(
baseURL + `/task?processInstanceId=${processInstanceId}`
)
var taskId = taskResponse.data[0].id
// Assign the task to the user (auditing reasons)
await axios.post(baseURL + `/task/${taskId}/assignee`, {
userId,
})
// Mark the first task as complete
await axios.post(baseURL + `/task/${taskId}/complete`, {})
// Fetch the next task
const newProcessResponse = await axios.get(
baseURL + `/task?processInstanceId=${processInstanceId}`
)
var newTaskId = newProcessResponse.data[0].id
// Assign it tp the user
await axios.post(baseURL + `/task/${newTaskId}/assignee`, {
userId,
})
} catch (error) {
res.status(500).json(error)
}
// Returns the users ID, the process Instance ID and the new task's ID
res.status(200).json({ userId, processInstanceId, newTaskId })
}
)
// POST /api/login
app.post(
'/api/login',
[
body('email').isEmail().withMessage('Invalid email'),
body('password')
.isLength({ min: 8 })
.withMessage('Passsword must be atleast 8 characters long'),
],
async (req, res) => {
const result = validationResult(req)
if (!result.isEmpty()) res.status(400).send(result)
const { email, password } = req.body
const userId = getUserId(email)
// TODO: Make API calls to Camunda here
try {
// Verify users credentials
const verifyResponse = await axios.post(baseURL + '/identity/verify', {
username: userId,
password,
})
// Send a 400 response if user is not authenticated 😤
if (!verifyResponse.data.authenticated)
res.status(400).json({ message: 'User not authenticated' })
// Look for tasks assigned to user
const taskResponse = await axios.get(baseURL + `/task?assignee=${userId}`)
// Return empty response if no tasks are pending
if (!taskResponse.data || !taskResponse.data.length)
res.status(200).json({ email })
const { id: taskId, taskDefinitionKey } = taskResponse.data[0]
// return the latest pending task id and definition key
res.status(200).json({ email, taskId, taskDefinitionKey })
} catch (error) {
res.status(500).json(error)
}
}
)
// POST /api/profile
app.post(
'/api/profile',
[body('email').isEmail().withMessage('Invalid email')],
async (req, res) => {
const result = validationResult(req)
if (!result.isEmpty()) res.status(400).send(result)
const userId = getUserId(req.body.email)
try {
var [taskResponse, userResponse] = await Promise.all([
await axios.get(baseURL + `/task?assignee=${userId}`),
await axios.get(baseURL + `/user/${userId}/profile`),
])
if (!taskResponse.data[0])
res.status(404).json({ message: 'This user has no pending tasks' })
const { id: taskId } = taskResponse.data[0]
// res.send(baseURL + `/task/${taskId}/variables`)
var variablesResponse = await axios.get(
baseURL + `/task/${taskId}/variables`
)
} catch (error) {
console.log({ error })
res.status(500).json(error)
}
const city = variablesResponse.data.city.value
const visa = variablesResponse.data.visa.value
const { value: uploadedEid } = variablesResponse.data.eidUploaded || {
value: false,
}
const { value: uploadedPassport } = variablesResponse.data
.passportUploaded || { value: false }
const { email, firstName, lastName } = userResponse.data
res.status(200).json({
email,
firstName,
lastName,
city,
visa,
uploadedEid,
uploadedPassport,
})
}
)
// POST /api/upload
app.post(
'/api/upload',
[body('email').isEmail().withMessage('Invalid email')],
async (req, res) => {
const result = validationResult(req)
if (!result.isEmpty()) res.status(400).send(result)
const variableRequest = {
variables: {
eidUploaded: {
value: true,
},
passportUploaded: {
value: true,
},
},
}
const userId = getUserId(req.body.email)
try {
const taskResponse = await axios.get(baseURL + `/task?assignee=${userId}`)
const {
id: taskId,
taskDefinitionKey,
processInstanceId,
} = taskResponse.data[0]
if (taskDefinitionKey !== 'UploadFiles')
res.status(400).json({
error:
"User can't Upload Files if the current task isn't 'Upload Files'",
})
await axios.post(baseURL + `/task/${taskId}/complete`, variableRequest)
// Fetch the next task
const newProcessResponse = await axios.get(
baseURL + `/task?processInstanceId=${processInstanceId}`
)
var newTaskId = newProcessResponse.data[0].id
// Assign it to the user
await axios.post(baseURL + `/task/${newTaskId}/assignee`, {
userId,
})
res.status(200).end()
} catch (error) {
console.log(error)
res.status(500).json(error)
}
const city = variablesResponse.data.city.value
const visa = variablesResponse.data.visa.value
const { email, firstName, lastName } = userResponse.data
res.status(200).json({ email, firstName, lastName, city, visa })
}
)
app.listen(port, () => {
console.log(`[server]: App listening on PORT:${port} ✅`)
})