From 915a6feba66d4637fed911f8d36e6edbb7e783dc Mon Sep 17 00:00:00 2001 From: Mahashour Date: Thu, 1 Oct 2026 14:38:24 +0300 Subject: [PATCH 1/2] feature: Evaluations --- .../src/controllers/evaluationController.js | 82 +++++++++++++++---- server/src/models/Evaluation.js | 28 +++++-- server/src/routes/evaluations.js | 17 ++-- 3 files changed, 96 insertions(+), 31 deletions(-) diff --git a/server/src/controllers/evaluationController.js b/server/src/controllers/evaluationController.js index e9b7ec8..5744f8d 100644 --- a/server/src/controllers/evaluationController.js +++ b/server/src/controllers/evaluationController.js @@ -1,33 +1,79 @@ +import Joi from 'joi'; import { Evaluation } from '../models/Evaluation.js'; +const createSchema = Joi.object({ + sessionCode: Joi.string().required(), + score: Joi.number().integer().min(1).max(5).required(), + comment: Joi.string().optional(), + evaluatedBy: Joi.string().hex().length(24).optional(), +}); + +// POST /api/evaluations +export async function createEvaluation(req, res, next) { + try { + const { value, error } = createSchema.validate(req.body); + if (error) return res.status(400).json({ message: error.message }); + + const evaluation = await Evaluation.create(value); + res.status(201).json({ evaluation }); + } catch (err) { + next(err); + } +} + // GET /api/evaluations -// TODO: implement per README.md section 2. export async function getAllEvaluations(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const evaluations = await Evaluation.find().sort({ createdAt: -1 }).lean(); + res.json({ evaluations }); + } catch (err) { + next(err); + } } // GET /api/evaluations/:id -// TODO: implement per README.md section 2. export async function getEvaluation(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const evaluation = await Evaluation.findById(req.params.id).lean(); + if (!evaluation) return res.status(404).json({ message: 'Evaluation not found' }); + res.json({ evaluation }); + } catch (err) { + next(err); + } } -// POST /api/evaluations -// TODO: implement per README.md section 2. -export async function createEvaluation(req, res, next) { - try { - // TODO - } catch (err) { next(err); } -} - -// GET /api/evaluations/summary?sessionCode=SS101 -// TODO: implement per README.md section 3. +// GET /api/evaluations/summary?sessionCode=XXX export async function getEvaluationSummary(req, res, next) { try { - // TODO - } catch (err) { next(err); } + const { sessionCode } = req.query; + if (!sessionCode) return res.status(400).json({ message: 'sessionCode is required' }); + + const stats = await Evaluation.aggregate([ + { $match: { sessionCode } }, + { + $group: { + _id: '$sessionCode', + averageScore: { $avg: '$score' }, + evaluationCount: { $sum: 1 }, + }, + }, + ]); + + if (stats.length === 0) { + return res.json({ + sessionCode, + averageScore: 0, + evaluationCount: 0, + }); + } + + const result = stats[0]; + res.json({ + sessionCode: result._id, + averageScore: result.averageScore, + evaluationCount: result.evaluationCount, + }); + } catch (err) { + next(err); + } } diff --git a/server/src/models/Evaluation.js b/server/src/models/Evaluation.js index b7a7d5a..fdf4636 100644 --- a/server/src/models/Evaluation.js +++ b/server/src/models/Evaluation.js @@ -1,14 +1,26 @@ import mongoose from 'mongoose'; -// TODO: define the Evaluation schema per README.md section 1. - -const evaluationSchema = new mongoose.Schema( - { - // TODO +const evaluationSchema = new mongoose.Schema({ + sessionCode: { + type: String, + required: true, + }, + score: { + type: Number, + required: true, + min: 1, + max: 5, + }, + comment: { + type: String, + }, + evaluatedBy: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', }, - { timestamps: true } -); +}, { timestamps: true }); -// TODO: add the compound uniqueness constraint described in README.md section 1. +// Compound unique index to prevent duplicate evaluations by the same user for the same session +evaluationSchema.index({ sessionCode: 1, evaluatedBy: 1 }, { unique: true }); export const Evaluation = mongoose.model('Evaluation', evaluationSchema); diff --git a/server/src/routes/evaluations.js b/server/src/routes/evaluations.js index 00ec53e..6537a9a 100644 --- a/server/src/routes/evaluations.js +++ b/server/src/routes/evaluations.js @@ -1,13 +1,20 @@ -import { Router } from 'express'; +import express from 'express'; import { + createEvaluation, getAllEvaluations, getEvaluation, - createEvaluation, - getEvaluationSummary + getEvaluationSummary, } from '../controllers/evaluationController.js'; -const router = Router(); +const router = express.Router(); + +// summary must be before :id +router.get('/summary', getEvaluationSummary); + +router.route('/') + .get(getAllEvaluations) + .post(createEvaluation); -// TODO: wire up the three routes in README.md section 2 and the summary route in section 3. +router.get('/:id', getEvaluation); export default router; From 672f84f5ed934a9f986acc26965b1107cec247f4 Mon Sep 17 00:00:00 2001 From: Mahashour Date: Thu, 1 Oct 2026 14:54:30 +0300 Subject: [PATCH 2/2] Fixed packaging to work on Windows --- server/package-lock.json | 30 ++++++++++++++++++++++++------ server/package.json | 9 +++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/server/package-lock.json b/server/package-lock.json index b819a71..cb5042c 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -10,6 +10,7 @@ "dependencies": { "bcryptjs": "^2.4.3", "cors": "^2.8.5", + "cross-env": "^10.1.0", "dotenv": "^16.4.5", "express": "^4.19.2", "joi": "^17.12.3", @@ -568,6 +569,12 @@ "dev": true, "license": "MIT" }, + "node_modules/@epic-web/invariant": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@epic-web/invariant/-/invariant-1.0.0.tgz", + "integrity": "sha512-lrTPqgvfFQtR/eY/qkIzp98OGdNJu0m5ji3q/nJI8v3SXkRKEnWiOxMmbvcSoAIzv/cGiuvRy57k4suKQSAdwA==", + "license": "MIT" + }, "node_modules/@hapi/hoek": { "version": "9.3.0", "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz", @@ -1869,11 +1876,27 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/cross-env": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-10.1.0.tgz", + "integrity": "sha512-GsYosgnACZTADcmEyJctkJIoqAhHjttw7RsFrVoJNXbsWWqaq6Ym+7kZjq6mS45O0jij6vtiReppKQEtqWy6Dw==", + "license": "MIT", + "dependencies": { + "@epic-web/invariant": "^1.0.0", + "cross-spawn": "^7.0.6" + }, + "bin": { + "cross-env": "dist/bin/cross-env.js", + "cross-env-shell": "dist/bin/cross-env-shell.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/cross-spawn": { "version": "7.0.6", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", - "dev": true, "license": "MIT", "dependencies": { "path-key": "^3.1.0", @@ -2803,7 +2826,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", - "dev": true, "license": "ISC" }, "node_modules/istanbul-lib-coverage": { @@ -4309,7 +4331,6 @@ "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", - "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -4679,7 +4700,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", - "dev": true, "license": "MIT", "dependencies": { "shebang-regex": "^3.0.0" @@ -4692,7 +4712,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", - "dev": true, "license": "MIT", "engines": { "node": ">=8" @@ -5295,7 +5314,6 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", - "dev": true, "license": "ISC", "dependencies": { "isexe": "^2.0.0" diff --git a/server/package.json b/server/package.json index eeec22e..f947030 100644 --- a/server/package.json +++ b/server/package.json @@ -5,17 +5,18 @@ "scripts": { "dev": "nodemon src/index.js", "start": "node src/index.js", - "test": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand", - "grade": "NODE_OPTIONS=--experimental-vm-modules jest --runInBand --verbose" + "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand", + "grade": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --runInBand --verbose" }, "dependencies": { "bcryptjs": "^2.4.3", "cors": "^2.8.5", + "cross-env": "^10.1.0", "dotenv": "^16.4.5", "express": "^4.19.2", + "joi": "^17.12.3", "mongoose": "^8.6.0", - "morgan": "^1.10.0", - "joi": "^17.12.3" + "morgan": "^1.10.0" }, "devDependencies": { "jest": "^29.7.0",