Skip to content

Commit

Permalink
update overview
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Mar 16, 2024
1 parent f4f5c38 commit 74ebec4
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 17 deletions.
11 changes: 4 additions & 7 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import UserEndorsementAdminController from "./controllers/user/UserEndorsementAd
import UserStatisticsController from "./controllers/user/UserStatisticsController";
import SyslogAdminController from "./controllers/admin-logs/SyslogAdminController";
import JoblogAdminController from "./controllers/admin-logs/JoblogAdminController";
import UserInformationController from "./controllers/user/UserInformationController";

const routerGroup = (callback: (router: Router) => void) => {
const router = Router();
Expand All @@ -45,12 +46,6 @@ const routerGroup = (callback: (router: Router) => void) => {

export const router = Router();

router.post("/test", async (req, res) => {
console.log(req.body);

res.sendStatus(400);
});

router.use(
"/auth",
routerGroup((r: Router) => {
Expand All @@ -69,12 +64,13 @@ router.use(
r.use(authMiddleware);

r.get("/user/update", LoginController.updateUserData);

r.patch("/settings", UserSettingsController.updateSettings);

r.get("/sessions", SessionController.getUserSessions);
r.delete("/session", SessionController.deleteUserSession);

r.get("/overview", UserInformationController.getOverviewStatistics);

r.get("/gdpr", GDPRController.getData);

r.use(
Expand Down Expand Up @@ -152,6 +148,7 @@ router.use(
r.use(
"/training-session",
routerGroup((r: Router) => {
r.get("/upcoming", TrainingSessionController.getUpcoming);
r.get("/:uuid", TrainingSessionController.getByUUID);
r.delete("/withdraw/:uuid", TrainingSessionController.withdrawFromSessionByUUID);
})
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/fast-track/FastTrackAdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Validator, { ValidationTypeEnum } from "../../utility/Validator";
async function getAll(request: Request, response: Response, next: NextFunction) {
try {
const user: User = response.locals.user;
PermissionHelper.checkUserHasPermission(user, "atd.fast-track.all.view", true);
PermissionHelper.checkUserHasPermission(user, "atd.fast_track.view", true);

const fastTracks = await FastTrackRequest.findAll({
include: [FastTrackRequest.associations.user],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async function destroyByUUID(request: Request, response: Response, next: NextFun
});

if (trainingRequest == null) {
response.status(404).send({message: "Training request with this UUID not found"});
response.status(404).send({ message: "Training request with this UUID not found" });
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ async function deleteTrainingSession(request: Request, response: Response) {
return;
}

for (const participant of (session?.users ?? [])) {
for (const participant of session?.users ?? []) {
await NotificationLibrary.sendUserNotification({
user_id: participant.id,
author_id: user.id,
Expand Down
23 changes: 22 additions & 1 deletion src/controllers/training-session/TrainingSessionController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request, Response } from "express";
import { NextFunction, Request, Response } from "express";
import { User } from "../../models/User";
import { TrainingSession } from "../../models/TrainingSession";
import { TrainingSessionBelongsToUsers } from "../../models/through/TrainingSessionBelongsToUsers";
Expand All @@ -7,6 +7,26 @@ import dayjs from "dayjs";
import NotificationLibrary from "../../libraries/notification/NotificationLibrary";
import { HttpStatusCode } from "axios";

/**
* Gets a list of upcoming training sessions
* @param request
* @param response
* @param next
*/
async function getUpcoming(request: Request, response: Response, next: NextFunction) {
try {
const user: User = response.locals.user;

const sessions: TrainingSession[] = (await user.getTrainingSessionsWithCourseAndStation()).filter(session => {
return !session.completed && dayjs.utc(session.date).isAfter(dayjs.utc());
});

response.send(sessions);
} catch (e) {
next(e);
}
}

/**
* [User]
* Gets all the associated data of a training session
Expand Down Expand Up @@ -125,6 +145,7 @@ async function withdrawFromSessionByUUID(request: Request, response: Response) {
}

export default {
getUpcoming,
getByUUID,
withdrawFromSessionByUUID,
};
63 changes: 63 additions & 0 deletions src/controllers/user/UserInformationController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { NextFunction, Request, Response } from "express";
import { User } from "../../models/User";
import { HttpStatusCode } from "axios";
import { EndorsementGroup } from "../../models/EndorsementGroup";
import { TrainingSession } from "../../models/TrainingSession";
import dayjs from "dayjs";

async function getOverviewStatistics(request: Request, response: Response, next: NextFunction) {
try {
const user: User = response.locals.user;

const userInformation = await User.findOne({
where: {
id: user.id,
},
include: [
{
association: User.associations.training_sessions,
through: {
attributes: [],
},
include: [TrainingSession.associations.course, TrainingSession.associations.training_station],
},
{
association: User.associations.endorsement_groups,
through: {
attributes: [],
},
include: [
{
association: EndorsementGroup.associations.stations,
through: {
attributes: [],
},
},
],
},
],
});

if (userInformation == null) {
response.sendStatus(HttpStatusCode.NotFound);
return;
}

const sessions = userInformation.training_sessions as TrainingSession[];
const completedSessions = sessions?.filter(session => session.completed);
const upcomingSessions = sessions.filter(session => !session.completed && dayjs.utc(session.date).isAfter(dayjs.utc()));

response.send({
count: sessions?.length ?? 0,
completedCount: completedSessions?.length ?? 0,
upcomingSessions: upcomingSessions,
endorsementGroups: userInformation.endorsement_groups ?? [],
});
} catch (e) {
next(e);
}
}

export default {
getOverviewStatistics,
};
2 changes: 2 additions & 0 deletions src/libraries/notification/NotificationLibrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type UserNotificationType = {
};

async function sendUserNotification(notificationType: UserNotificationType) {
// Todo: Add optional email as well!

await Notification.create({
uuid: generateUUID(),
user_id: notificationType.user_id,
Expand Down
6 changes: 1 addition & 5 deletions src/models/Role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { Association, CreationOptional, InferAttributes, InferCreationAttributes
import { sequelize } from "../core/Sequelize";
import { Permission } from "./Permission";
import { User } from "./User";
import {
ROLE_BELONGS_TO_USER_TABLE_NAME,
ROLE_TABLE_ATTRIBUTES,
ROLE_TABLE_NAME
} from "../../db/migrations/20221115171263-create-permission-tables";
import { ROLE_BELONGS_TO_USER_TABLE_NAME, ROLE_TABLE_ATTRIBUTES, ROLE_TABLE_NAME } from "../../db/migrations/20221115171263-create-permission-tables";

export class Role extends Model<InferAttributes<Role>, InferCreationAttributes<Role>> {
//
Expand Down
19 changes: 18 additions & 1 deletion src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
}

async getTrainingSessions(): Promise<TrainingSession[]> {
const user = await User.findOne({
where: {
id: this.id,
},
include: [
{
association: User.associations.training_sessions,
as: "training_sessions",
},
],
});

return user?.training_sessions ?? [];
}

async getTrainingSessionsWithCourseAndStation(): Promise<TrainingSession[]> {
const user = await User.findOne({
where: {
id: this.id,
Expand All @@ -114,8 +130,9 @@ export class User extends Model<InferAttributes<User>, InferCreationAttributes<U
association: User.associations.training_sessions,
as: "training_sessions",
through: {
as: "through",
attributes: [],
},
include: [TrainingSession.associations.course, TrainingSession.associations.training_station],
},
],
});
Expand Down

0 comments on commit 74ebec4

Please sign in to comment.