-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (193 loc) · 7.13 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
//Modules used
const express = require('express');
const session = require('express-session');
const {jwtDecode} = require("jwt-decode");
const path = require('path');
const config = require('./config');
const pool = require('./dbPool');
const app = express();
//App configurations
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(session({
secret: config.SECRET,
resave: false,
saveUninitialized: true
}));
app.use((req, res, next) =>{
res.locals.user = req.session.user;
next();
});
//Framework
app.set("view engine", "ejs");
//To execute SQL statements
async function executeSQL(sql, params) {
return new Promise(function (resolve, reject) {
pool.query(sql, params, function (err, rows, fields) {
if (err) {
console.error("Error executing SQL query:", err);
reject(err); // Reject the promise with the error
} else {
resolve(rows);
}
});
});
}
//Routes
app.get('/', (req, res) => {
res.render('index', {"funFacts": config.funFacts});
});
app.post('/', async (req, res) => {
try {
let googleUser = jwtDecode(req.body.gJWToken);
let givenGoogleID = googleUser.sub;
let sql = `SELECT * FROM authorized_users WHERE googleID = ${givenGoogleID}`;
let user = await executeSQL(sql);
if (user.length !== 0) {
req.session.user = givenGoogleID;
res.render('update', { "funFacts": config.funFacts, "isAdmin": user[0].isAdmin });
}
} catch (error) {
console.error("Error querying the database:", error);
res.status(500).send("Internal Server Error");
}
});
app.get('/update', async (req, res) => {
try {
let sql = `SELECT isAdmin FROM authorized_users WHERE googleID=${req.session.user}`;
let user = await executeSQL(sql);
res.render('update', { "funFacts": config.funFacts, "isAdmin": user[0].isAdmin });
} catch (error) {
console.error("Error querying the database:", error);
res.status(500).send("Internal Server Error");
}
});
app.post('/update', async (req, res) => {
try {
let location = "";
let returnTime = "";
let status = req.body.selectedStatus;
if (req.body.location) {
location = req.body.location;
}
if (req.body.returnTime) {
returnTime = req.body.returnTime;
}
let userip = req.ip;
let sql = `UPDATE authorized_users SET currentStatus = ?, currentLocation = ?, returnTime = ? where googleID = ?`;
let params = [status, location, returnTime, req.session.user];
await executeSQL(sql, params);
sql = `INSERT INTO activity_log (timestamp, googleID, ipAddress, location, status)
VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)`;
params = [req.session.user, userip, location, status];
await executeSQL(sql, params);
sql = `SELECT isAdmin FROM authorized_users WHERE googleID = ${req.session.user}`;
let check = await executeSQL(sql);
res.render('update', {"funFacts": config.funFacts, "isAdmin": check[0].isAdmin, "Message": "Log updated!"});
} catch (error) {
console.error("Error updating the database: ", error);
res.status(500).send("Internal Server Error");
}
});
app.get('/administrator', async (req, res) =>{
try {
let sql = 'SELECT timestamp, fName, lName, ipAddress, location, status ' +
'FROM authorized_users JOIN activity_log ON authorized_users.googleID = activity_log.googleID ' +
'ORDER BY timestamp DESC LIMIT 10';
let activityRows = await executeSQL(sql);
sql = `SELECT * FROM authorized_users WHERE googleID = ${req.session.user}`;
let admin = await executeSQL(sql);
sql = `SELECT fName, lName, googleID FROM authorized_users`;
let etisUsers = await executeSQL(sql);
let report = [];
res.render('administrator', {
"etisUsers": etisUsers,
"rows": activityRows,
"admin": admin,
"funFacts": config.funFacts,
"report": report,
modifyStatus
});
} catch (error) {
console.error("Error querying the database:", error);
res.status(500).send("Internal Server Error");
}
});
app.post('/administrator', async (req, res) => {
try{
let employee = req.body.etisUser;
let start = req.body.startDate;
let end = req.body.endDate;
let sql = `SELECT * FROM activity_log WHERE googleID = ? AND timestamp > ? AND timestamp < ?`;
let params = [employee, start, end];
let report = await executeSQL(sql, params);
if (!report) {
report = [];
}
sql = 'SELECT timestamp, fName, lName, ipAddress, location, status FROM authorized_users JOIN activity_log ON authorized_users.googleID = activity_log.googleID LIMIT 10';
let activityRows = await executeSQL(sql);
sql = `SELECT * FROM authorized_users WHERE googleID = ${req.session.user}`;
let admin = await executeSQL(sql);
sql = `SELECT fName, lName, googleID FROM authorized_users`;
let etisUsers = await executeSQL(sql);
sql = `SELECT fName, lName FROM authorized_users WHERE googleID = ${employee}`;
employee = await executeSQL(sql);
res.render('administrator', {
"employee": employee,
"report": report,
"etisUsers": etisUsers,
"rows": activityRows,
"admin": admin,
"funFacts": config.funFacts,
modifyStatus
});
} catch (error){
console.error("Error querying the database:", error);
res.status(500).send("Internal Server Error");
}
});
app.post('/signOut', (req, res) => {
req.session.user = undefined;
res.render('index', {"funFacts": config.funFacts});
});
app.get('/burgerLog', async (req, res) => {
try {
let sql = `SELECT fName, lName, currentStatus, currentLocation, returnTime, burger FROM authorized_users`;
let rows = await executeSQL(sql);
res.render('burgerLog', {"funFacts": config.funFacts, "rows": rows, modifyStatus});
} catch (error) {
console.error("Error querying the database:", error);
res.status(500).send("Internal Server Error");
}
});
// Explicitly set Content-Type for CSS file
app.get('/public/css/styles.css', (req, res) => {
res.type('text/css');
res.sendFile(path.join(__dirname, 'public', 'css', 'styles.css'));
});
//For styling
function modifyStatus(status) {
switch (status) {
case 'in':
return 'In 🫡';
case 'out':
return 'Out ✌️';
case 'lunch':
return 'Lunch 🍔';
case 'break':
return 'Break 😎';
case 'on-site':
return 'On-Site 🤓';
case 'sick':
return 'Sick 😷';
case 'vacation':
return 'Vacation 🍹';
default:
return 'Error';
}
}
// Start the server
app.listen(config.PORT, config.HOSTNAME, () => {
console.log(`Server is running on port `+config.PORT);
});