Skip to content

Commit

Permalink
feat: mark a notification or all notifications as read
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseAlbDR committed Feb 26, 2024
1 parent 3d72c8e commit e937da5
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 6 deletions.
9 changes: 9 additions & 0 deletions prisma/migrations/20240226165313_fix_is_read_at/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- You are about to drop the column `isReaddAt` on the `Notification` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "Notification" DROP COLUMN "isReaddAt",
ADD COLUMN "isReadAt" TIMESTAMP(3);
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ model Notification {
message String
isRead Boolean @default(false)
createdAt DateTime @default(now())
isReaddAt DateTime?
isReadAt DateTime?
queue String
User User @relation(fields: [userId], references: [id], onDelete: Cascade)
}
Expand Down
9 changes: 9 additions & 0 deletions src/presentation/users/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,13 @@ export class UserController {

res.status(HttpCodes.OK).json(notifications);
};

readNotification = async (req: Request, res: Response) => {
const { id } = req.params;
const user = req.user;

await this.userService.readNotification(user, id);

res.status(HttpCodes.OK).json({ message: 'Notification marked as read' });
};
}
19 changes: 14 additions & 5 deletions src/presentation/users/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,28 @@ export class UserRoutes {

router.use(authMiddleware.authenticateUser);

router.get('/me', userController.getCurrentUser);

// Favorites
router.get(
'/me/favorites',
// authMiddleware.authorizePermissions('adopter'),
authMiddleware.authorizePermissions('adopter'),
userController.getUserFavorites
);

// Notifications
router.get('/me/notifications', userController.getUserNotifications);

router.put('/me/notifications/read/:id', userController.readNotification);

// Animals
router.get(
'/me/animals/',
// authMiddleware.authorizePermissions('shelter'),
authMiddleware.authorizePermissions('shelter'),
userController.getUserAnimals
);

// Current User CRUD
router.get('/me', userController.getCurrentUser);

router.put(
'/me',
ValidationMiddleware.validate(UpdateUserDto),
Expand All @@ -78,13 +84,16 @@ export class UserRoutes {
router.delete('/me', userController.deleteUser);

router.get('/:id', userController.getSingleUser);
// End Current User CRUD

// All users
router.get(
'/',
// authMiddleware.authorizePermissions('admin'),
authMiddleware.authorizePermissions('admin'),
userController.getAllUsers
);

// Images
router.post(
'/upload-images',
[
Expand Down
42 changes: 42 additions & 0 deletions src/presentation/users/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,46 @@ export class UserService {
notifications,
};
}

/**
* Marks a notification as read for a user.
* @param user - PayloadUser object representing the user.
* @param id - ID of the notification to mark as read or 'all' to mark all notifications as read
*/
public async readNotification(user: PayloadUser, id: string) {
if (id === 'all')
await prisma.notification.updateMany({
where: {
userId: user.id,
},
data: {
isRead: true,
isReadAt: new Date(),
},
});

const notification = await prisma.notification.findUnique({
where: {
id,
},
});

if (!notification) throw new NotFoundError('Notification not found');
if (notification.isRead)
throw new BadRequestError('Notification is already read');

CheckPermissions.check(user, notification.userId);

await prisma.notification.update({
where: {
id: id,
},
data: {
isRead: true,
isReadAt: new Date(),
},
});

return true;
}
}

0 comments on commit e937da5

Please sign in to comment.