Skip to content

Notifications: Notification popup appeared, but zero in list screen #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/controllers/notificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ export const getNotifications = async (req: Request, res: Response) => {
const skip = req.query.skip ? Number(req.query.skip) : 0;
const limit = req.query.limit ? Number(req.query.limit) : 20;

const status = ['cleared', 'uncleared'].includes(req.query.status as string)
? (req.query.status as 'cleared' | 'uncleared') : undefined;

try {
const notifications = await notificationService.getNotifications(pi_uid, skip, limit);
const notifications = await notificationService.getNotifications(pi_uid, skip, limit, status);
return res.status(200).json(notifications);
} catch (error) {
logger.error('Failed to get notifications', error);
Expand Down
7 changes: 7 additions & 0 deletions src/routes/notification.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const notificationRoutes = Router();
* type: integer
* default: 20
* description: Maximum number of notifications to return
* - name: status
* in: query
* required: false
* schema:
* type: string
* enum: [cleared, uncleared]
* description: Filter notifications by cleared vs uncleared status, if applicable.
* responses:
* 200:
* description: Successful response
Expand Down
17 changes: 15 additions & 2 deletions src/services/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ export const addNotification = async (pi_uid: string, reason: string): Promise<I
}
};

export const getNotifications = async (pi_uid: string, skip: number, limit: number): Promise<INotification[]> => {
export const getNotifications = async (
pi_uid: string,
skip: number,
limit: number,
status?: 'cleared' | 'uncleared'
): Promise<INotification[]> => {
try {
const notifications = await Notification.find({ pi_uid })
const filter: any = { pi_uid };

if (status === 'cleared') {
filter.is_cleared = true;
} else if (status === 'uncleared') {
filter.is_cleared = false;
}

const notifications = await Notification.find(filter)
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
Expand Down
47 changes: 41 additions & 6 deletions test/controllers/notificationController.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('notificationController', () => {
};
});

it('should get notifications (pagination provided) and return [200] on success', async () => {
it('should get all notifications (pagination provided) and return [200] on success', async () => {
const mockNotifications = [
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: false, reason: 'TEST_REASON_A' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_B' }
Expand All @@ -95,25 +95,60 @@ describe('notificationController', () => {

await getNotifications(req, res);

expect(notificationService.getNotifications).toHaveBeenCalledWith('0a0a0a-0a0a-0a0a', 5, 10);
expect(notificationService.getNotifications).toHaveBeenCalledWith('0a0a0a-0a0a-0a0a', 5, 10, undefined);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(mockNotifications);
});

it('should get notifications (pagination not provided) and return [200] on success', async () => {
req.query = { }; // no skip + limit params
it('should get only cleared notifications (pagination provided) and return [200] on success', async () => {
req.query.status = 'cleared';
const mockNotifications = [
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: false, reason: 'TEST_REASON_A' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_B' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_C' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_D' },
];

(notificationService.getNotifications as jest.Mock).mockResolvedValue(mockNotifications);

await getNotifications(req, res);

expect(notificationService.getNotifications).toHaveBeenCalledWith('0a0a0a-0a0a-0a0a', 0, 20);
expect(notificationService.getNotifications).toHaveBeenCalledWith('0a0a0a-0a0a-0a0a', 5, 10, 'cleared');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(mockNotifications);
});

it('should get only uncleared notifications (pagination provided) and return [200] on success', async () => {
req.query.status = 'uncleared';
const mockNotifications = [
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: false, reason: 'TEST_REASON_A' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_B' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_C' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: false, reason: 'TEST_REASON_D' },
];

(notificationService.getNotifications as jest.Mock).mockResolvedValue(mockNotifications);

await getNotifications(req, res);

expect(notificationService.getNotifications).toHaveBeenCalledWith('0a0a0a-0a0a-0a0a', 5, 10, 'uncleared');
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(mockNotifications);
});

it('should get all notifications for invalid status (pagination provided) and return [200] on success', async () => {
req.query.status = 'invalid';
const mockNotifications = [
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: false, reason: 'TEST_REASON_A' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_B' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: true, reason: 'TEST_REASON_C' },
{ pi_uid: '0a0a0a-0a0a-0a0a', is_cleared: false, reason: 'TEST_REASON_D' },
];

(notificationService.getNotifications as jest.Mock).mockResolvedValue(mockNotifications);

await getNotifications(req, res);

expect(notificationService.getNotifications).toHaveBeenCalledWith('0a0a0a-0a0a-0a0a', 5, 10, undefined);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(mockNotifications);
});
Expand Down
60 changes: 51 additions & 9 deletions test/services/notification.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,33 @@ describe('getNotifications function', () => {
const skip = 5;
const limit = 10;

it('should return a list of notifications associated with the user', async () => {
const mockNotifications = [
{ pi_uid, is_cleared: false, reason: 'TEST_REASON_A' },
{ pi_uid, is_cleared: true, reason: 'TEST_REASON_B' }
];

function mockNotificationQuery(mockResult: any) {
const sortMock = jest.fn().mockReturnThis();
const skipMock = jest.fn().mockReturnThis();
const limitMock = jest.fn().mockReturnThis();
const execMock = jest.fn().mockResolvedValue(mockNotifications);

const execMock = jest.fn().mockResolvedValue(mockResult);
(Notification.find as jest.Mock).mockReturnValue({
sort: sortMock,
skip: skipMock,
limit: limitMock,
exec: execMock,
});

return { sortMock, skipMock, limitMock, execMock };
}

const existingNotifications = await getNotifications(pi_uid, skip, limit);
it('should return a list of notifications associated with the user', async () => {
const mockNotifications = [
{ pi_uid, is_cleared: false, reason: 'TEST_REASON_A' },
{ pi_uid, is_cleared: true, reason: 'TEST_REASON_B' },
{ pi_uid, is_cleared: true, reason: 'TEST_REASON_C' },
{ pi_uid, is_cleared: false, reason: 'TEST_REASON_D' }
];

const { sortMock, skipMock, limitMock, execMock } = mockNotificationQuery(mockNotifications);

const existingNotifications = await getNotifications(pi_uid, skip, limit, undefined);

expect(Notification.find).toHaveBeenCalledWith({ pi_uid });
expect(sortMock).toHaveBeenCalledWith({ createdAt: -1 });
Expand All @@ -75,6 +83,40 @@ describe('getNotifications function', () => {
expect(existingNotifications).toEqual(mockNotifications);
});

it('should filter notifications for status cleared', async () => {
const mockClearedNotifications = [
{ pi_uid, is_cleared: true, reason: 'TEST_REASON_B' },
{ pi_uid, is_cleared: true, reason: 'TEST_REASON_C' }
];

const { sortMock, skipMock, limitMock, execMock } = mockNotificationQuery(mockClearedNotifications);

const result = await getNotifications(pi_uid, skip, limit, 'cleared');
expect(Notification.find).toHaveBeenCalledWith({ pi_uid, is_cleared: true });
expect(sortMock).toHaveBeenCalledWith({ createdAt: -1 });
expect(skipMock).toHaveBeenCalledWith(skip);
expect(limitMock).toHaveBeenCalledWith(limit);
expect(execMock).toHaveBeenCalled();
expect(result).toEqual(mockClearedNotifications);
});

it('should filter notifications for status uncleared', async () => {
const mockUnclearedNotifications = [
{ pi_uid, is_cleared: false, reason: 'TEST_REASON_A' },
{ pi_uid, is_cleared: false, reason: 'TEST_REASON_D' }
];

const { sortMock, skipMock, limitMock, execMock } = mockNotificationQuery(mockUnclearedNotifications);

const result = await getNotifications(pi_uid, skip, limit, 'uncleared');
expect(Notification.find).toHaveBeenCalledWith({ pi_uid, is_cleared: false });
expect(sortMock).toHaveBeenCalledWith({ createdAt: -1 });
expect(skipMock).toHaveBeenCalledWith(skip);
expect(limitMock).toHaveBeenCalledWith(limit);
expect(execMock).toHaveBeenCalled();
expect(result).toEqual(mockUnclearedNotifications);
});

it('should throw an error if getting notifications fail', async () => {
const mockError = new Error('Mock database error');

Expand Down