From 66f5837a16e4a267cfbdc8232d1cbe862efebfab Mon Sep 17 00:00:00 2001 From: caiodasilva2005 Date: Fri, 20 Dec 2024 08:48:20 -0500 Subject: [PATCH] #3074-using current user --- .../src/controllers/users.controllers.ts | 21 +++++----- src/backend/src/routes/users.routes.ts | 10 ++--- src/backend/src/services/users.services.ts | 42 +++++++++++-------- src/backend/tests/unmocked/users.test.ts | 22 ---------- 4 files changed, 38 insertions(+), 57 deletions(-) diff --git a/src/backend/src/controllers/users.controllers.ts b/src/backend/src/controllers/users.controllers.ts index cc084c714e..b3fee931c2 100644 --- a/src/backend/src/controllers/users.controllers.ts +++ b/src/backend/src/controllers/users.controllers.ts @@ -194,10 +194,9 @@ export default class UsersController { static async getUserUnreadNotifications(req: Request, res: Response, next: NextFunction) { try { - const { userId } = req.params; - const { organization } = req; + const { organization, currentUser } = req; - const unreadNotifications = await UsersService.getUserUnreadNotifications(userId, organization); + const unreadNotifications = await UsersService.getUserUnreadNotifications(currentUser.userId, organization); res.status(200).json(unreadNotifications); } catch (error: unknown) { next(error); @@ -206,11 +205,14 @@ export default class UsersController { static async removeUserNotification(req: Request, res: Response, next: NextFunction) { try { - const { userId } = req.params; - const { notificationId } = req.body; - const { organization } = req; + const { notificationId } = req.params; + const { organization, currentUser } = req; - const unreadNotifications = await UsersService.removeUserNotification(userId, notificationId, organization); + const unreadNotifications = await UsersService.removeUserNotification( + currentUser.userId, + notificationId, + organization + ); res.status(200).json(unreadNotifications); } catch (error: unknown) { next(error); @@ -219,10 +221,9 @@ export default class UsersController { static async getUserUnreadAnnouncements(req: Request, res: Response, next: NextFunction) { try { - const { userId } = req.params; - const { organization } = req; + const { organization, currentUser } = req; - const unreadAnnouncements = await UsersService.getUserUnreadAnnouncements(userId, organization); + const unreadAnnouncements = await UsersService.getUserUnreadAnnouncements(currentUser.userId, organization); res.status(200).json(unreadAnnouncements); } catch (error: unknown) { next(error); diff --git a/src/backend/src/routes/users.routes.ts b/src/backend/src/routes/users.routes.ts index 802d586500..96be49828c 100644 --- a/src/backend/src/routes/users.routes.ts +++ b/src/backend/src/routes/users.routes.ts @@ -54,12 +54,8 @@ userRouter.post( validateInputs, UsersController.getManyUserTasks ); -userRouter.get('/:userId/notifications', UsersController.getUserUnreadNotifications); -userRouter.get('/:userId/announcements', UsersController.getUserUnreadAnnouncements); -userRouter.post( - '/:userId/notifications/remove', - nonEmptyString(body('notificationId')), - UsersController.removeUserNotification -); +userRouter.get('/notifications/current-user', UsersController.getUserUnreadNotifications); +userRouter.get('/announcements/current-user', UsersController.getUserUnreadAnnouncements); +userRouter.post('/notifications/:notificationId/remove', UsersController.removeUserNotification); export default userRouter; diff --git a/src/backend/src/services/users.services.ts b/src/backend/src/services/users.services.ts index fff25ef56b..b537d019d2 100644 --- a/src/backend/src/services/users.services.ts +++ b/src/backend/src/services/users.services.ts @@ -578,45 +578,49 @@ export default class UsersService { * @returns the unread notifications of the user */ static async getUserUnreadNotifications(userId: string, organization: Organization) { - const requestedUser = await prisma.user.findUnique({ - where: { userId }, - include: { unreadNotifications: getNotificationQueryArgs(organization.organizationId) } + const unreadNotifications = await prisma.notification.findMany({ + where: { + users: { + some: { userId } + } + }, + ...getNotificationQueryArgs(organization.organizationId) }); - if (!requestedUser) throw new NotFoundException('User', userId); - return requestedUser.unreadNotifications.map(notificationTransformer); + if (!unreadNotifications) throw new HttpException(404, 'User Unread Notifications Not Found'); + + return unreadNotifications.map(notificationTransformer); } /** * Gets all of a user's unread announcements - * @param userId id of user to get unread announcements from + * @param userId id of the current user * @param organization the user's orgainzation * @returns the unread announcements of the user */ static async getUserUnreadAnnouncements(userId: string, organization: Organization) { - const requestedUser = await prisma.user.findUnique({ - where: { userId }, - include: { unreadAnnouncements: getAnnouncementQueryArgs(organization.organizationId) } + const unreadAnnouncements = await prisma.announcement.findMany({ + where: { + usersReceived: { + some: { userId } + } + }, + ...getAnnouncementQueryArgs(organization.organizationId) }); - if (!requestedUser) throw new NotFoundException('User', userId); - return requestedUser.unreadAnnouncements.map(announcementTransformer); + if (!unreadAnnouncements) throw new HttpException(404, 'User Unread Announcements Not Found'); + + return unreadAnnouncements.map(announcementTransformer); } /** * Removes a notification from the user's unread notifications - * @param userId id of the user to remove notification from + * @param userId id of the current user * @param notificationId id of the notification to remove * @param organization the user's organization * @returns the user's updated unread notifications */ static async removeUserNotification(userId: string, notificationId: 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: { @@ -629,6 +633,8 @@ export default class UsersService { include: { unreadNotifications: getNotificationQueryArgs(organization.organizationId) } }); + if (!updatedUser) throw new HttpException(404, `Failed to remove notication: ${notificationId}`); + return updatedUser.unreadNotifications.map(notificationTransformer); } } diff --git a/src/backend/tests/unmocked/users.test.ts b/src/backend/tests/unmocked/users.test.ts index 789109a2ae..bd0b934270 100644 --- a/src/backend/tests/unmocked/users.test.ts +++ b/src/backend/tests/unmocked/users.test.ts @@ -52,12 +52,6 @@ describe('User Tests', () => { }); describe('Get Notifications', () => { - it('fails on invalid user id', async () => { - await expect(async () => await UsersService.getUserUnreadNotifications('1', organization)).rejects.toThrow( - new NotFoundException('User', '1') - ); - }); - it('Succeeds and gets user notifications', async () => { const testBatman = await createTestUser(batmanAppAdmin, orgId); await NotificationsService.sendNotifcationToUsers('test1', 'test1', [testBatman.userId], orgId); @@ -72,16 +66,6 @@ describe('User Tests', () => { }); describe('Remove Notifications', () => { - it('Fails with invalid user', async () => { - const testBatman = await createTestUser(batmanAppAdmin, orgId); - await NotificationsService.sendNotifcationToUsers('test1', 'test1', [testBatman.userId], orgId); - const notifications = await UsersService.getUserUnreadNotifications(testBatman.userId, organization); - - await expect( - async () => await UsersService.removeUserNotification('1', notifications[0].notificationId, organization) - ).rejects.toThrow(new NotFoundException('User', '1')); - }); - it('Succeeds and removes user notification', async () => { const testBatman = await createTestUser(batmanAppAdmin, orgId); await NotificationsService.sendNotifcationToUsers('test1', 'test1', [testBatman.userId], orgId); @@ -105,12 +89,6 @@ describe('User Tests', () => { }); describe('Get Announcements', () => { - it('fails on invalid user id', async () => { - await expect(async () => await UsersService.getUserUnreadAnnouncements('1', organization)).rejects.toThrow( - new NotFoundException('User', '1') - ); - }); - it('Succeeds and gets user announcements', async () => { const testBatman = await createTestUser(batmanAppAdmin, orgId); await AnnouncementService.createAnnouncement(