From a7c93aee3935e7971bd6f54d52fdffbd65b37312 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 30 Jul 2024 13:19:37 -0700 Subject: [PATCH] Add DB operations for notifications --- Server/db/mongo/modules/notificationModule.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Server/db/mongo/modules/notificationModule.js diff --git a/Server/db/mongo/modules/notificationModule.js b/Server/db/mongo/modules/notificationModule.js new file mode 100644 index 000000000..7ed2d2064 --- /dev/null +++ b/Server/db/mongo/modules/notificationModule.js @@ -0,0 +1,41 @@ +const Notification = require("../../../models/Notification"); +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} 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>} 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; + } +}; + +module.exports = { + createNotification, + getNotificationsByMonitorId, +};