Add controller, db, and validation for notifications

This commit is contained in:
Alex Holliday committed 2025-06-09 10:30:21 +08:00
1 parent 0f10a39f91
commit 5a50e9b301
3 files changed
+128 -1

No files matched your search

@@ -1,4 +1,5 @@
import Notification from "../../models/Notification.js";
import Monitor from "../../models/Monitor.js";
const SERVICE_NAME = "notificationModule";
/**
* Creates a new notification.
@@ -21,6 +22,28 @@ const createNotification = async (notificationData) => {
}
};
const getNotificationsByTeamId = async (teamId) => {
try {
const notifications = await Notification.find({ teamId });
return notifications;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getNotificationsByTeamId";
throw error;
}
};
const getNotificationsByIds = async (notificationIds) => {
try {
const notifications = await Notification.find({ _id: { $in: notificationIds } });
return notifications;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getNotificationsByIds";
throw error;
}
};
/**
* Retrieves notifications by monitor ID.
* @param {mongoose.Types.ObjectId} monitorId - The ID of the monitor.
@@ -49,8 +72,23 @@ const deleteNotificationsByMonitorId = async (monitorId) => {
}
};
const deleteNotificationById = async (id) => {
try {
const result = await Notification.findByIdAndDelete(id);
await Monitor.updateMany({ notifications: id }, { $pull: { notifications: id } });
return result;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "deleteNotificationById";
throw error;
}
};
export {
createNotification,
getNotificationsByTeamId,
getNotificationsByIds,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
deleteNotificationById,
};