From 51a59d051a8f6a72fc39eada7e28d97e140d63d5 Mon Sep 17 00:00:00 2001 From: Artur Poffo Date: Fri, 23 Feb 2024 19:23:47 -0300 Subject: [PATCH] Feat(Module, Enrollment, Route): Mark module as completed route --- README.md | 2 +- .../controllers/mark-module-as-completed.ts | 41 +++++++++++++++++++ src/infra/http/routes/enrollment.ts | 2 + 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/infra/http/controllers/mark-module-as-completed.ts diff --git a/README.md b/README.md index 7f92c77..85590b9 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ ### Enrollments - [x] POST /courses/:courseId/enroll - Enroll to course - [x] POST /enrollments/:enrollmentId/modules/:moduleId/complete - Mark module as completed -- [ ] POST /enrollments/:enrollmentId/classes/:classId/complete - Mark class as completed +- [x] POST /enrollments/:enrollmentId/classes/:classId/complete - Mark class as completed - [ ] POST /enrollments/:enrollmentId/complete - Mark enrollment as completed - [x] GET /courses/:courseId/students/:studentId/enrollments - Get enrollment of a student on a course - [x] DELETE /enrollments/:enrollmentId - Cancel enrollment diff --git a/src/infra/http/controllers/mark-module-as-completed.ts b/src/infra/http/controllers/mark-module-as-completed.ts new file mode 100644 index 0000000..6c60a3f --- /dev/null +++ b/src/infra/http/controllers/mark-module-as-completed.ts @@ -0,0 +1,41 @@ +import { NotAllowedError } from '@/core/errors/errors/not-allowed-error' +import { ResourceNotFoundError } from '@/core/errors/errors/resource-not-found-error' +import { AllClassesInTheModuleMustBeMarkedAsCompleted } from '@/domain/course-management/application/use-cases/errors/all-classes-in-the-module-must-be-marked-as-completed' +import { makeMarkModuleAsCompletedUseCase } from '@/infra/use-cases/factories/make-mark-module-as-completed-use-case' +import { type FastifyReply, type FastifyRequest } from 'fastify' +import { z } from 'zod' + +const markModuleAsCompletedParamsSchema = z.object({ + enrollmentId: z.string().uuid(), + moduleId: z.string().uuid() +}) + +export async function markModuleAsCompletedController(request: FastifyRequest, reply: FastifyReply) { + const { enrollmentId, moduleId } = markModuleAsCompletedParamsSchema.parse(request.params) + const { sub: studentId } = request.user + + const markModuleAsCompletedUseCase = makeMarkModuleAsCompletedUseCase() + + const result = await markModuleAsCompletedUseCase.exec({ + enrollmentId, + moduleId, + studentId + }) + + if (result.isLeft()) { + const error = result.value + + switch (error.constructor) { + case ResourceNotFoundError: + return await reply.status(404).send({ message: error.message }) + case NotAllowedError: + return await reply.status(401).send({ message: error.message }) + case AllClassesInTheModuleMustBeMarkedAsCompleted: + return await reply.status(403).send({ message: error.message }) + default: + return await reply.status(500).send({ message: error.message }) + } + } + + return await reply.status(201).send() +} diff --git a/src/infra/http/routes/enrollment.ts b/src/infra/http/routes/enrollment.ts index 843d8bd..3fd8177 100644 --- a/src/infra/http/routes/enrollment.ts +++ b/src/infra/http/routes/enrollment.ts @@ -3,6 +3,7 @@ import { cancelEnrollmentController } from '../controllers/cancel-enrollment' import { enrollToCourseController } from '../controllers/enroll-to-course' import { getEnrollmentDetailsController } from '../controllers/get-enrollment-details' import { markClassAsCompletedController } from '../controllers/mark-class-as-completed' +import { markModuleAsCompletedController } from '../controllers/mark-module-as-completed' import { verifyJwt } from '../middlewares/verify-jwt' import { verifyUserRole } from '../middlewares/verify-user-role' @@ -11,6 +12,7 @@ export async function enrollmentRoutes(app: FastifyInstance) { app.post('/courses/:courseId/enroll', { onRequest: [verifyJwt, verifyUserRole('STUDENT')] }, enrollToCourseController) app.post('/enrollments/:enrollmentId/classes/:classId', { onRequest: [verifyJwt, verifyUserRole('STUDENT')] }, markClassAsCompletedController) + app.post('/enrollments/:enrollmentId/modules/:moduleId', { onRequest: [verifyJwt, verifyUserRole('STUDENT')] }, markModuleAsCompletedController) app.delete('/enrollments/:enrollmentId', { onRequest: [verifyJwt, verifyUserRole('STUDENT')] }, cancelEnrollmentController) }