Merge pull request #492 from bluewave-labs/feat/db/notifications

Add DB operations for notifications
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-07-30 18:00:48 -07:00
commit 04102990eb
3 files changed
+60 -4

No files matched your search

+1
View File
@@ -158,6 +158,7 @@ const deleteMonitor = async (req, res, next) => {
await req.jobQueue.deleteJob(monitor);
await req.db.deleteChecks(monitor._id);
await req.db.deleteAlertByMonitorId(monitor._id);
await req.db.deleteNotificationByMonitorId(monitor._id);
/**
* TODO
* We should remove all checks and alerts associated with this monitor
+9 -4
View File
@@ -65,8 +65,6 @@ const {
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
getMonitorByIdForIncidents,
getMonitorsByUserIdForIncidents,
createMonitor,
deleteMonitor,
deleteAllMonitors,
@@ -120,6 +118,12 @@ const {
deleteMaintenanceWindowByUserId,
} = require("./modules/maintenaceWindowModule");
const {
createNotification,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
} = require("./modules/notificationModule");
module.exports = {
connect,
insertUser,
@@ -136,8 +140,6 @@ module.exports = {
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
getMonitorByIdForIncidents,
getMonitorsByUserIdForIncidents,
createMonitor,
deleteMonitor,
deleteAllMonitors,
@@ -162,4 +164,7 @@ module.exports = {
deleteMaintenaceWindowById,
deleteMaintenanceWindowByMonitorId,
deleteMaintenanceWindowByUserId,
createNotification,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
};
@@ -0,0 +1,50 @@
const Notification = require("../../../models/Notification");
/**
* Creates a new notification.
* @param {Object} notificationData - The data for the new notification.
* @param {mongoose.Types.ObjectId} notificationData.monitorId - The ID of the monitor.
* @param {string} notificationData.type - The type of the notification (e.g., "email", "sms").
* @param {string} [notificationData.address] - The address for the notification (if applicable).
* @param {string} [notificationData.phone] - The phone number for the notification (if applicable).
* @returns {Promise<Object>} The created notification.
* @throws Will throw an error if the notification cannot be created.
*/
const createNotification = async (notificationData) => {
try {
const notification = await new Notification({ ...notificationData }).save();
return notification;
} catch (error) {
throw error;
}
};
/**
* Retrieves notifications by monitor ID.
* @param {mongoose.Types.ObjectId} monitorId - The ID of the monitor.
* @returns {Promise<Array<Object>>} An array of notifications.
* @throws Will throw an error if the notifications cannot be retrieved.
*/
const getNotificationsByMonitorId = async (monitorId) => {
try {
const notifications = await Notification.find({ monitorId });
return notifications;
} catch (error) {
throw error;
}
};
const deleteNotificationsByMonitorId = async (monitorId) => {
try {
const result = await Notification.deleteMany({ monitorId });
return result.deletedCount;
} catch (error) {
throw error;
}
};
module.exports = {
createNotification,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
};