Skip to content

Commit

Permalink
Add more views
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Mar 17, 2024
1 parent 2e50c68 commit 6aedd54
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
})
Expand Down
29 changes: 29 additions & 0 deletions src/controllers/training-session/TrainingSessionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -146,6 +174,7 @@ async function withdrawFromSessionByUUID(request: Request, response: Response) {

export default {
getUpcoming,
getCompleted,
getByUUID,
withdrawFromSessionByUUID,
};
26 changes: 25 additions & 1 deletion src/controllers/user/UserCourseController.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -154,6 +177,7 @@ async function withdrawFromCourseByUUID(request: Request, response: Response) {
export default {
getAvailableCourses,
getActiveCourses,
getCompletedCourses,
getMyCourses,
enrolInCourse,
withdrawFromCourseByUUID,
Expand Down
2 changes: 1 addition & 1 deletion src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
through: {
attributes: [],
},
include: [TrainingSession.associations.course, TrainingSession.associations.training_station],
include: [TrainingSession.associations.course, TrainingSession.associations.training_station, TrainingSession.associations.training_type],
},
],
});
Expand Down

0 comments on commit 6aedd54

Please sign in to comment.