diff --git a/peerprep/backend/question-service/src/middleware/normalizationMiddleware.ts b/peerprep/backend/question-service/src/middleware/normalizationMiddleware.ts new file mode 100644 index 0000000000..1c98ece1d1 --- /dev/null +++ b/peerprep/backend/question-service/src/middleware/normalizationMiddleware.ts @@ -0,0 +1,13 @@ +import { Request, Response, NextFunction } from 'express'; + +export const normalizeQuestionData = (req: Request, res: Response, next: NextFunction): void => { + if (req.body) { + if (Array.isArray(req.body.categories)) { + req.body.categories = req.body.categories.map((cat: string) => cat.toLowerCase()); + } + if (typeof req.body.difficulty === 'string') { + req.body.difficulty = req.body.difficulty.toLowerCase(); + } + } + next(); +}; diff --git a/peerprep/backend/question-service/src/server.ts b/peerprep/backend/question-service/src/server.ts index 3ed070789b..3262ca7ebc 100644 --- a/peerprep/backend/question-service/src/server.ts +++ b/peerprep/backend/question-service/src/server.ts @@ -8,6 +8,7 @@ import questionRoutes from './routes/questionRoutes'; import databaseRoutes from './routes/databaseRoutes'; import gptRoutes from './routes/gptRoutes'; import loadSampleData from './sampleData'; +import { normalizeQuestionData } from './middleware/normalizationMiddleware'; connectDB() // Initialize MongoDB connection .then(() => { @@ -43,6 +44,11 @@ app.use(cors({ } as CorsOptions)); app.use(express.json()); +// Apply normalization middleware to specific routes +// This middleware will normalize `categories` and `difficulty` fields to lowercase +app.use('/api/questions', normalizeQuestionData); + + // API routes app.use('/api', questionRoutes);