Skip to content

Commit

Permalink
#2998 merged send notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Nov 19, 2024
2 parents 03124d3 + e3b3bb7 commit d56ddd9
Show file tree
Hide file tree
Showing 4 changed files with 65 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 @@ -203,4 +203,16 @@ export default class UsersController {
return next(error);
}
}

static async sendNotitifcation(req: Request, res: Response, next: NextFunction) {
try {
const { userId } = req.params;
const { text, iconName } = req.body;

const updatedUser = await UsersService.sendNotification(userId, text, iconName);
return res.status(200).json(updatedUser);
} catch (error: unknown) {
return next(error);
}
}
}
6 changes: 6 additions & 0 deletions src/backend/src/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ userRouter.post(
UsersController.getManyUserTasks
);
userRouter.get('/:userId/notifications', UsersController.getUserUnreadNotifications);
userRouter.post(
`/:userId/notifications/send`,
nonEmptyString(body('text')),
nonEmptyString(body('iconName')),
UsersController.sendNotitifcation
);

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

return requestedUser.unreadNotifications.map(notificationTransformer);
}

static async sendNotification(userId: string, text: string, iconName: string) {
const requestedUser = await prisma.user.findUnique({
where: { userId }
});

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

const createdNotification = await prisma.notification.create({
data: {
text,
iconName
}
});

const udaptedUser = await prisma.user.update({
where: { userId: requestedUser.userId },
data: { unreadNotifications: { connect: createdNotification } },
include: { unreadNotifications: true }
});

return udaptedUser;
}
}
24 changes: 24 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 prisma from '../../src/prisma/prisma';

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

describe('Send Notification', () => {
it('fails on invalid user id', async () => {
await expect(async () => await UsersService.sendNotification('1', 'test', 'test')).rejects.toThrow(
new NotFoundException('User', '1')
);
});

it('Succeeds and sends notification to user', async () => {
const testBatman = await createTestUser(batmanAppAdmin, orgId);
await UsersService.sendNotification(testBatman.userId, 'test1', 'test1');
await UsersService.sendNotification(testBatman.userId, 'test2', 'test2');

const batmanWithNotifications = await prisma.user.findUnique({
where: { userId: testBatman.userId },
include: { unreadNotifications: true }
});

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

0 comments on commit d56ddd9

Please sign in to comment.