feat: add admin questions routes
Made-with: Cursor
This commit is contained in:
128
src/routes/admin/questions.ts
Normal file
128
src/routes/admin/questions.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { AdminQuestionService } from '../../services/admin/admin-question.service.js';
|
||||
import type { EditQuestionInput } from '../../services/admin/admin-question.service.js';
|
||||
|
||||
const STACKS = ['html', 'css', 'js', 'ts', 'react', 'vue', 'nodejs', 'git', 'web_basics'] as const;
|
||||
const LEVELS = ['basic', 'beginner', 'intermediate', 'advanced', 'expert'] as const;
|
||||
const QUESTION_TYPES = ['single_choice', 'multiple_select', 'true_false', 'short_text'] as const;
|
||||
|
||||
const listPendingQuerySchema = {
|
||||
querystring: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
limit: { type: 'integer', minimum: 1, maximum: 100 },
|
||||
offset: { type: 'integer', minimum: 0 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
const questionIdParamsSchema = {
|
||||
params: {
|
||||
type: 'object',
|
||||
required: ['questionId'],
|
||||
properties: {
|
||||
questionId: { type: 'string', format: 'uuid' },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const editQuestionSchema = {
|
||||
params: {
|
||||
type: 'object',
|
||||
required: ['questionId'],
|
||||
properties: {
|
||||
questionId: { type: 'string', format: 'uuid' },
|
||||
},
|
||||
},
|
||||
body: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
stack: { type: 'string', enum: [...STACKS] },
|
||||
level: { type: 'string', enum: [...LEVELS] },
|
||||
type: { type: 'string', enum: [...QUESTION_TYPES] },
|
||||
questionText: { type: 'string', minLength: 1 },
|
||||
options: {
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['key', 'text'],
|
||||
properties: {
|
||||
key: { type: 'string' },
|
||||
text: { type: 'string' },
|
||||
},
|
||||
},
|
||||
},
|
||||
correctAnswer: {
|
||||
oneOf: [
|
||||
{ type: 'string' },
|
||||
{ type: 'array', items: { type: 'string' } },
|
||||
],
|
||||
},
|
||||
explanation: { type: 'string', minLength: 1 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
export async function adminQuestionsRoutes(app: FastifyInstance) {
|
||||
const adminQuestionService = new AdminQuestionService(app.db);
|
||||
const { rateLimitOptions } = app;
|
||||
|
||||
app.get(
|
||||
'/questions/pending',
|
||||
{
|
||||
schema: listPendingQuerySchema,
|
||||
config: { rateLimit: rateLimitOptions.apiAuthed },
|
||||
preHandler: [app.authenticate, app.authenticateAdmin],
|
||||
},
|
||||
async (req, reply) => {
|
||||
const { limit, offset } = (req.query as { limit?: number; offset?: number }) ?? {};
|
||||
const result = await adminQuestionService.listPending(limit, offset);
|
||||
return reply.send(result);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/questions/:questionId/approve',
|
||||
{
|
||||
schema: questionIdParamsSchema,
|
||||
config: { rateLimit: rateLimitOptions.apiAuthed },
|
||||
preHandler: [app.authenticate, app.authenticateAdmin],
|
||||
},
|
||||
async (req, reply) => {
|
||||
const { questionId } = req.params as { questionId: string };
|
||||
await adminQuestionService.approve(questionId);
|
||||
return reply.status(204).send();
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/questions/:questionId/reject',
|
||||
{
|
||||
schema: questionIdParamsSchema,
|
||||
config: { rateLimit: rateLimitOptions.apiAuthed },
|
||||
preHandler: [app.authenticate, app.authenticateAdmin],
|
||||
},
|
||||
async (req, reply) => {
|
||||
const { questionId } = req.params as { questionId: string };
|
||||
await adminQuestionService.reject(questionId);
|
||||
return reply.status(204).send();
|
||||
},
|
||||
);
|
||||
|
||||
app.patch(
|
||||
'/questions/:questionId',
|
||||
{
|
||||
schema: editQuestionSchema,
|
||||
config: { rateLimit: rateLimitOptions.apiAuthed },
|
||||
preHandler: [app.authenticate, app.authenticateAdmin],
|
||||
},
|
||||
async (req, reply) => {
|
||||
const { questionId } = req.params as { questionId: string };
|
||||
const body = req.body as EditQuestionInput;
|
||||
const question = await adminQuestionService.edit(questionId, body);
|
||||
return reply.send(question);
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user