From f4f5c38fd0fd8dba17cf6b78f16c44302ef81652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikolas=20G=C3=B6rlitz?= Date: Sat, 16 Mar 2024 02:16:28 +0100 Subject: [PATCH] fix some notification stuff --- .../TrainingRequestAdminController.ts | 52 +++++++++++-------- .../TrainingSessionAdminController.ts | 6 +-- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/controllers/training-request/TrainingRequestAdminController.ts b/src/controllers/training-request/TrainingRequestAdminController.ts index dae6c31..decd406 100644 --- a/src/controllers/training-request/TrainingRequestAdminController.ts +++ b/src/controllers/training-request/TrainingRequestAdminController.ts @@ -155,34 +155,40 @@ async function getByUUID(request: Request, response: Response) { * Allows a mentor (or above) to delete the training request of a user based on its UUID. * @param request * @param response + * @param next */ -async function destroyByUUID(request: Request, response: Response) { - const trainingRequestUUID: string = request.params.uuid; - - const trainingRequest: TrainingRequest | null = await TrainingRequest.findOne({ - where: { - uuid: trainingRequestUUID, - }, - include: [TrainingRequest.associations.training_type], - }); +async function destroyByUUID(request: Request, response: Response, next: NextFunction) { + try { + const user: User = response.locals.user; + const trainingRequestUUID: string = request.params.uuid; + + const trainingRequest: TrainingRequest | null = await TrainingRequest.findOne({ + where: { + uuid: trainingRequestUUID, + }, + include: [TrainingRequest.associations.training_type], + }); - if (trainingRequest == null) { - response.status(404).send({ message: "Training request with this UUID not found" }); - return; - } + if (trainingRequest == null) { + response.status(404).send({message: "Training request with this UUID not found"}); + return; + } - await trainingRequest.destroy(); + await trainingRequest.destroy(); - await NotificationLibrary.sendUserNotification({ - user_id: trainingRequest.user_id, - message_de: `Deine Trainingsanfrage für "${trainingRequest.training_type?.name}" wurde von $author gelöscht`, - message_en: `$author has deleted your training request for "${trainingRequest.training_type?.name}"`, - author_id: response.locals.user.id, - severity: "default", - icon: "trash", - }); + await NotificationLibrary.sendUserNotification({ + user_id: trainingRequest.user_id, + message_de: `Deine Trainingsanfrage für "${trainingRequest.training_type?.name}" wurde von ${user.first_name} ${user.last_name} gelöscht`, + message_en: `${user.first_name} ${user.last_name} has deleted your training request for "${trainingRequest.training_type?.name}"`, + author_id: response.locals.user.id, + severity: "default", + icon: "trash", + }); - response.send({ message: "OK" }); + response.sendStatus(HttpStatusCode.NoContent); + } catch (e) { + next(e); + } } export default { diff --git a/src/controllers/training-session/TrainingSessionAdminController.ts b/src/controllers/training-session/TrainingSessionAdminController.ts index e428e96..231b25f 100644 --- a/src/controllers/training-session/TrainingSessionAdminController.ts +++ b/src/controllers/training-session/TrainingSessionAdminController.ts @@ -204,8 +204,8 @@ async function deleteTrainingSession(request: Request, response: Response) { return; } - session.users?.forEach(participant => { - NotificationLibrary.sendUserNotification({ + for (const participant of (session?.users ?? [])) { + await NotificationLibrary.sendUserNotification({ user_id: participant.id, author_id: user.id, message_de: `Deine Session im Kurs ${session.course?.name} am ${dayjs @@ -217,7 +217,7 @@ async function deleteTrainingSession(request: Request, response: Response) { severity: "danger", icon: "alert-triangle", }); - }); + } // Update training requests to reflect the now non-existent session await TrainingRequest.update(