|
3 | 3 | * |
4 | 4 | * Handles CRUD operations for in-app notifications: |
5 | 5 | * - List org notifications with pagination |
| 6 | + * - List user-specific notifications with pagination |
6 | 7 | * - Mark individual notification as read |
7 | 8 | * - Mark all notifications as read (bulk) |
8 | 9 | * - Get unread count for badge display |
| 10 | + * - Delete a notification |
9 | 11 | */ |
10 | 12 |
|
11 | 13 | import Notification from '../models/Notification.model.js'; |
@@ -95,3 +97,56 @@ export async function markAllAsRead(req, res) { |
95 | 97 |
|
96 | 98 | sendSuccess(res, { message: 'All notifications marked as read.', data: { modifiedCount: result.modifiedCount } }); |
97 | 99 | } |
| 100 | + |
| 101 | +/** |
| 102 | + * GET /notifications/user |
| 103 | + * List notifications for the authenticated user (user-scoped, not org-scoped). |
| 104 | + */ |
| 105 | +export async function getUserNotifications(req, res) { |
| 106 | + const { userId } = req.user; |
| 107 | + const { page = 1, limit = 20, read, type } = req.query; |
| 108 | + |
| 109 | + const filter = { userId }; |
| 110 | + |
| 111 | + if (read !== undefined) { |
| 112 | + filter.read = read === 'true'; |
| 113 | + } |
| 114 | + if (type) { |
| 115 | + filter.type = type; |
| 116 | + } |
| 117 | + |
| 118 | + const skip = (page - 1) * limit; |
| 119 | + |
| 120 | + const [notifications, total] = await Promise.all([ |
| 121 | + Notification.find(filter) |
| 122 | + .sort({ createdAt: -1 }) |
| 123 | + .skip(skip) |
| 124 | + .limit(parseInt(limit)) |
| 125 | + .lean(), |
| 126 | + Notification.countDocuments(filter), |
| 127 | + ]); |
| 128 | + |
| 129 | + const meta = buildPaginationMeta(total, parseInt(page), parseInt(limit)); |
| 130 | + |
| 131 | + sendSuccess(res, { data: { notifications, meta } }); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * DELETE /notifications/:id |
| 136 | + * Delete a notification. |
| 137 | + */ |
| 138 | +export async function deleteNotification(req, res) { |
| 139 | + const { userId } = req.user; |
| 140 | + const { id } = req.params; |
| 141 | + |
| 142 | + const notification = await Notification.findOneAndDelete({ _id: id, userId }); |
| 143 | + |
| 144 | + if (!notification) { |
| 145 | + return res.status(HTTP.NOT_FOUND).json({ |
| 146 | + success: false, |
| 147 | + error: { code: 'NOT_FOUND', message: 'Notification not found.' }, |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + sendSuccess(res, { message: 'Notification deleted.' }); |
| 152 | +} |
0 commit comments