-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
553 lines (490 loc) · 19.8 KB
/
app.js
File metadata and controls
553 lines (490 loc) · 19.8 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
const express = require("express");
const path = require("path");
const multer = require("multer");
const collection = require("./mongodb");
const app = express();
const port = 3000;
const Authority = require("./models/authority");
// Static files path
const staticPath = path.join(__dirname, "public");
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(staticPath));
// Configure Multer for file uploads (store in memory)
const upload = multer({ storage: multer.memoryStorage() });
// Serve index.html
app.get("/", (req, res) => {
res.sendFile(path.join(staticPath, "index.html"));
});
// Handle issue report submissions
app.post("/report", upload.array("images", 5), async (req, res) => {
try {
const { name, mobile, aadhar, issue, area } = req.body;
const images = req.files.map((file) => {
return {
filename: file.originalname,
contentType: file.mimetype,
data: file.buffer.toString("base64"), // Convert buffer to base64
};
});
if (!name || !mobile || !aadhar || !issue || !area) {
return res.status(400).sendFile(path.join(staticPath, "error.html"));
}
const data = { name, mobile, aadhar, issue, area, images };
await collection.insertMany([data]);
res.status(200).sendFile(path.join(staticPath, "success.html"));
} catch (error) {
console.error("Error saving report:", error);
res.status(500).sendFile(path.join(staticPath, "error.html"));
}
});
// View all reported issues with images (Base64 data)
// View all reported issues with images (Styled with TailwindCSS)
app.get("/view-reports", async (req, res) => {
try {
const reports = await collection.find();
// Render the reports with centered images and modal functionality
let html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>View Reports</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-900 text-gray-100">
<div class="container mx-auto py-10">
<h1 class="text-4xl font-bold text-center mb-10">Reported Issues</h1>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
${reports
.map((report) => {
return `
<div
class="p-6 bg-gray-800 rounded-lg shadow-md transition-transform transform hover:scale-105 hover:shadow-xl cursor-pointer"
>
<p class="font-semibold text-lg mb-2">${report.name}</p>
<p class="text-sm text-gray-400 mb-2"><strong>Mobile:</strong> ${report.mobile}</p>
<p class="text-sm text-gray-400 mb-2"><strong>Aadhar:</strong> ${report.aadhar}</p>
<p class="text-sm text-gray-400 mb-2"><strong>Issue:</strong> ${report.issue}</p>
<p class="text-sm text-gray-400 mb-4"><strong>Area:</strong> ${report.area}</p>
<div class="space-y-4">
${report.images
.map((image) => {
return `
<div class="relative flex justify-center items-center overflow-hidden rounded-lg">
<img
src="data:${image.contentType};base64,${image.data}"
alt="${image.filename}"
class="w-full max-h-40 object-cover transition-transform duration-300 transform hover:scale-110 cursor-pointer"
onclick="showModal('${image.contentType}', '${image.data}', '${image.filename}')"
/>
</div>
`;
})
.join("")}
</div>
</div>
`;
})
.join("")}
</div>
<div class="text-center mt-8">
<a href="https://civilizedchaos.netlify.app/" class="btn">Back to Home</a>
</div>
</div>
<!-- Modal -->
<div id="imageModal" class="fixed inset-0 bg-black bg-opacity-75 flex items-center justify-center hidden">
<div class="relative bg-gray-800 rounded-lg shadow-lg p-4 max-w-3xl w-full">
<button
onclick="closeModal()"
class="absolute top-2 right-2 text-gray-400 hover:text-white focus:outline-none"
>
✕
</button>
<img id="modalImage" src="" alt="" class="w-full rounded-lg" />
<p id="modalImageName" class="text-gray-400 mt-4 text-center"></p>
</div>
</div>
<script>
// Show modal with the clicked image
function showModal(contentType, data, filename) {
const modal = document.getElementById("imageModal");
const modalImage = document.getElementById("modalImage");
const modalImageName = document.getElementById("modalImageName");
modalImage.src = \`data:\${contentType};base64,\${data}\`;
modalImageName.textContent = filename;
modal.classList.remove("hidden");
}
// Close the modal
function closeModal() {
const modal = document.getElementById("imageModal");
modal.classList.add("hidden");
}
</script>
<style>
.btn {
display: inline-block;
background-color: #3498db;
color: #fff;
font-weight: bold;
text-align: center;
padding: 12px 24px;
border-radius: 6px;
text-decoration: none;
transition: all 0.3s ease;
font-size: 1rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.btn:hover {
background-color: #2980b9;
transform: translateY(-4px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.2);
}
.btn:active {
transform: translateY(2px);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* Responsive styles */
@media (max-width: 768px) {
.btn {
width: 100%;
padding: 15px 30px;
font-size: 1.1rem;
}
}
</style>
</body>
</html>
`;
res.send(html);
} catch (error) {
console.error("Error fetching reports:", error);
res.status(500).send("Error fetching reports.");
}
});
app.get("/graphical-analysis", async (req, res) => {
try {
const reports = await collection.find();
// Initialize arrays for monthly reports, solved, and pending issues
const monthlyReports = new Array(12).fill(0); // Zero for each month
const solvedIssues = new Array(12).fill(0);
const pendingIssues = new Array(12).fill(0);
// Populate monthly data based on reports
reports.forEach((report) => {
const month = new Date(report._id.getTimestamp()).getMonth(); // Get month index (0 = January, 11 = December)
monthlyReports[month]++;
// Assuming all reports are "pending" initially
pendingIssues[month]++;
// Example of how to differentiate solved and pending issues
if (report.issue.toLowerCase().includes("resolved")) {
solvedIssues[month]++;
pendingIssues[month]--;
}
});
// HTML for the graphical analysis page
let html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Graphical Analysis</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-900 text-gray-100">
<div class="container mx-auto px-4 py-10">
<h1 class="text-4xl font-extrabold text-center mb-8 text-gray-100 hover:text-gray-300">Graphical Analysis</h1>
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Reports Chart -->
<div class="p-6 bg-gray-800 rounded-lg shadow-lg transition-transform transform hover:scale-105 hover:shadow-2xl">
<canvas id="reportsChart" class="w-full h-80"></canvas>
</div>
<!-- Issues Chart -->
<div class="p-6 bg-gray-800 rounded-lg shadow-lg transition-transform transform hover:scale-105 hover:shadow-2xl">
<canvas id="issuesChart" class="w-full h-80"></canvas>
</div>
</div>
<a href="https://civilizedchaos.netlify.app" class="block mt-8 mx-auto text-center text-lg font-semibold bg-blue-600 text-white py-3 px-6 rounded-md hover:bg-blue-700 transition-all">
Back to Home
</a>
</div>
<script>
// Data for Monthly Reports Filed
const labels = ['December', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November'];
const reportsData = {
labels: labels,
datasets: [{
label: 'Reports Filed',
data: ${JSON.stringify(monthlyReports)},
backgroundColor: [
'rgba(54, 162, 235, 0.7)', 'rgba(75, 192, 192, 0.7)', 'rgba(255, 206, 86, 0.7)',
'rgba(153, 102, 255, 0.7)', 'rgba(255, 159, 64, 0.7)', 'rgba(201, 203, 207, 0.7)',
'rgba(99, 255, 132, 0.7)', 'rgba(255, 99, 132, 0.7)', 'rgba(255, 159, 64, 0.7)',
'rgba(54, 162, 235, 0.7)', 'rgba(75, 192, 192, 0.7)', 'rgba(153, 102, 255, 0.7)'
],
borderColor: [
'rgba(54, 162, 235, 1)', 'rgba(75, 192, 192, 1)', 'rgba(255, 206, 86, 1)',
'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)', 'rgba(201, 203, 207, 1)',
'rgba(99, 255, 132, 1)', 'rgba(255, 99, 132, 1)', 'rgba(255, 159, 64, 1)',
'rgba(54, 162, 235, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)'
],
borderWidth: 1
}]
};
const reportsConfig = {
type: 'bar',
data: reportsData,
options: {
responsive: true,
plugins: {
legend: { position: 'top', labels: { color: '#ffffff' } },
title: { display: true, text: 'Monthly Reports Filed', color: '#ffffff' }
},
scales: {
x: { ticks: { color: '#ffffff' }, grid: { color: 'rgba(255, 255, 255, 0.1)' } },
y: { beginAtZero: true, max: 10, ticks: { color: '#ffffff' }, grid: { color: 'rgba(255, 255, 255, 0.1)' } }
}
}
};
const reportsChart = new Chart(document.getElementById('reportsChart'), reportsConfig);
// Data for Solved vs Pending Issues
const issuesData = {
labels: labels,
datasets: [
{
label: 'Issues Solved',
data: ${JSON.stringify(solvedIssues)},
backgroundColor: 'rgba(54, 162, 235, 0.7)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
},
{
label: 'Issues Pending',
data: ${JSON.stringify(pendingIssues)},
backgroundColor: 'rgba(255, 99, 132, 0.7)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}
]
};
const issuesConfig = {
type: 'bar',
data: issuesData,
options: {
responsive: true,
plugins: {
legend: { position: 'top', labels: { color: '#ffffff' } },
title: { display: true, text: 'Monthly Issue Resolution (Solved vs Pending)', color: '#ffffff' }
},
scales: {
x: { ticks: { color: '#ffffff' }, grid: { color: 'rgba(255, 255, 255, 0.1)' } },
y: { beginAtZero: true, max: 10, ticks: { color: '#ffffff' }, grid: { color: 'rgba(255, 255, 255, 0.1)' } }
}
}
};
const issuesChart = new Chart(document.getElementById('issuesChart'), issuesConfig);
</script>
</body>
</html>
`;
// Send the rendered HTML as a response
res.send(html);
} catch (error) {
res.status(500).send("Error generating graphical analysis.");
}
});
// Public: View all authorities
app.get("/authorities", async (req, res) => {
try {
const authorities = await Authority.find();
res.status(200).json(authorities);
} catch (error) {
res.status(500).json({ message: "Error fetching authorities." });
}
});
// Government login page
app.get("/authorities-modify/login", (req, res) => {
res.sendFile(path.join(__dirname, "views", "login.html")); // Login page
});
// Handle government login
app.post("/authorities-modify/login", async(req, res) => {
const { id, password } = req.body;
console.log("Received data:", req.body);
// Hardcoded credentials
const governmentId = "1234";
const governmentPassword = "qwerty";
if (id === governmentId && password === governmentPassword) {
res.redirect("/authorities-modify");
} else {
res.sendFile(path.join(__dirname, "views", "login.html")); // Reload login page on failure
}
});
// Modify authorities (protected)
app.get("/authorities-modify", (req, res) => {
res.sendFile(path.join(__dirname, "views", "modify.html")); // Modify authorities page
});
// Add new authority (protected)
app.post("/authorities-modify/add", async (req, res) => {
const { name, email, honourScore } = req.body;
try {
const newAuthority = new Authority({ name, email, honourScore });
await newAuthority.save();
res.status(200).sendFile(path.join(staticPath, "success_add.html"));
} catch (error) {
console.error("Error saving report:", error);
res.status(500).sendFile(path.join(staticPath, "error_add.html"));
}
});
// Update honour score (protected)
app.post("/authorities-modify/update", async (req, res) => {
const { name, honourScore } = req.body;
try {
const authority = await Authority.findOneAndUpdate(
{ name: name }, // Searching by authority name
{ honourScore: honourScore }, // Updating the honourScore
{ new: true } // Returns the updated document
);
console.log("Found authority:", authority);
if (!authority) {
return res.status(404).send("Authority not found.");
}
// Send a success message along with redirect
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Update Success</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #2c3e50;
color: #ecf0f1;
}
.success-message {
background-color: #27ae60;
color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
}
a {
display: block;
margin-top: 20px;
text-decoration: none;
color: #3498db;
font-weight: bold;
text-align: center;
}
a:hover {
color: #2980b9;
}
</style>
</head>
<body>
<div class="success-message">
<h2>Success!</h2>
<p>The Honour Score for ${name} has been updated successfully.</p>
<a href="/authorities-modify">Go back</a>
</div>
</body>
</html>
`);
} catch (error) {
res.status(500).send("Error updating honour score.");
}
});
// Logout for government
app.get("/authorities-modify/logout", (req, res) => {
res.redirect("/authorities-modify/login");
});
app.get("/view-authorities", async (req, res) => {
try {
const authorities = await Authority.find();
// Render the authorities in a modern blog-like layout
let html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>List of Authorities</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-900 text-gray-100">
<div class="container mx-auto py-10 px-6">
<h1 class="text-4xl font-bold text-center mb-8">List of Authorities</h1>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
${authorities
.map((authority) => {
return `
<div class="bg-gray-800 p-6 rounded-lg shadow-lg hover:shadow-2xl transition-all">
<h3 class="text-xl font-semibold text-blue-400 mb-2">${authority.name}</h3>
<p class="text-gray-300 mb-2">${authority.email}</p>
<div class="flex items-center justify-between">
<span class="text-gray-500">Honour Score:</span>
<span class="text-yellow-400 font-semibold">${authority.honourScore}</span>
</div>
</div>
`;
})
.join("")}
</div>
<div class="text-center mt-8">
<a href="https://civilizedchaos.netlify.app/" class="btn">Back to Home</a>
</div>
</div>
<style>
.btn {
display: inline-block;
background-color: #3498db;
color: #fff;
font-weight: bold;
text-align: center;
padding: 12px 24px;
border-radius: 6px;
text-decoration: none;
transition: all 0.3s ease;
font-size: 1rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.btn:hover {
background-color: #2980b9;
transform: translateY(-4px);
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.2);
}
.btn:active {
transform: translateY(2px);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
/* Responsive styles */
@media (max-width: 768px) {
.btn {
width: 100%;
padding: 15px 30px;
font-size: 1.1rem;
}
}
</style>
</body>
</html>
`;
res.send(html);
} catch (error) {
console.error("Error fetching authorities:", error);
res.status(500).send("Error fetching authorities.");
}
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});