feat: expand admin and instructor tools
All checks were successful
CI / Validate frontend and API (push) Successful in 50s
CI / Build and publish Docker images (push) Successful in 20s

This commit is contained in:
Cauê Faleiros
2026-09-01 13:19:29 -03:00
parent caaf904281
commit 4736fb5208
13 changed files with 385 additions and 22 deletions

View File

@@ -19,6 +19,8 @@ const commentSchema = z.object({
lessonId: z.string().uuid().nullable().optional(),
text: z.string().trim().min(1).max(4000),
});
const commentParamsSchema = z.object({ commentId: z.string().uuid() });
const replySchema = z.object({ text: z.string().trim().min(1).max(4000) });
const ensurePublishedCourse = async (courseId: string) => {
const result = await pool.query(
@@ -29,6 +31,16 @@ const ensurePublishedCourse = async (courseId: string) => {
};
export const learningRoutes: FastifyPluginAsync = async (app) => {
const canModerateComment = async (commentId: string, user: { id: string; role: string }) => {
const result = await pool.query<{ instructor_id: string }>(
`select c.instructor_id
from comments cm join courses c on c.id = cm.course_id
where cm.id = $1`,
[commentId],
);
const comment = result.rows[0];
return Boolean(comment && (user.role === 'admin' || comment.instructor_id === user.id));
};
app.get('/courses/:courseId/progress', { preHandler: app.authenticate }, async (request, reply) => {
const { courseId } = courseParamsSchema.parse(request.params);
if (!(await ensurePublishedCourse(courseId))) {
@@ -123,4 +135,24 @@ export const learningRoutes: FastifyPluginAsync = async (app) => {
);
return reply.code(201).send({ data: result.rows[0] });
});
app.put('/comments/:commentId/reply', { preHandler: app.requireRoles(['instructor', 'admin']) }, async (request, reply) => {
const { commentId } = commentParamsSchema.parse(request.params);
const input = replySchema.parse(request.body);
if (!(await canModerateComment(commentId, request.user))) return reply.code(403).send({ error: 'You cannot reply to this comment' });
await pool.query(
`insert into comment_replies (comment_id, author_id, body)
values ($1, $2, $3)
on conflict (comment_id) do update set author_id = excluded.author_id, body = excluded.body`,
[commentId, request.user.id, input.text],
);
return { data: { id: commentId } };
});
app.delete('/comments/:commentId', { preHandler: app.requireRoles(['instructor', 'admin']) }, async (request, reply) => {
const { commentId } = commentParamsSchema.parse(request.params);
if (!(await canModerateComment(commentId, request.user))) return reply.code(403).send({ error: 'You cannot moderate this comment' });
await pool.query('delete from comments where id = $1', [commentId]);
return reply.code(204).send();
});
};