-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
455 lines (380 loc) · 12.9 KB
/
server.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const path = require('path');
const http = require('http');
const socketIo = require('socket.io');
const excelJS = require('exceljs'); // Add excelJS for exporting data to Excel
require('dotenv').config(); // Load environment variables
const session = require('express-session');
const MongoStore = require('connect-mongo');
const bcrypt = require('bcrypt');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const port = process.env.PORT || 3000;
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
// User Schema
const userSchema = new mongoose.Schema({
username: String,
password: String,
role: { type: String, enum: ['main-admin', 'admin', 'door-user'], default: 'door-user' }
});
const User = mongoose.model('User', userSchema);
// Function to create main admin user
const createMainAdminUser = async () => {
const adminUsername = process.env.ADMIN_USERNAME;
const adminPassword = process.env.ADMIN_PASSWORD;
if (!adminUsername || !adminPassword) {
console.error('Admin username or password is not set in the .env file');
process.exit(1);
}
const user = await User.findOne({ username: adminUsername });
if (!user) {
const hashedPassword = bcrypt.hashSync(adminPassword, 10);
const adminUser = new User({ username: adminUsername, password: hashedPassword, role: 'main-admin' });
await adminUser.save();
console.log('Main admin user created');
} else if (user.role !== 'main-admin') {
user.role = 'main-admin';
await user.save();
console.log('Main admin user role updated');
} else {
console.log('Main admin user already exists');
}
};
// Call the function to create the main admin user
createMainAdminUser();
// Define schemas and models
const nameSchema = new mongoose.Schema({
door: String,
name: String,
timestamp: { type: Date, default: Date.now }
});
const Name = mongoose.model('Name', nameSchema);
const doorSchema = new mongoose.Schema({
door: String
});
const Door = mongoose.model('Door', doorSchema);
const archiveSchema = new mongoose.Schema({
eventName: String,
timestamp: { type: Date, default: Date.now },
data: [nameSchema]
});
const Archive = mongoose.model('Archive', archiveSchema);
// Middleware to parse JSON bodies
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Session setup
app.use(session({
secret: process.env.SESSION_SECRET || 'your_secret_key',
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI })
}));
// Authentication middleware
function isAuthenticated(req, res, next) {
if (req.session.user) {
return next();
} else {
res.redirect('/login');
}
}
// Role-based middleware
function isMainAdmin(req, res, next) {
if (req.session.user && req.session.user.role === 'main-admin') {
return next();
} else {
res.status(403).send('Forbidden');
}
}
app.delete('/users/:id', isMainAdmin, async (req, res) => {
try {
const { id } = req.params;
await User.findByIdAndDelete(id);
res.send('User deleted');
} catch (error) {
res.status(500).send(error);
}
});
// Add these routes for fetching and deleting users
app.get('/users', isMainAdmin, async (req, res) => {
try {
const users = await User.find({});
res.json(users);
} catch (error) {
res.status(500).send(error);
}
});
function isAdmin(req, res, next) {
if (req.session.user && (req.session.user.role === 'admin' || req.session.user.role === 'main-admin')) {
return next();
} else {
res.status(403).send('Forbidden');
}
}
// Serve static files
app.use(express.static(path.join(__dirname, 'public')));
// Authentication routes
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
app.post('/login', async (req, res) => {
const { username, password } = req.body;
const user = await User.findOne({ username });
if (user && bcrypt.compareSync(password, user.password)) {
req.session.user = {
id: user._id,
username: user.username,
role: user.role
};
res.redirect('/door');
} else {
res.send('Invalid username or password');
}
});
app.get('/logout', (req, res) => {
req.session.destroy();
res.redirect('/login');
});
// Protect all routes except login and static files
app.use((req, res, next) => {
if (req.path !== '/login' && req.path !== '/logout' && !req.path.startsWith('/public')) {
isAuthenticated(req, res, next);
} else {
next();
}
});
app.get('/user-role', (req, res) => {
if (req.session.user) {
res.json({ role: req.session.user.role });
} else {
res.status(403).send('Forbidden');
}
});
// Application routes
app.get('/', (req, res) => {
if (req.session.user) {
res.redirect('/door');
} else {
res.redirect('/login');
}
});
app.get('/door', isAuthenticated, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'door.html'));
});
app.get('/names', isAuthenticated, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'names.html'));
});
app.get('/admin', isAdmin, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'admin.html'));
});
app.get('/createadmin', isMainAdmin, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'createadmin.html'));
});
app.post('/createadmin', isMainAdmin, async (req, res) => {
const { username, password, role } = req.body;
const hashedPassword = bcrypt.hashSync(password, 10);
const newUser = new User({ username, password: hashedPassword, role });
await newUser.save();
res.send(`User ${username} with role ${role} created`);
});
// Application routes for event management
app.get('/stats', isAdmin, (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'stats.html'));
});
app.get('/stats-data', isAuthenticated, async (req, res) => {
const names = await Name.find({});
const doorCounts = names.reduce((acc, { door }) => {
acc[door] = (acc[door] || 0) + 1;
return acc;
}, {});
const totalCount = names.length;
res.json({ doorCounts, totalCount });
});
app.post('/register', isAuthenticated, async (req, res) => {
const { door, name } = req.body;
if (!door || !name) {
return res.status(400).send('Door and name are required');
}
const newName = new Name({ door, name });
await newName.save();
// Emit updated stats to all connected clients
const names = await Name.find({});
const doorCounts = names.reduce((acc, { door }) => {
acc[door] = (acc[door] || 0) + 1;
return acc;
}, {});
const totalCount = names.length;
io.emit('statsUpdate', { doorCounts, totalCount });
// Emit new name to update all names page
io.emit('newName', { newName, doorCounts, totalCount });
res.send(`Name ${name} registered at door ${door}`);
});
app.get('/recent-names/:door', isAdmin, async (req, res) => {
const { door } = req.params;
const recentNames = await Name.find({ door }).sort({ timestamp: -1 }).limit(10);
res.json(recentNames);
});
app.get('/all-names', isAdmin, async (req, res) => {
const names = await Name.find({}).sort({ door: 1, timestamp: 1 });
res.json(names);
});
// Door management routes
app.get('/doors', isAdmin, async (req, res) => {
const doors = await Door.find({}).sort({ door: 1 });
res.json(doors);
});
app.post('/doors', isAdmin, async (req, res) => {
const { door } = req.body;
const newDoor = new Door({ door });
await newDoor.save();
res.send(`Door ${door} created`);
});
app.put('/doors/:id', isAdmin, async (req, res) => {
const { id } = req.params;
const { newDoorName } = req.body;
await Door.findByIdAndUpdate(id, { door: newDoorName });
res.send(`Door updated to ${newDoorName}`);
});
app.delete('/doors/:id', isAdmin, async (req, res) => {
const { id } = req.params;
await Door.findByIdAndDelete(id);
res.send(`Door deleted`);
});
app.get('/names/:name', isAdmin, async (req, res) => {
const { name } = req.params;
const { allEvents } = req.query; // Get query parameter
const regex = new RegExp(name, 'i'); // Create a case-insensitive regex
if (allEvents === 'true') {
// Search all events (including archived)
const archives = await Archive.find({ 'data.name': { $regex: regex } });
const results = archives.flatMap(archive => archive.data.filter(entry => regex.test(entry.name)));
res.json(results);
} else {
// Search only current event
const nameEntries = await Name.find({ name: { $regex: regex } });
res.json(nameEntries);
}
});
app.delete('/names/:id', isAdmin, async (req, res) => {
const { id } = req.params;
await Name.findByIdAndDelete(id);
res.send(`Name entry deleted`);
});
app.get('/export', isAdmin, async (req, res) => {
try {
const names = await Name.find({}).sort({ timestamp: 1 });
const doorCounts = names.reduce((acc, { door }) => {
acc[door] = (acc[door] || 0) + 1;
return acc;
}, {});
const totalCount = names.length;
const workbook = new excelJS.Workbook();
const worksheet = workbook.addWorksheet('Event Data');
worksheet.columns = [
{ header: 'Name', key: 'name', width: 25 },
{ header: 'Door', key: 'door', width: 10 },
{ header: 'Timestamp', key: 'timestamp', width: 25 }
];
names.forEach(name => {
worksheet.addRow({
name: name.name,
door: name.door,
timestamp: new Date(name.timestamp).toLocaleString()
});
});
const summarySheet = workbook.addWorksheet('Summary');
summarySheet.columns = [
{ header: 'Door', key: 'door', width: 25 },
{ header: 'Count', key: 'count', width: 10 }
];
for (const [door, count] of Object.entries(doorCounts)) {
summarySheet.addRow({ door, count });
}
summarySheet.addRow({ door: 'Total', count: totalCount });
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', `attachment; filename=event_data.xlsx`);
await workbook.xlsx.write(res);
res.end();
} catch (error) {
res.status(500).send(error);
}
});
// Archive event data
app.post('/archive', isAdmin, async (req, res) => {
const { eventName } = req.body;
const names = await Name.find({});
const newArchive = new Archive({ eventName, data: names });
await newArchive.save();
res.send('Event data archived');
});
app.get('/archives', isAdmin, async (req, res) => {
try {
const archives = await Archive.find({});
const archiveData = archives.map(archive => ({
_id: archive._id,
eventName: archive.eventName,
timestamp: archive.timestamp,
totalCount: archive.data.length
}));
res.json(archiveData);
} catch (error) {
res.status(500).send(error);
}
});
app.get('/archive/:id', isAdmin, async (req, res) => {
try {
const { id } = req.params;
const archive = await Archive.findById(id);
res.json(archive);
} catch (error) {
res.status(500).send(error);
}
});
// Export archived data
app.get('/export-archive/:id', isAdmin, async (req, res) => {
try {
const { id } = req.params;
const archive = await Archive.findById(id);
const workbook = new excelJS.Workbook();
const worksheet = workbook.addWorksheet('Archived Event Data');
worksheet.columns = [
{ header: 'Name', key: 'name', width: 25 },
{ header: 'Door', key: 'door', width: 10 },
{ header: 'Timestamp', key: 'timestamp', width: 25 }
];
archive.data.forEach(entry => {
worksheet.addRow({
name: entry.name,
door: entry.door,
timestamp: new Date(entry.timestamp).toLocaleString()
});
});
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.setHeader('Content-Disposition', `attachment; filename=archive_${archive.eventName}.xlsx`);
await workbook.xlsx.write(res);
res.end();
} catch (error) {
res.status(500).send(error);
}
});
// Delete archived data
app.delete('/archive/:id', isAdmin, async (req, res) => {
try {
const { id } = req.params;
await Archive.findByIdAndDelete(id);
res.send(`Archived data deleted`);
} catch (error) {
res.status(500).send(error);
}
});
// Delete all entries for an event
app.delete('/names', isAdmin, async (req, res) => {
await Name.deleteMany({});
res.send(`All entries deleted`);
});
server.listen(port, () => {
console.log(`App running at http://localhost:${port}`);
});