-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
105 lines (85 loc) · 2.28 KB
/
app.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
const express = require('express');
const https = require('https');
const cors = require('cors');
const app = express();
const port = 4000;
app.use(cors());
app.use(
express.urlencoded({
extended: true,
})
);
app.get('/api/supervisors', (req, res) => {
const url = 'https://o3m5qixdng.execute-api.us-east-1.amazonaws.com/api/managers';
https.get(url, response => {
let body = [];
response.on('data', chunk => {
body.push(chunk);
});
response.on('end', () => {
let parsedData = JSON.parse(Buffer.concat(body).toString());
// Remove numeric jurisdictions from response
let filtered = [];
parsedData.forEach(item => {
if (isNaN(item.jurisdiction)) {
filtered.push(item);
}
});
// Sort by jurisdiction, last name, and first name in alphabetical order
filtered.sort((a, b) => {
if (a.lastName.toUpperCase() === b.lastName.toUpperCase()) {
return a.firstName > b.firstName ? 1 : -1;
}
if (a.jurisdiction === b.jurisdiction) {
return a.lastName > b.lastName ? 1 : -1;
}
return a.jurisdiction > b.jurisdiction ? 1 : -1;
});
// Formatting of the result
let result = [];
filtered.forEach(item => {
result.push({
id: item.id,
label: item.formatted = item.jurisdiction + ' - ' + item.lastName + ', ' + item.firstName
});
});
res.send(result);
});
});
});
app.post('/api/submit', (req, res) => {
let valid = true;
let errors = {
firstName: '',
lastName: '',
email: '',
phone: '',
supervisor: ''
};
let body = [];
req.on('data', chunk => {
body.push(chunk);
});
req.on('end', () => {
let parsedData = JSON.parse(Buffer.concat(body).toString());
if (parsedData.firstName === '') {
errors.firstName = 'First Name is required!';
valid = false;
}
if (parsedData.lastName === '') {
errors.lastName = 'Last Name is required!';
valid = false;
}
if (parsedData.supervisor === '') {
errors.supervisor = 'Supervisor is required!';
valid = false;
}
if (valid) {
res.send({status: 200});
} else {
res.send({status: 400, errors});
}
});
});
app.listen(port, () => {
});