diff --git a/src/Router.ts b/src/Router.ts index f8bbf31..89398e7 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -103,6 +103,7 @@ router.use( r.get("/available", UserCourseController.getAvailableCourses); r.get("/active", UserCourseController.getActiveCourses); r.put("/enrol", UserCourseController.enrolInCourse); + r.get("/completed", UserCourseController.getCompletedCourses); r.delete("/withdraw", UserCourseController.withdrawFromCourseByUUID); r.get("/info", CourseInformationController.getInformationByUUID); @@ -149,6 +150,7 @@ router.use( "/training-session", routerGroup((r: Router) => { r.get("/upcoming", TrainingSessionController.getUpcoming); + r.get("/completed", TrainingSessionController.getCompleted); r.get("/:uuid", TrainingSessionController.getByUUID); r.delete("/withdraw/:uuid", TrainingSessionController.withdrawFromSessionByUUID); }) diff --git a/src/controllers/training-session/TrainingSessionController.ts b/src/controllers/training-session/TrainingSessionController.ts index 3ea5275..66ac26e 100644 --- a/src/controllers/training-session/TrainingSessionController.ts +++ b/src/controllers/training-session/TrainingSessionController.ts @@ -27,6 +27,34 @@ async function getUpcoming(request: Request, response: Response, next: NextFunct } } +async function getCompleted(request: Request, response: Response, next: NextFunction) { + try { + const user: User = response.locals.user; + + const sessions = (await user.getTrainingSessionsWithCourseAndStation()) + .filter(session => { + return session.completed; + }) + .map(session => ({ + uuid: session.uuid, + mentor_id: session.mentor_id, + date: session.date, + course: { + name: session.course?.name, + }, + training_type: { + name: session.training_type?.name, + type: session.training_type?.type, + }, + training_station: session.training_station ?? null, + })); + + response.send(sessions); + } catch (e) { + next(e); + } +} + /** * [User] * Gets all the associated data of a training session @@ -146,6 +174,7 @@ async function withdrawFromSessionByUUID(request: Request, response: Response) { export default { getUpcoming, + getCompleted, getByUUID, withdrawFromSessionByUUID, }; diff --git a/src/controllers/user/UserCourseController.ts b/src/controllers/user/UserCourseController.ts index 1a2c209..f494d27 100644 --- a/src/controllers/user/UserCourseController.ts +++ b/src/controllers/user/UserCourseController.ts @@ -1,5 +1,5 @@ import { User } from "../../models/User"; -import { Request, Response } from "express"; +import { NextFunction, Request, Response } from "express"; import { Course } from "../../models/Course"; import { UsersBelongsToCourses } from "../../models/through/UsersBelongsToCourses"; import { TrainingRequest } from "../../models/TrainingRequest"; @@ -50,6 +50,29 @@ async function getActiveCourses(request: Request, response: Response) { response.send(courses); } +async function getCompletedCourses(request: Request, response: Response, next: NextFunction) { + try { + const reqUser: User = response.locals.user; + + const userInCourses = await UsersBelongsToCourses.findAll({ + where: { + user_id: reqUser.id, + completed: true, + }, + include: [UsersBelongsToCourses.associations.course], + }); + + let courses: Course[] = []; + for (const c of userInCourses) { + if (c.course != null) courses.push(c.course); + } + + response.send(courses); + } catch (e) { + next(e); + } +} + /** * Returns all courses that are associated to the current user (i.e. enrolled in course or completed) * @param request @@ -154,6 +177,7 @@ async function withdrawFromCourseByUUID(request: Request, response: Response) { export default { getAvailableCourses, getActiveCourses, + getCompletedCourses, getMyCourses, enrolInCourse, withdrawFromCourseByUUID, diff --git a/src/models/User.ts b/src/models/User.ts index c8d6835..73b4c6d 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -132,7 +132,7 @@ export class User extends Model, InferCreationAttributes