module -> class
This commit is contained in:
1 parent
ae40cb8a54
commit
7bf96f6194
6 files changed
+110
-129
No files matched your search
@@ -52,6 +52,7 @@ import Team from "../db/models/Team.js";
|
|||||||
import MaintenanceWindow from "../db/models/MaintenanceWindow.js";
|
import MaintenanceWindow from "../db/models/MaintenanceWindow.js";
|
||||||
import MonitorStats from "../db/models/MonitorStats.js";
|
import MonitorStats from "../db/models/MonitorStats.js";
|
||||||
import NetworkCheck from "../db/models/NetworkCheck.js";
|
import NetworkCheck from "../db/models/NetworkCheck.js";
|
||||||
|
import Notification from "../db/models/Notification.js";
|
||||||
|
|
||||||
import InviteModule from "../db/mongo/modules/inviteModule.js";
|
import InviteModule from "../db/mongo/modules/inviteModule.js";
|
||||||
import CheckModule from "../db/mongo/modules/checkModule.js";
|
import CheckModule from "../db/mongo/modules/checkModule.js";
|
||||||
@@ -61,6 +62,7 @@ import HardwareCheckModule from "../db/mongo/modules/hardwareCheckModule.js";
|
|||||||
import MaintenanceWindowModule from "../db/mongo/modules/maintenanceWindowModule.js";
|
import MaintenanceWindowModule from "../db/mongo/modules/maintenanceWindowModule.js";
|
||||||
import MonitorModule from "../db/mongo/modules/monitorModule.js";
|
import MonitorModule from "../db/mongo/modules/monitorModule.js";
|
||||||
import NetworkCheckModule from "../db/mongo/modules/networkCheckModule.js";
|
import NetworkCheckModule from "../db/mongo/modules/networkCheckModule.js";
|
||||||
|
import NotificationModule from "../db/mongo/modules/notificationModule.js";
|
||||||
|
|
||||||
export const initializeServices = async ({ logger, envSettings, settingsService }) => {
|
export const initializeServices = async ({ logger, envSettings, settingsService }) => {
|
||||||
const serviceRegistry = new ServiceRegistry({ logger });
|
const serviceRegistry = new ServiceRegistry({ logger });
|
||||||
@@ -93,6 +95,8 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
|||||||
NormalizeDataUptimeDetails,
|
NormalizeDataUptimeDetails,
|
||||||
});
|
});
|
||||||
const networkCheckModule = new NetworkCheckModule({ NetworkCheck });
|
const networkCheckModule = new NetworkCheckModule({ NetworkCheck });
|
||||||
|
const notificationModule = new NotificationModule({ Notification, Monitor });
|
||||||
|
|
||||||
const db = new MongoDB({
|
const db = new MongoDB({
|
||||||
logger,
|
logger,
|
||||||
envSettings,
|
envSettings,
|
||||||
@@ -104,6 +108,7 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
|||||||
maintenanceWindowModule,
|
maintenanceWindowModule,
|
||||||
monitorModule,
|
monitorModule,
|
||||||
networkCheckModule,
|
networkCheckModule,
|
||||||
|
notificationModule,
|
||||||
});
|
});
|
||||||
|
|
||||||
await db.connect();
|
await db.connect();
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ class NotificationController extends BaseController {
|
|||||||
body.userId = userId;
|
body.userId = userId;
|
||||||
body.teamId = teamId;
|
body.teamId = teamId;
|
||||||
|
|
||||||
const notification = await this.db.createNotification(body);
|
const notification = await this.db.notificationModule.createNotification(body);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: "Notification created successfully",
|
msg: "Notification created successfully",
|
||||||
data: notification,
|
data: notification,
|
||||||
@@ -70,7 +70,7 @@ class NotificationController extends BaseController {
|
|||||||
throw this.errorService.createBadRequestError("Team ID is required");
|
throw this.errorService.createBadRequestError("Team ID is required");
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifications = await this.db.getNotificationsByTeamId(teamId);
|
const notifications = await this.db.notificationModule.getNotificationsByTeamId(teamId);
|
||||||
|
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: "Notifications fetched successfully",
|
msg: "Notifications fetched successfully",
|
||||||
@@ -88,12 +88,12 @@ class NotificationController extends BaseController {
|
|||||||
throw this.errorService.createBadRequestError("Team ID is required");
|
throw this.errorService.createBadRequestError("Team ID is required");
|
||||||
}
|
}
|
||||||
|
|
||||||
const notification = await this.db.getNotificationById(req.params.id);
|
const notification = await this.db.notificationModule.getNotificationById(req.params.id);
|
||||||
if (!notification.teamId.equals(teamId)) {
|
if (!notification.teamId.equals(teamId)) {
|
||||||
throw this.errorService.createAuthorizationError();
|
throw this.errorService.createAuthorizationError();
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.db.deleteNotificationById(req.params.id);
|
await this.db.notificationModule.deleteNotificationById(req.params.id);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: "Notification deleted successfully",
|
msg: "Notification deleted successfully",
|
||||||
});
|
});
|
||||||
@@ -104,7 +104,7 @@ class NotificationController extends BaseController {
|
|||||||
|
|
||||||
getNotificationById = this.asyncHandler(
|
getNotificationById = this.asyncHandler(
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
const notification = await this.db.getNotificationById(req.params.id);
|
const notification = await this.db.notificationModule.getNotificationById(req.params.id);
|
||||||
|
|
||||||
const teamId = req?.user?.teamId;
|
const teamId = req?.user?.teamId;
|
||||||
if (!teamId) {
|
if (!teamId) {
|
||||||
@@ -134,13 +134,13 @@ class NotificationController extends BaseController {
|
|||||||
throw this.errorService.createBadRequestError("Team ID is required");
|
throw this.errorService.createBadRequestError("Team ID is required");
|
||||||
}
|
}
|
||||||
|
|
||||||
const notification = await this.db.getNotificationById(req.params.id);
|
const notification = await this.db.notificationModule.getNotificationById(req.params.id);
|
||||||
|
|
||||||
if (!notification.teamId.equals(teamId)) {
|
if (!notification.teamId.equals(teamId)) {
|
||||||
throw this.errorService.createAuthorizationError();
|
throw this.errorService.createAuthorizationError();
|
||||||
}
|
}
|
||||||
|
|
||||||
const editedNotification = await this.db.editNotification(req.params.id, req.body);
|
const editedNotification = await this.db.notificationModule.editNotification(req.params.id, req.body);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: "Notification updated successfully",
|
msg: "Notification updated successfully",
|
||||||
data: editedNotification,
|
data: editedNotification,
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ class MongoDB {
|
|||||||
maintenanceWindowModule,
|
maintenanceWindowModule,
|
||||||
monitorModule,
|
monitorModule,
|
||||||
networkCheckModule,
|
networkCheckModule,
|
||||||
|
notificationModule,
|
||||||
}) {
|
}) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.envSettings = envSettings;
|
this.envSettings = envSettings;
|
||||||
@@ -53,7 +54,7 @@ class MongoDB {
|
|||||||
this.checkModule = checkModule;
|
this.checkModule = checkModule;
|
||||||
this.maintenanceWindowModule = maintenanceWindowModule;
|
this.maintenanceWindowModule = maintenanceWindowModule;
|
||||||
this.monitorModule = monitorModule;
|
this.monitorModule = monitorModule;
|
||||||
Object.assign(this, notificationModule);
|
this.notificationModule = notificationModule;
|
||||||
Object.assign(this, settingsModule);
|
Object.assign(this, settingsModule);
|
||||||
this.statusPageModule = statusPageModule;
|
this.statusPageModule = statusPageModule;
|
||||||
Object.assign(this, diagnosticModule);
|
Object.assign(this, diagnosticModule);
|
||||||
|
|||||||
@@ -1,125 +1,100 @@
|
|||||||
import Notification from "../../models/Notification.js";
|
// import Notification from "../../models/Notification.js";
|
||||||
import Monitor from "../../models/Monitor.js";
|
// import Monitor from "../../models/Monitor.js";
|
||||||
const SERVICE_NAME = "notificationModule";
|
const SERVICE_NAME = "notificationModule";
|
||||||
/**
|
|
||||||
* 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) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "createNotification";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getNotificationsByTeamId = async (teamId) => {
|
class NotificationModule {
|
||||||
try {
|
constructor({ Notification, Monitor }) {
|
||||||
const notifications = await Notification.find({ teamId });
|
this.Notification = Notification;
|
||||||
return notifications;
|
this.Monitor = Monitor;
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "getNotificationsByTeamId";
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
};
|
createNotification = async (notificationData) => {
|
||||||
|
try {
|
||||||
const getNotificationsByIds = async (notificationIds) => {
|
const notification = await new this.Notification({ ...notificationData }).save();
|
||||||
try {
|
return notification;
|
||||||
const notifications = await Notification.find({ _id: { $in: notificationIds } });
|
} catch (error) {
|
||||||
return notifications;
|
error.service = SERVICE_NAME;
|
||||||
} catch (error) {
|
error.method = "createNotification";
|
||||||
error.service = SERVICE_NAME;
|
throw error;
|
||||||
error.method = "getNotificationsByIds";
|
|
||||||
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) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "getNotificationsByMonitorId";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteNotificationsByMonitorId = async (monitorId) => {
|
|
||||||
try {
|
|
||||||
const result = await Notification.deleteMany({ monitorId });
|
|
||||||
return result.deletedCount;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "deleteNotificationsByMonitorId";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteNotificationById = async (id) => {
|
|
||||||
try {
|
|
||||||
const notification = await Notification.findById(id);
|
|
||||||
if (!notification) {
|
|
||||||
throw new Error("Notification not found");
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
getNotificationsByTeamId = async (teamId) => {
|
||||||
|
try {
|
||||||
|
const notifications = await this.Notification.find({ teamId });
|
||||||
|
return notifications;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getNotificationsByTeamId";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
getNotificationsByIds = async (notificationIds) => {
|
||||||
|
try {
|
||||||
|
const notifications = await this.Notification.find({ _id: { $in: notificationIds } });
|
||||||
|
return notifications;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getNotificationsByIds";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
getNotificationsByMonitorId = async (monitorId) => {
|
||||||
|
try {
|
||||||
|
const notifications = await this.Notification.find({ monitorId });
|
||||||
|
return notifications;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getNotificationsByMonitorId";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
deleteNotificationsByMonitorId = async (monitorId) => {
|
||||||
|
try {
|
||||||
|
const result = await this.Notification.deleteMany({ monitorId });
|
||||||
|
return result.deletedCount;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "deleteNotificationsByMonitorId";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
deleteNotificationById = async (id) => {
|
||||||
|
try {
|
||||||
|
const notification = await this.Notification.findById(id);
|
||||||
|
if (!notification) {
|
||||||
|
throw new Error("Notification not found");
|
||||||
|
}
|
||||||
|
|
||||||
const result = await Notification.findByIdAndDelete(id);
|
const result = await this.Notification.findByIdAndDelete(id);
|
||||||
await Monitor.updateMany({ notifications: id }, { $pull: { notifications: id } });
|
await this.Monitor.updateMany({ notifications: id }, { $pull: { notifications: id } });
|
||||||
return result;
|
return result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
error.service = SERVICE_NAME;
|
error.service = SERVICE_NAME;
|
||||||
error.method = "deleteNotificationById";
|
error.method = "deleteNotificationById";
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
getNotificationById = async (id) => {
|
||||||
|
try {
|
||||||
|
const notification = await this.Notification.findById(id);
|
||||||
|
return notification;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getNotificationById";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
editNotification = async (id, notificationData) => {
|
||||||
|
try {
|
||||||
|
const notification = await this.Notification.findByIdAndUpdate(id, notificationData, {
|
||||||
|
new: true,
|
||||||
|
});
|
||||||
|
return notification;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "editNotification";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const getNotificationById = async (id) => {
|
export default NotificationModule;
|
||||||
try {
|
|
||||||
const notification = await Notification.findById(id);
|
|
||||||
return notification;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "getNotificationById";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const editNotification = async (id, notificationData) => {
|
|
||||||
try {
|
|
||||||
const notification = await Notification.findByIdAndUpdate(id, notificationData, {
|
|
||||||
new: true,
|
|
||||||
});
|
|
||||||
return notification;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "editNotification";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export {
|
|
||||||
createNotification,
|
|
||||||
getNotificationsByTeamId,
|
|
||||||
getNotificationsByIds,
|
|
||||||
getNotificationsByMonitorId,
|
|
||||||
deleteNotificationsByMonitorId,
|
|
||||||
deleteNotificationById,
|
|
||||||
getNotificationById,
|
|
||||||
editNotification,
|
|
||||||
};
|
|
||||||
@@ -155,7 +155,7 @@ class MonitorService {
|
|||||||
await this.jobQueue.deleteJob(monitor);
|
await this.jobQueue.deleteJob(monitor);
|
||||||
await this.db.checkModule.deleteChecks(monitor._id);
|
await this.db.checkModule.deleteChecks(monitor._id);
|
||||||
await this.db.deletePageSpeedChecksByMonitorId(monitor._id);
|
await this.db.deletePageSpeedChecksByMonitorId(monitor._id);
|
||||||
await this.db.deleteNotificationsByMonitorId(monitor._id);
|
await this.db.notificationsModule.deleteNotificationsByMonitorId(monitor._id);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.warn({
|
this.logger.warn({
|
||||||
message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
|
message: `Error deleting associated records for monitor ${monitor._id} with name ${monitor.name}`,
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ class NotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async notifyAll({ notificationIDs, subject, html, content }) {
|
async notifyAll({ notificationIDs, subject, html, content }) {
|
||||||
const notifications = await this.db.getNotificationsByIds(notificationIDs);
|
const notifications = await this.db.notificationModule.getNotificationsByIds(notificationIDs);
|
||||||
|
|
||||||
// Map each notification to a test promise
|
// Map each notification to a test promise
|
||||||
const promises = notifications.map(async (notification) => {
|
const promises = notifications.map(async (notification) => {
|
||||||
|
|||||||
Reference in new issue
Block a user