Skip to content

Commit

Permalink
start planned training list and fix mobile styling for session creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Aug 16, 2023
1 parent d6ebf81 commit 8c7c09f
Show file tree
Hide file tree
Showing 5 changed files with 62 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 @@ -145,6 +145,7 @@ router.use(
"/training-request",
routerGroup((r: Router) => {
r.get("/", TrainingRequestAdminController.getOpen);
r.get("/planned", TrainingRequestAdminController.getPlanned);
r.get("/training", TrainingRequestAdminController.getOpenTrainingRequests);
r.get("/lesson", TrainingRequestAdminController.getOpenLessonRequests);
r.get("/:uuid", TrainingRequestAdminController.getByUUID);
Expand Down
51 changes: 51 additions & 0 deletions src/controllers/training-request/TrainingRequestAdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TrainingRequest } from "../../models/TrainingRequest";
import { Op } from "sequelize";
import NotificationLibrary from "../../libraries/notification/NotificationLibrary";
import { TrainingType } from "../../models/TrainingType";
import { TrainingSession } from "../../models/TrainingSession";

/**
* Returns all currently open training requests
Expand Down Expand Up @@ -53,6 +54,55 @@ async function getOpen(request: Request, response: Response) {
response.send(trainingRequests);
}

/**
* Returns all the planned sessions of the current user as either mentor or CPT examiner
* The differentiation between mentor & examiner must be done in the frontend
* @param request
* @param response
*/
async function getPlanned(request: Request, response: Response) {
const user: User = request.body.user;

let trainingSession = await TrainingSession.findAll({
where: {
[Op.or]: {
mentor_id: user.id,
cpt_examiner_id: user.id
},
},
include: [
TrainingSession.associations.course,
TrainingSession.associations.training_type,
{
association: TrainingSession.associations.training_session_belongs_to_users,
attributes: ['passed']
}
]
});

/*
For each training session, we need to check whether there is at least one associated participant, who has not yet received a log
If the log doesn't exist, then this acts as a reminder to the mentor to fill out this log. Only when all participants have received a log
entry, will the session be shown as "complete"
*/
trainingSession = trainingSession.filter((trainingSession: TrainingSession) => {
let hasOneNonPassed = false;
if (trainingSession.training_session_belongs_to_users == null || trainingSession.training_session_belongs_to_users.length == 0) {
return true;
}

for (const userSession of trainingSession.training_session_belongs_to_users) {
if (userSession.log_id == null) {
hasOneNonPassed = true;
}
}

return hasOneNonPassed;
})

response.send(trainingSession);
}

/**
* Returns all training requests that the current user is able to mentor based on his mentor groups
* Only returns Trainings (not lessons)
Expand Down Expand Up @@ -186,6 +236,7 @@ async function destroyByUUID(request: Request, response: Response) {

export default {
getOpen,
getPlanned,
getOpenTrainingRequests,
getOpenLessonRequests,
getByUUID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function withdrawFromSessionByUUID(request: Request, response: Response) {
icon: "door-exit",
});

response.send({ message: "OK" });
response.sendStatus(HttpStatusCode.NoContent);
}

export default {
Expand Down
3 changes: 3 additions & 0 deletions src/models/TrainingSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TrainingType } from "./TrainingType";
import { Course } from "./Course";
import { TrainingLog } from "./TrainingLog";
import { TrainingStation } from "./TrainingStation";
import { TrainingSessionBelongsToUsers } from "./through/TrainingSessionBelongsToUsers";

export class TrainingSession extends Model<InferAttributes<TrainingSession>, InferCreationAttributes<TrainingSession>> {
//
Expand Down Expand Up @@ -37,6 +38,7 @@ export class TrainingSession extends Model<InferAttributes<TrainingSession>, Inf
declare training_logs?: NonAttribute<TrainingLog[]>;
declare training_station?: NonAttribute<TrainingStation>;
declare course?: NonAttribute<Course>;
declare training_session_belongs_to_users?: NonAttribute<TrainingSessionBelongsToUsers[]>;

declare static associations: {
users: Association<TrainingSession, User>;
Expand All @@ -45,6 +47,7 @@ export class TrainingSession extends Model<InferAttributes<TrainingSession>, Inf
training_type: Association<TrainingSession, TrainingType>;
training_logs: Association<TrainingSession, TrainingLog>;
training_station: Association<TrainingSession, TrainingStation>;
training_session_belongs_to_users: Association<TrainingSession, TrainingSessionBelongsToUsers>;
course: Association<TrainingSession, Course>;
};
}
Expand Down
6 changes: 6 additions & 0 deletions src/models/associations/TrainingSessionAssociations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export function registerTrainingSessionAssociations() {
foreignKey: "id",
});

TrainingSession.hasMany(TrainingSessionBelongsToUsers, {
as: "training_session_belongs_to_users",
sourceKey: "id",
foreignKey: "training_session_id",
});

TrainingSession.hasOne(User, {
as: "mentor",
sourceKey: "mentor_id",
Expand Down

0 comments on commit 8c7c09f

Please sign in to comment.