Skip to content

Commit

Permalink
my trainings
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Mar 17, 2024
1 parent fb68783 commit 2e50c68
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -591,4 +622,5 @@ export default {
getCourseTrainingTypes,
getPlanned,
getAvailableMentorsByUUID,
getMyTrainingSessions,
};
10 changes: 10 additions & 0 deletions src/models/TrainingSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ export class TrainingSession extends Model<InferAttributes<TrainingSession>, Inf
course: Association<TrainingSession, Course>;
cpt: Association<TrainingSession, CptSession>;
};

static async getIDFromUUID(uuid: string) {
const trainingSession = await TrainingSession.findOne({
where: {
uuid: uuid,
},
});

return trainingSession?.id ?? -1;
}
}

TrainingSession.init(TRAINING_SESSION_TABLE_ATTRIBUTES, {
Expand Down

0 comments on commit 2e50c68

Please sign in to comment.