Skip to content

Commit

Permalink
#3074-using current user
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Dec 20, 2024
1 parent 0dfedd2 commit 66f5837
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 57 deletions.
21 changes: 11 additions & 10 deletions src/backend/src/controllers/users.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 3 additions & 7 deletions src/backend/src/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
42 changes: 24 additions & 18 deletions src/backend/src/services/users.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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);
}
}
22 changes: 0 additions & 22 deletions src/backend/tests/unmocked/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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(
Expand Down

0 comments on commit 66f5837

Please sign in to comment.