import { getStartAndEndOfDay } from "../Task/TaskController.js"; import { Notification } from "./notificationModal.js"; export const getNotification = async (req, res) => { try { // Ensure req.user._id is defined and valid console.log("req came here "); if (!req.user || !req.user._id) { return res.status(400).json({ return_message: "Invalid user ID" }); } // Fetch notifications for the user const notifications = await Notification.find({ added_for: req.user._id, }).sort({ createdAt: -1 }); if (notifications && notifications.length > 0) { return res .status(200) .json({ return_message: "Fetched notifications", notifications }); } return res.status(402).json({ return_message: "No notifications found" }); } catch (error) { return res .status(500) .json({ return_message: "Something went wrong", error }); } }; export const getSingleNotification = async (req, res) => { try { const notificationId = req.params.id; // Validate notificationId if (!notificationId) { return res .status(400) .json({ return_message: "Notification ID is required" }); } // Fetch the notification by ID const notification = await Notification.findById(notificationId); if (notification) { return res .status(200) .json({ return_message: "Fetched notification", notification }); } return res.status(404).json({ return_message: "Notification not found" }); } catch (error) { return res .status(500) .json({ return_message: "Something went wrong", error }); } }; export const getNotificationByDates = async (req, res) => { try { const filter = { added_for: req.user._id }; // Default filter by user ID // Get the date from the query and parse it const { Date: queryDate } = req.query; let notificationDate = queryDate ? new Date(queryDate.split("/").reverse().join("-")) // Convert DD/MM/YYYY to YYYY-MM-DD : new Date(); // Default to today's date // Get the start and end of the day const { startOfDay, endOfDay } = getStartAndEndOfDay(notificationDate); // Find notifications filtered by user and date range const notifications = await Notification.find({ ...filter, createdAt: { $gte: startOfDay, $lte: endOfDay }, }).sort({ createdAt: -1 }); if (notifications.length > 0) { return res.status(200).json({ success: true, notifications, }); } return res .status(404) .json({ success: false, message: "No notifications found" }); } catch (error) { res.status(500).json({ success: false, message: error.message, }); } };