api/resources/Notification/notificationController.js
2024-09-17 17:29:41 +05:30

57 lines
1.5 KiB
JavaScript

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 });
}
};