Skip to content

Commit

Permalink
Merge pull request #3063 from Northeastern-Electric-Racing/#2998-Caio…
Browse files Browse the repository at this point in the history
…-GetUnreadNotifications

#2998 - Get Unread Notifications
  • Loading branch information
walker-sean authored Dec 13, 2024
2 parents 46c34ea + da6e24f commit d085200
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/backend/src/controllers/users.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,16 @@ export default class UsersController {
next(error);
}
}

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

const unreadNotifications = await UsersService.getUserUnreadNotifications(userId, organization);
res.status(200).json(unreadNotifications);
} catch (error: unknown) {
next(error);
}
}
}
3 changes: 3 additions & 0 deletions src/backend/src/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { writeFileSync } from 'fs';
import WorkPackageTemplatesService from '../services/work-package-template.services';
import RecruitmentServices from '../services/recruitment.services';
import OrganizationsService from '../services/organizations.services';
import NotificationsService from '../services/notifications.services';

const prisma = new PrismaClient();

Expand Down Expand Up @@ -1893,6 +1894,8 @@ const performSeed: () => Promise<void> = async () => {
await RecruitmentServices.createFaq(batman, 'When was FinishLine created?', 'FinishLine was created in 2019', ner);

await RecruitmentServices.createFaq(batman, 'How many developers are working on FinishLine?', '178 as of 2024', ner);

await NotificationsService.sendNotifcationToUsers('Admin!', 'star', [thomasEmrax.userId], ner.organizationId);
};

performSeed()
Expand Down
1 change: 1 addition & 0 deletions src/backend/src/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ userRouter.post(
validateInputs,
UsersController.getManyUserTasks
);
userRouter.get('/:userId/notifications', UsersController.getUserUnreadNotifications);

export default userRouter;
12 changes: 12 additions & 0 deletions src/backend/src/services/users.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import { getAuthUserQueryArgs } from '../prisma-query-args/auth-user.query-args'
import authenticatedUserTransformer from '../transformers/auth-user.transformer';
import { getTaskQueryArgs } from '../prisma-query-args/tasks.query-args';
import taskTransformer from '../transformers/tasks.transformer';
import { getNotificationQueryArgs } from '../prisma-query-args/notifications.query-args';
import notificationTransformer from '../transformers/notifications.transformer';

export default class UsersService {
/**
Expand Down Expand Up @@ -566,4 +568,14 @@ export default class UsersService {
const resolvedTasks = await Promise.all(tasksPromises);
return resolvedTasks.flat();
}

static async getUserUnreadNotifications(userId: string, organization: Organization) {
const requestedUser = await prisma.user.findUnique({
where: { userId },
include: { unreadNotifications: getNotificationQueryArgs(organization.organizationId) }
});
if (!requestedUser) throw new NotFoundException('User', userId);

return requestedUser.unreadNotifications.map(notificationTransformer);
}
}
21 changes: 21 additions & 0 deletions src/backend/tests/unmocked/users.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createTestOrganization, createTestTask, createTestUser, resetUsers } fr
import { batmanAppAdmin } from '../test-data/users.test-data';
import UsersService from '../../src/services/users.services';
import { NotFoundException } from '../../src/utils/errors.utils';
import NotificationsService from '../../src/services/notifications.services';

describe('User Tests', () => {
let orgId: string;
Expand Down Expand Up @@ -48,4 +49,24 @@ describe('User Tests', () => {
expect(userTasks).toStrictEqual([batmanTask, batmanTask]);
});
});

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);
await NotificationsService.sendNotifcationToUsers('test2', 'test2', [testBatman.userId], orgId);

const notifications = await UsersService.getUserUnreadNotifications(testBatman.userId, organization);

expect(notifications).toHaveLength(2);
expect(notifications[0].text).toBe('test1');
expect(notifications[1].text).toBe('test2');
});
});
});

0 comments on commit d085200

Please sign in to comment.