From 2e50c68434f794cec2e95eb5ebbde68885108173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikolas=20G=C3=B6rlitz?= Date: Sun, 17 Mar 2024 10:53:41 +0100 Subject: [PATCH] my trainings --- src/Router.ts | 1 + .../TrainingSessionAdminController.ts | 34 ++++++++++++++++++- src/models/TrainingSession.ts | 10 ++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/Router.ts b/src/Router.ts index 1802ccb..f8bbf31 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -201,6 +201,7 @@ router.use( routerGroup((r: Router) => { r.get("/planned", TrainingSessionAdminController.getPlanned); r.post("/training", TrainingSessionAdminController.createTrainingSession); + r.get("/my", TrainingSessionAdminController.getMyTrainingSessions); r.delete("/training", TrainingSessionAdminController.deleteTrainingSession); r.get("/:uuid", TrainingSessionAdminController.getByUUID); r.patch("/:uuid", TrainingSessionAdminController.updateByUUID); diff --git a/src/controllers/training-session/TrainingSessionAdminController.ts b/src/controllers/training-session/TrainingSessionAdminController.ts index ac3bc9b..b97341f 100644 --- a/src/controllers/training-session/TrainingSessionAdminController.ts +++ b/src/controllers/training-session/TrainingSessionAdminController.ts @@ -243,9 +243,11 @@ async function getByUUID(request: Request, response: Response) { const user: User = response.locals.user; const params = request.params as { uuid: string }; + const id = await TrainingSession.getIDFromUUID(params.uuid); + const trainingSession = await TrainingSession.findOne({ where: { - uuid: params.uuid, + id: id, }, include: [ TrainingSession.associations.course, @@ -255,6 +257,18 @@ async function getByUUID(request: Request, response: Response) { through: { attributes: [], }, + include: [ + { + association: User.associations.training_logs, + through: { + where: { + training_session_id: id, + }, + attributes: ["passed"], + }, + attributes: ["uuid"], + }, + ], }, { association: TrainingSession.associations.training_type, @@ -580,6 +594,23 @@ async function getAvailableMentorsByUUID(request: Request, response: Response, n } } +async function getMyTrainingSessions(request: Request, response: Response, next: NextFunction) { + try { + const user: User = response.locals.user; + + const sessions = await TrainingSession.findAll({ + where: { + mentor_id: user.id, + }, + include: [TrainingSession.associations.users, TrainingSession.associations.course, TrainingSession.associations.training_type], + }); + + response.send(sessions); + } catch (e) { + next(e); + } +} + export default { getByUUID, createTrainingSession, @@ -591,4 +622,5 @@ export default { getCourseTrainingTypes, getPlanned, getAvailableMentorsByUUID, + getMyTrainingSessions, }; diff --git a/src/models/TrainingSession.ts b/src/models/TrainingSession.ts index a8a93e9..de207f9 100644 --- a/src/models/TrainingSession.ts +++ b/src/models/TrainingSession.ts @@ -53,6 +53,16 @@ export class TrainingSession extends Model, Inf course: Association; cpt: Association; }; + + static async getIDFromUUID(uuid: string) { + const trainingSession = await TrainingSession.findOne({ + where: { + uuid: uuid, + }, + }); + + return trainingSession?.id ?? -1; + } } TrainingSession.init(TRAINING_SESSION_TABLE_ATTRIBUTES, {