Skip to content

Commit

Permalink
#3075-created get unread announcements endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Dec 18, 2024
1 parent 2f337d9 commit 6446202
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/backend/src/controllers/users.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,17 @@ export default class UsersController {
next(error);
}
}

static async removeUserAnnouncement(req: Request, res: Response, next: NextFunction) {
try {
const { userId } = req.params;
const { announcementId } = req.body;
const { organization } = req;

const unreadAnnouncements = await UsersService.removeUserAnnouncement(userId, announcementId, organization);
res.status(200).json(unreadAnnouncements);
} catch (error: unknown) {
next(error);
}
}
}
5 changes: 5 additions & 0 deletions src/backend/src/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ userRouter.post(
nonEmptyString(body('notificationId')),
UsersController.removeUserNotification
);
userRouter.post(
'/:userId/announcements/remove',
nonEmptyString(body('announcementId')),
UsersController.removeUserAnnouncement
);

export default userRouter;
29 changes: 29 additions & 0 deletions src/backend/src/services/users.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,33 @@ export default class UsersService {

return updatedUser.unreadNotifications.map(notificationTransformer);
}

/**
* Removes a announcement from the user's unread announcement
* @param userId id of the user to remove announcement from
* @param announcementId id of the announcement to remove
* @param organization the user's organization
* @returns the user's updated unread announcement
*/
static async removeUserAnnouncement(userId: string, announcementId: string, organization: Organization) {
const requestedUser = await prisma.user.findUnique({
where: { userId }
});

if (!requestedUser) throw new NotFoundException('User', userId);

const updatedUser = await prisma.user.update({
where: { userId },
data: {
unreadAnnouncements: {
disconnect: {
announcementId
}
}
},
include: { unreadAnnouncements: getAnnouncementQueryArgs(organization.organizationId) }
});

return updatedUser.unreadAnnouncements.map(announcementTransformer);
}
}

0 comments on commit 6446202

Please sign in to comment.