-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
423 lines (376 loc) · 13.5 KB
/
server.js
File metadata and controls
423 lines (376 loc) · 13.5 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
import express from "express";
import mongoose from "mongoose";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
import dotenv from "dotenv";
import cors from "cors";
import helmet from "helmet";
import morgan from "morgan";
import multer from "multer";
import fs from "fs";
import path from "path";
import validator from "validator";
import rateLimit from "express-rate-limit";
dotenv.config();
// ---------- Setup ----------
const app = express();
app.use(express.json({ limit: "1mb" }));
app.use(cors());
app.use(helmet());
app.use(morgan("dev"));
const limiter = rateLimit({ windowMs: 60_000, max: 120 });
app.use(limiter);
// Ensure upload dir
const UPLOAD_DIR = process.env.UPLOAD_DIR || "./uploads";
fs.mkdirSync(UPLOAD_DIR, { recursive: true });
app.use("/uploads", express.static(UPLOAD_DIR));
// ---------- DB ----------
await mongoose.connect(process.env.MONGO_URI);
// ---------- Constants ----------
const ROLES = {
DEFENDANT: "DEFENDANT",
PLAINTIFF: "PLAINTIFF",
JUROR: "JUROR",
JUDGE: "JUDGE"
};
const CASE_STATUS = {
PENDING: "PENDING",
APPROVED: "APPROVED",
REJECTED: "REJECTED"
};
const VOTE = {
GUILTY: "guilty",
NOT_GUILTY: "not_guilty"
};
// ---------- Schemas ----------
const UserSchema = new mongoose.Schema(
{
name: { type: String, required: true, trim: true },
email: {
type: String,
required: true,
unique: true,
lowercase: true,
trim: true,
validate: { validator: validator.isEmail, message: "Invalid email" }
},
passwordHash: { type: String, required: true },
role: {
type: String,
enum: Object.values(ROLES),
required: true
}
},
{ timestamps: true }
);
const CaseSchema = new mongoose.Schema(
{
title: { type: String, required: true, trim: true, maxlength: 200 },
partyType: {
type: String,
enum: [ROLES.DEFENDANT, ROLES.PLAINTIFF],
required: true
},
partyName: { type: String, required: true, trim: true, maxlength: 120 },
argumentText: { type: String, required: true, trim: true, maxlength: 5000 },
evidenceText: { type: String, trim: true, maxlength: 5000 },
evidenceFiles: [{ type: String }], // file paths
status: {
type: String,
enum: Object.values(CASE_STATUS),
default: CASE_STATUS.PENDING
},
createdBy: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true },
roleAtCreation: { type: String, enum: [ROLES.DEFENDANT, ROLES.PLAINTIFF], required: true }
},
{ timestamps: true }
);
const VoteSchema = new mongoose.Schema(
{
case: { type: mongoose.Schema.Types.ObjectId, ref: "Case", required: true, index: true },
juror: { type: mongoose.Schema.Types.ObjectId, ref: "User", required: true, index: true },
choice: { type: String, enum: [VOTE.GUILTY, VOTE.NOT_GUILTY], required: true }
},
{ timestamps: true }
);
VoteSchema.index({ case: 1, juror: 1 }, { unique: true });
const User = mongoose.model("User", UserSchema);
const Case = mongoose.model("Case", CaseSchema);
const VoteModel = mongoose.model("Vote", VoteSchema);
// ---------- Auth Helpers ----------
function signToken(user) {
return jwt.sign(
{ sub: user._id.toString(), role: user.role, name: user.name },
process.env.JWT_SECRET,
{ expiresIn: process.env.JWT_EXPIRES_IN || "7d" }
);
}
function auth(required = true) {
return (req, res, next) => {
const authHeader = req.headers.authorization || "";
const token = authHeader.startsWith("Bearer ") ? authHeader.slice(7) : null;
if (!token) {
if (required) return res.status(401).json({ error: "Missing token" });
req.user = null;
return next();
}
try {
const payload = jwt.verify(token, process.env.JWT_SECRET);
req.user = { id: payload.sub, role: payload.role, name: payload.name };
return next();
} catch {
return res.status(401).json({ error: "Invalid token" });
}
};
}
function requireRoles(...allowed) {
return (req, res, next) => {
if (!req.user) return res.status(401).json({ error: "Unauthorized" });
if (!allowed.includes(req.user.role)) {
return res.status(403).json({ error: "Forbidden: insufficient role" });
}
next();
};
}
// ---------- Uploads (optional docs) ----------
const storage = multer.diskStorage({
destination: (req, file, cb) => cb(null, UPLOAD_DIR),
filename: (req, file, cb) => {
const safeBase = file.originalname.replace(/[^a-z0-9.\-_]/gi, "_");
const unique = `${Date.now()}-${Math.round(Math.random() * 1e9)}-${safeBase}`;
cb(null, unique);
}
});
const upload = multer({
storage,
limits: { fileSize: 10 * 1024 * 1024 } // 10 MB per file
});
// ---------- Routes ----------
// Health
app.get("/health", (_req, res) => res.json({ ok: true, time: new Date().toISOString() }));
// AUTH
// ----------------------------------
// 2. AUTH ROUTES — must go BEFORE 404 handler
// ----------------------------------
app.post("/auth/signup", async (req, res) => {
try {
const { name, email, password, role } = req.body || {};
if (!name || !email || !password || !role)
return res.status(400).json({ error: "name, email, password, role are required" });
if (!Object.values(ROLES).includes(role))
return res.status(400).json({ error: "Invalid role" });
const existing = await User.findOne({ email: email.toLowerCase() });
if (existing)
return res.status(409).json({ error: "Email already registered" });
const passwordHash = await bcrypt.hash(password, 10);
const user = await User.create({ name, email: email.toLowerCase(), passwordHash, role });
const token = signToken(user);
res.status(201).json({
token,
user: { id: user._id, name: user.name, email: user.email, role: user.role },
});
} catch (e) {
res.status(500).json({ error: "Signup failed" });
}
});
app.post("/auth/login", async (req, res) => {
try {
const { email, password } = req.body || {};
if (!email || !password)
return res.status(400).json({ error: "email and password required" });
const user = await User.findOne({ email: email.toLowerCase() });
if (!user)
return res.status(401).json({ error: "Invalid credentials" });
const ok = await bcrypt.compare(password, user.passwordHash);
if (!ok)
return res.status(401).json({ error: "Invalid credentials" });
const token = signToken(user);
res.json({
token,
user: { id: user._id, name: user.name, email: user.email, role: user.role },
});
} catch {
res.status(500).json({ error: "Login failed" });
}
});
// CASES
// POST /case/submit - Defendant / Plaintiff
app.post(
"/case/submit",
auth(true),
requireRoles(ROLES.DEFENDANT, ROLES.PLAINTIFF),
upload.array("files", 5),
async (req, res) => {
try {
const { title, partyName, argumentText, evidenceText } = req.body || {};
if (!title || !partyName || !argumentText) {
return res.status(400).json({ error: "title, partyName, argumentText are required" });
}
const evidenceFiles = (req.files || []).map(f => `/uploads/${path.basename(f.path)}`);
const doc = await Case.create({
title: title.trim(),
partyType: req.user.role, // role of the submitter (must be DEFENDANT or PLAINTIFF per middleware)
partyName: partyName.trim(),
argumentText: argumentText.trim(),
evidenceText: evidenceText?.trim() || "",
evidenceFiles,
createdBy: req.user.id,
roleAtCreation: req.user.role, // locked
status: CASE_STATUS.PENDING // judge approval mandatory
});
res.status(201).json(doc);
} catch (e) {
res.status(500).json({ error: "Failed to submit case" });
}
}
);
// GET /case/all - All roles - View all submissions
// Note: Jurors see only APPROVED. Others see all.
app.get("/case/all", auth(true), async (req, res) => {
try {
const filter =
req.user?.role === ROLES.JUROR
? { status: CASE_STATUS.APPROVED }
: {}; // judges/plaintiffs/defendants see all
const docs = await Case.find(filter).sort({ createdAt: -1 });
res.json(docs);
} catch {
res.status(500).json({ error: "Failed to fetch cases" });
}
});
// PATCH /case/edit/:id - Judge only
app.patch("/case/edit/:id", auth(true), requireRoles(ROLES.JUDGE), async (req, res) => {
try {
const { id } = req.params;
const editable = ["title", "partyName", "argumentText", "evidenceText", "partyType", "status"];
const update = {};
for (const k of editable) if (k in req.body) update[k] = req.body[k];
const doc = await Case.findByIdAndUpdate(id, update, { new: true });
if (!doc) return res.status(404).json({ error: "Case not found" });
res.json(doc);
} catch {
res.status(500).json({ error: "Failed to edit case" });
}
});
// DELETE /case/delete/:id - Judge only
app.delete("/case/delete/:id", auth(true), requireRoles(ROLES.JUDGE), async (req, res) => {
try {
const { id } = req.params;
const doc = await Case.findByIdAndDelete(id);
if (!doc) return res.status(404).json({ error: "Case not found" });
res.json({ ok: true });
} catch {
res.status(500).json({ error: "Failed to delete case" });
}
});
// Brownie: FILTER by party name - Juror only, approved only
app.get("/case/by-name/:name", auth(true), requireRoles(ROLES.JUROR), async (req, res) => {
try {
const name = req.params.name;
const docs = await Case.find({
status: CASE_STATUS.APPROVED,
partyName: { $regex: new RegExp(validator.escape(name), "i") }
}).sort({ createdAt: -1 });
res.json(docs);
} catch {
res.status(500).json({ error: "Failed to filter" });
}
});
// Brownie: Approve/Reject - Judge only
app.patch("/case/approve/:id", auth(true), requireRoles(ROLES.JUDGE), async (req, res) => {
try {
const doc = await Case.findByIdAndUpdate(
req.params.id,
{ status: CASE_STATUS.APPROVED },
{ new: true }
);
if (!doc) return res.status(404).json({ error: "Case not found" });
res.json(doc);
} catch {
res.status(500).json({ error: "Failed to approve" });
}
});
app.patch("/case/reject/:id", auth(true), requireRoles(ROLES.JUDGE), async (req, res) => {
try {
const doc = await Case.findByIdAndUpdate(
req.params.id,
{ status: CASE_STATUS.REJECTED },
{ new: true }
);
if (!doc) return res.status(404).json({ error: "Case not found" });
res.json(doc);
} catch {
res.status(500).json({ error: "Failed to reject" });
}
});
// JURY VOTING
// POST /jury/vote/:caseId - Juror only; only on APPROVED cases; one vote per juror per case
app.post("/jury/vote/:caseId", auth(true), requireRoles(ROLES.JUROR), async (req, res) => {
try {
const { caseId } = req.params;
const { choice } = req.body || {};
if (![VOTE.GUILTY, VOTE.NOT_GUILTY].includes(choice))
return res.status(400).json({ error: "choice must be 'guilty' or 'not_guilty'" });
const caseDoc = await Case.findById(caseId);
if (!caseDoc) return res.status(404).json({ error: "Case not found" });
if (caseDoc.status !== CASE_STATUS.APPROVED)
return res.status(400).json({ error: "Case not approved for voting" });
const vote = await VoteModel.findOneAndUpdate(
{ case: caseId, juror: req.user.id },
{ choice },
{ upsert: true, new: true, setDefaultsOnInsert: true }
);
res.status(201).json(vote);
} catch (e) {
if (e?.code === 11000) {
return res.status(409).json({ error: "You have already voted on this case" });
}
res.status(500).json({ error: "Failed to vote" });
}
});
// GET /jury/results/:caseId - Juror/Judge
app.get(
"/jury/results/:caseId",
auth(true),
requireRoles(ROLES.JUROR, ROLES.JUDGE),
async (req, res) => {
try {
const { caseId } = req.params;
const caseDoc = await Case.findById(caseId);
if (!caseDoc) return res.status(404).json({ error: "Case not found" });
// For jurors, only reveal results if case is approved
if (req.user.role === ROLES.JUROR && caseDoc.status !== CASE_STATUS.APPROVED) {
return res.status(403).json({ error: "Results unavailable for unapproved case" });
}
const agg = await VoteModel.aggregate([
{ $match: { case: new mongoose.Types.ObjectId(caseId) } },
{
$group: {
_id: "$choice",
count: { $sum: 1 }
}
}
]);
const counts = { guilty: 0, not_guilty: 0, total: 0 };
for (const row of agg) {
if (row._id === VOTE.GUILTY) counts.guilty = row.count;
if (row._id === VOTE.NOT_GUILTY) counts.not_guilty = row.count;
}
counts.total = counts.guilty + counts.not_guilty;
res.json({ caseId, status: caseDoc.status, results: counts });
} catch {
res.status(500).json({ error: "Failed to fetch results" });
}
}
);
// ---------- Error & Server ----------
app.use((req, res) => res.status(404).json({ error: "Not found" }));
const PORT = process.env.PORT || 4000;
app._router.stack.forEach(r => {
if (r.route && r.route.path) {
console.log("Loaded route:", r.route.path, Object.keys(r.route.methods));
}
});
app.listen(PORT, () => {
console.log(`Chef’s Court API running on :${PORT}`);
});