Skip to content

Commit

Permalink
fix some notification stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoerlitz committed Mar 16, 2024
1 parent 3749bff commit f4f5c38
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
52 changes: 29 additions & 23 deletions src/controllers/training-request/TrainingRequestAdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit f4f5c38

Please sign in to comment.