|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import Notification from "../models/notification.model"; |
| 3 | +import mongoose from "mongoose"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Get all notifications for the authenticated user |
| 7 | + */ |
| 8 | +export const getNotifications = async (req: Request, res: Response): Promise<void> => { |
| 9 | + try { |
| 10 | + const userId = req.user?.id; |
| 11 | + if (!userId) { |
| 12 | + res.status(401).json({ message: "Unauthorized" }); |
| 13 | + return; |
| 14 | + } |
| 15 | + |
| 16 | + const { read, limit = 50, page = 1 } = req.query; |
| 17 | + const skip = (Number(page) - 1) * Number(limit); |
| 18 | + |
| 19 | + const query: any = { userId: new mongoose.Types.ObjectId(userId) }; |
| 20 | + if (read !== undefined) { |
| 21 | + query.read = read === "true"; |
| 22 | + } |
| 23 | + |
| 24 | + const notifications = await Notification.find(query) |
| 25 | + .populate("auctionId", "itemName") |
| 26 | + .sort({ createdAt: -1 }) |
| 27 | + .limit(Number(limit)) |
| 28 | + .skip(skip); |
| 29 | + |
| 30 | + const total = await Notification.countDocuments(query); |
| 31 | + const unreadCount = await Notification.countDocuments({ |
| 32 | + userId: new mongoose.Types.ObjectId(userId), |
| 33 | + read: false, |
| 34 | + }); |
| 35 | + |
| 36 | + res.status(200).json({ |
| 37 | + notifications, |
| 38 | + pagination: { |
| 39 | + page: Number(page), |
| 40 | + limit: Number(limit), |
| 41 | + total, |
| 42 | + pages: Math.ceil(total / Number(limit)), |
| 43 | + }, |
| 44 | + unreadCount, |
| 45 | + }); |
| 46 | + } catch (error: any) { |
| 47 | + console.error("Get notifications error:", error); |
| 48 | + res.status(500).json({ message: error.message || "Server error" }); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +/** |
| 53 | + * Mark notification as read |
| 54 | + */ |
| 55 | +export const markAsRead = async (req: Request, res: Response): Promise<void> => { |
| 56 | + try { |
| 57 | + const userId = req.user?.id; |
| 58 | + if (!userId) { |
| 59 | + res.status(401).json({ message: "Unauthorized" }); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const { id } = req.params; |
| 64 | + const notification = await Notification.findOne({ |
| 65 | + _id: id, |
| 66 | + userId: new mongoose.Types.ObjectId(userId), |
| 67 | + }); |
| 68 | + |
| 69 | + if (!notification) { |
| 70 | + res.status(404).json({ message: "Notification not found" }); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + notification.read = true; |
| 75 | + await notification.save(); |
| 76 | + |
| 77 | + res.status(200).json({ message: "Notification marked as read", notification }); |
| 78 | + } catch (error: any) { |
| 79 | + console.error("Mark as read error:", error); |
| 80 | + res.status(500).json({ message: error.message || "Server error" }); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Mark all notifications as read |
| 86 | + */ |
| 87 | +export const markAllAsRead = async (req: Request, res: Response): Promise<void> => { |
| 88 | + try { |
| 89 | + const userId = req.user?.id; |
| 90 | + if (!userId) { |
| 91 | + res.status(401).json({ message: "Unauthorized" }); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const result = await Notification.updateMany( |
| 96 | + { |
| 97 | + userId: new mongoose.Types.ObjectId(userId), |
| 98 | + read: false, |
| 99 | + }, |
| 100 | + { read: true } |
| 101 | + ); |
| 102 | + |
| 103 | + res.status(200).json({ |
| 104 | + message: "All notifications marked as read", |
| 105 | + updatedCount: result.modifiedCount, |
| 106 | + }); |
| 107 | + } catch (error: any) { |
| 108 | + console.error("Mark all as read error:", error); |
| 109 | + res.status(500).json({ message: error.message || "Server error" }); |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * Delete a notification |
| 115 | + */ |
| 116 | +export const deleteNotification = async (req: Request, res: Response): Promise<void> => { |
| 117 | + try { |
| 118 | + const userId = req.user?.id; |
| 119 | + if (!userId) { |
| 120 | + res.status(401).json({ message: "Unauthorized" }); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + const { id } = req.params; |
| 125 | + const notification = await Notification.findOneAndDelete({ |
| 126 | + _id: id, |
| 127 | + userId: new mongoose.Types.ObjectId(userId), |
| 128 | + }); |
| 129 | + |
| 130 | + if (!notification) { |
| 131 | + res.status(404).json({ message: "Notification not found" }); |
| 132 | + return; |
| 133 | + } |
| 134 | + |
| 135 | + res.status(200).json({ message: "Notification deleted successfully" }); |
| 136 | + } catch (error: any) { |
| 137 | + console.error("Delete notification error:", error); |
| 138 | + res.status(500).json({ message: error.message || "Server error" }); |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +/** |
| 143 | + * Get unread notification count |
| 144 | + */ |
| 145 | +export const getUnreadCount = async (req: Request, res: Response): Promise<void> => { |
| 146 | + try { |
| 147 | + const userId = req.user?.id; |
| 148 | + if (!userId) { |
| 149 | + res.status(401).json({ message: "Unauthorized" }); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + const count = await Notification.countDocuments({ |
| 154 | + userId: new mongoose.Types.ObjectId(userId), |
| 155 | + read: false, |
| 156 | + }); |
| 157 | + |
| 158 | + res.status(200).json({ unreadCount: count }); |
| 159 | + } catch (error: any) { |
| 160 | + console.error("Get unread count error:", error); |
| 161 | + res.status(500).json({ message: error.message || "Server error" }); |
| 162 | + } |
| 163 | +}; |
| 164 | + |
0 commit comments