module -> class
This commit is contained in:
1 parent
00f5eaf44d
commit
585fb4f700
5 files changed
+119
-192
No files matched your search
@@ -46,12 +46,15 @@ import User from "../db/models/User.js";
|
|||||||
import InviteToken from "../db/models/InviteToken.js";
|
import InviteToken from "../db/models/InviteToken.js";
|
||||||
import StatusPage from "../db/models/StatusPage.js";
|
import StatusPage from "../db/models/StatusPage.js";
|
||||||
import Team from "../db/models/Team.js";
|
import Team from "../db/models/Team.js";
|
||||||
|
import MaintenanceWindow from "../db/models/MaintenanceWindow.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";
|
||||||
import StatusPageModule from "../db/mongo/modules/statusPageModule.js";
|
import StatusPageModule from "../db/mongo/modules/statusPageModule.js";
|
||||||
import UserModule from "../db/mongo/modules/userModule.js";
|
import UserModule from "../db/mongo/modules/userModule.js";
|
||||||
import HardwareCheckModule from "../db/mongo/modules/hardwareCheckModule.js";
|
import HardwareCheckModule from "../db/mongo/modules/hardwareCheckModule.js";
|
||||||
|
import MaintenanceWindowModule from "../db/mongo/modules/maintenanceWindowModule.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 });
|
||||||
ServiceRegistry.instance = serviceRegistry;
|
ServiceRegistry.instance = serviceRegistry;
|
||||||
@@ -67,7 +70,17 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
|||||||
const statusPageModule = new StatusPageModule({ StatusPage, NormalizeData, stringService });
|
const statusPageModule = new StatusPageModule({ StatusPage, NormalizeData, stringService });
|
||||||
const userModule = new UserModule({ User, Team, GenerateAvatarImage, ParseBoolean, stringService });
|
const userModule = new UserModule({ User, Team, GenerateAvatarImage, ParseBoolean, stringService });
|
||||||
const hardwareCheckModule = new HardwareCheckModule({ HardwareCheck, Monitor, logger });
|
const hardwareCheckModule = new HardwareCheckModule({ HardwareCheck, Monitor, logger });
|
||||||
const db = new MongoDB({ logger, envSettings, checkModule, inviteModule, statusPageModule, userModule, hardwareCheckModule });
|
const maintenanceWindowModule = new MaintenanceWindowModule({ MaintenanceWindow });
|
||||||
|
const db = new MongoDB({
|
||||||
|
logger,
|
||||||
|
envSettings,
|
||||||
|
checkModule,
|
||||||
|
inviteModule,
|
||||||
|
statusPageModule,
|
||||||
|
userModule,
|
||||||
|
hardwareCheckModule,
|
||||||
|
maintenanceWindowModule,
|
||||||
|
});
|
||||||
|
|
||||||
await db.connect();
|
await db.connect();
|
||||||
|
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ import * as diagnosticModule from "./modules/diagnosticModule.js";
|
|||||||
class MongoDB {
|
class MongoDB {
|
||||||
static SERVICE_NAME = "MongoDB";
|
static SERVICE_NAME = "MongoDB";
|
||||||
|
|
||||||
constructor({ logger, envSettings, checkModule, inviteModule, statusPageModule, userModule, hardwareCheckModule }) {
|
constructor({ logger, envSettings, checkModule, inviteModule, statusPageModule, userModule, hardwareCheckModule, maintenanceWindowModule }) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.envSettings = envSettings;
|
this.envSettings = envSettings;
|
||||||
this.userModule = userModule;
|
this.userModule = userModule;
|
||||||
@@ -51,7 +51,7 @@ class MongoDB {
|
|||||||
Object.assign(this, pageSpeedCheckModule);
|
Object.assign(this, pageSpeedCheckModule);
|
||||||
this.hardwareCheckModule = hardwareCheckModule;
|
this.hardwareCheckModule = hardwareCheckModule;
|
||||||
this.checkModule = checkModule;
|
this.checkModule = checkModule;
|
||||||
Object.assign(this, maintenanceWindowModule);
|
this.maintenanceWindowModule = maintenanceWindowModule;
|
||||||
Object.assign(this, notificationModule);
|
Object.assign(this, notificationModule);
|
||||||
Object.assign(this, settingsModule);
|
Object.assign(this, settingsModule);
|
||||||
this.statusPageModule = statusPageModule;
|
this.statusPageModule = statusPageModule;
|
||||||
|
|||||||
@@ -1,192 +1,106 @@
|
|||||||
import MaintenanceWindow from "../../models/MaintenanceWindow.js";
|
|
||||||
const SERVICE_NAME = "maintenanceWindowModule";
|
const SERVICE_NAME = "maintenanceWindowModule";
|
||||||
|
|
||||||
/**
|
class MaintenanceWindowModule {
|
||||||
* Asynchronously creates a new MaintenanceWindow document and saves it to the database.
|
constructor({ MaintenanceWindow }) {
|
||||||
* If the maintenance window is a one-time event, the expiry field is set to the same value as the end field.
|
this.MaintenanceWindow = MaintenanceWindow;
|
||||||
* @async
|
}
|
||||||
* @function createMaintenanceWindow
|
createMaintenanceWindow = async (maintenanceWindowData) => {
|
||||||
* @param {Object} maintenanceWindowData - The data for the new MaintenanceWindow document.
|
try {
|
||||||
* @param {mongoose.Schema.Types.ObjectId} maintenanceWindowData.monitorId - The ID of the monitor.
|
const maintenanceWindow = new this.MaintenanceWindow({
|
||||||
* @param {Boolean} maintenanceWindowData.active - Indicates whether the maintenance window is active.
|
...maintenanceWindowData,
|
||||||
* @param {Boolean} maintenanceWindowData.oneTime - Indicates whether the maintenance window is a one-time event.
|
});
|
||||||
* @param {Date} maintenanceWindowData.start - The start date and time of the maintenance window.
|
|
||||||
* @param {Date} maintenanceWindowData.end - The end date and time of the maintenance window.
|
|
||||||
* @returns {Promise<MaintenanceWindow>} The saved MaintenanceWindow document.
|
|
||||||
* @throws {Error} If there is an error saving the document.
|
|
||||||
*/
|
|
||||||
const createMaintenanceWindow = async (maintenanceWindowData) => {
|
|
||||||
try {
|
|
||||||
const maintenanceWindow = new MaintenanceWindow({
|
|
||||||
...maintenanceWindowData,
|
|
||||||
});
|
|
||||||
|
|
||||||
// If the maintenance window is a one time window, set the expiry to the end date
|
// If the maintenance window is a one time window, set the expiry to the end date
|
||||||
if (maintenanceWindowData.oneTime) {
|
if (maintenanceWindowData.oneTime) {
|
||||||
maintenanceWindow.expiry = maintenanceWindowData.end;
|
maintenanceWindow.expiry = maintenanceWindowData.end;
|
||||||
|
}
|
||||||
|
const result = await maintenanceWindow.save();
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "createMaintenanceWindow";
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
const result = await maintenanceWindow.save();
|
};
|
||||||
return result;
|
getMaintenanceWindowById = async ({ id, teamId }) => {
|
||||||
} catch (error) {
|
try {
|
||||||
error.service = SERVICE_NAME;
|
const maintenanceWindow = await this.MaintenanceWindow.findOne({
|
||||||
error.method = "createMaintenanceWindow";
|
_id: id,
|
||||||
throw error;
|
teamId: teamId,
|
||||||
}
|
});
|
||||||
};
|
return maintenanceWindow;
|
||||||
|
} catch (error) {
|
||||||
const getMaintenanceWindowById = async ({ id, teamId }) => {
|
error.service = SERVICE_NAME;
|
||||||
try {
|
error.method = "getMaintenanceWindowById";
|
||||||
const maintenanceWindow = await MaintenanceWindow.findOne({
|
throw error;
|
||||||
_id: id,
|
|
||||||
teamId: teamId,
|
|
||||||
});
|
|
||||||
return maintenanceWindow;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "getMaintenanceWindowById";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Asynchronously retrieves all MaintenanceWindow documents associated with a specific team ID.
|
|
||||||
* @async
|
|
||||||
* @function getMaintenanceWindowByUserId
|
|
||||||
* @param {String} teamId - The ID of the team.
|
|
||||||
* @param {Object} query - The request body.
|
|
||||||
* @returns {Promise<Array<MaintenanceWindow>>} An array of MaintenanceWindow documents.
|
|
||||||
* @throws {Error} If there is an error retrieving the documents.
|
|
||||||
*/
|
|
||||||
const getMaintenanceWindowsByTeamId = async (teamId, query) => {
|
|
||||||
try {
|
|
||||||
let { active, page, rowsPerPage, field, order } = query || {};
|
|
||||||
const maintenanceQuery = { teamId };
|
|
||||||
|
|
||||||
if (active !== undefined) maintenanceQuery.active = active;
|
|
||||||
|
|
||||||
const maintenanceWindowCount = await MaintenanceWindow.countDocuments(maintenanceQuery);
|
|
||||||
|
|
||||||
// Pagination
|
|
||||||
let skip = 0;
|
|
||||||
if (page && rowsPerPage) {
|
|
||||||
skip = page * rowsPerPage;
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Sorting
|
getMaintenanceWindowsByTeamId = async (teamId, query) => {
|
||||||
let sort = {};
|
try {
|
||||||
if (field !== undefined && order !== undefined) {
|
let { active, page, rowsPerPage, field, order } = query || {};
|
||||||
sort[field] = order === "asc" ? 1 : -1;
|
const maintenanceQuery = { teamId };
|
||||||
|
|
||||||
|
if (active !== undefined) maintenanceQuery.active = active;
|
||||||
|
|
||||||
|
const maintenanceWindowCount = await this.MaintenanceWindow.countDocuments(maintenanceQuery);
|
||||||
|
|
||||||
|
// Pagination
|
||||||
|
let skip = 0;
|
||||||
|
if (page && rowsPerPage) {
|
||||||
|
skip = page * rowsPerPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sorting
|
||||||
|
let sort = {};
|
||||||
|
if (field !== undefined && order !== undefined) {
|
||||||
|
sort[field] = order === "asc" ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const maintenanceWindows = await this.MaintenanceWindow.find(maintenanceQuery).skip(skip).limit(rowsPerPage).sort(sort);
|
||||||
|
|
||||||
|
return { maintenanceWindows, maintenanceWindowCount };
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getMaintenanceWindowByUserId";
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
getMaintenanceWindowsByMonitorId = async ({ monitorId, teamId }) => {
|
||||||
|
try {
|
||||||
|
const maintenanceWindows = await this.MaintenanceWindow.find({
|
||||||
|
monitorId: monitorId,
|
||||||
|
teamId: teamId,
|
||||||
|
});
|
||||||
|
return maintenanceWindows;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "getMaintenanceWindowsByMonitorId";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const maintenanceWindows = await MaintenanceWindow.find(maintenanceQuery).skip(skip).limit(rowsPerPage).sort(sort);
|
deleteMaintenanceWindowById = async ({ id, teamId }) => {
|
||||||
|
try {
|
||||||
|
const maintenanceWindow = await this.MaintenanceWindow.findOneAndDelete({ _id: id, teamId });
|
||||||
|
return maintenanceWindow;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "deleteMaintenanceWindowById";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return { maintenanceWindows, maintenanceWindowCount };
|
editMaintenanceWindowById = async ({ id, body }) => {
|
||||||
} catch (error) {
|
try {
|
||||||
error.service = SERVICE_NAME;
|
const editedMaintenanceWindow = await this.MaintenanceWindow.findByIdAndUpdate(id, body, { new: true });
|
||||||
error.method = "getMaintenanceWindowByUserId";
|
return editedMaintenanceWindow;
|
||||||
throw error;
|
} catch (error) {
|
||||||
}
|
error.service = SERVICE_NAME;
|
||||||
};
|
error.method = "editMaintenanceWindowById";
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
export default MaintenanceWindowModule;
|
||||||
* Asynchronously retrieves all MaintenanceWindow documents associated with a specific monitor ID.
|
|
||||||
* @async
|
|
||||||
* @function getMaintenanceWindowsByMonitorId
|
|
||||||
* @param {mongoose.Schema.Types.ObjectId} monitorId - The ID of the monitor.
|
|
||||||
* @returns {Promise<Array<MaintenanceWindow>>} An array of MaintenanceWindow documents.
|
|
||||||
* @throws {Error} If there is an error retrieving the documents.
|
|
||||||
*/
|
|
||||||
const getMaintenanceWindowsByMonitorId = async ({ monitorId, teamId }) => {
|
|
||||||
try {
|
|
||||||
const maintenanceWindows = await MaintenanceWindow.find({
|
|
||||||
monitorId: monitorId,
|
|
||||||
teamId: teamId,
|
|
||||||
});
|
|
||||||
return maintenanceWindows;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "getMaintenanceWindowsByMonitorId";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Asynchronously deletes a MaintenanceWindow document by its ID.
|
|
||||||
* @async
|
|
||||||
* @function deleteMaintenanceWindowById
|
|
||||||
* @param {mongoose.Schema.Types.ObjectId} maintenanceWindowId - The ID of the MaintenanceWindow document to delete.
|
|
||||||
* @returns {Promise<MaintenanceWindow>} The deleted MaintenanceWindow document.
|
|
||||||
* @throws {Error} If there is an error deleting the document.
|
|
||||||
*/
|
|
||||||
const deleteMaintenanceWindowById = async ({ id, teamId }) => {
|
|
||||||
try {
|
|
||||||
const maintenanceWindow = await MaintenanceWindow.findOneAndDelete({ _id: id, teamId });
|
|
||||||
return maintenanceWindow;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "deleteMaintenanceWindowById";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Asynchronously deletes all MaintenanceWindow documents associated with a specific monitor ID.
|
|
||||||
* @async
|
|
||||||
* @function deleteMaintenanceWindowByMonitorId
|
|
||||||
* @param {mongoose.Schema.Types.ObjectId} monitorId - The ID of the monitor.
|
|
||||||
* @returns {Promise<Object>} The result of the delete operation. This object contains information about the operation, such as the number of documents deleted.
|
|
||||||
* @throws {Error} If there is an error deleting the documents.
|
|
||||||
* @example
|
|
||||||
*/
|
|
||||||
const deleteMaintenanceWindowByMonitorId = async (monitorId) => {
|
|
||||||
try {
|
|
||||||
const result = await MaintenanceWindow.deleteMany({ monitorId: monitorId });
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "deleteMaintenanceWindowByMonitorId";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Asynchronously deletes all MaintenanceWindow documents associated with a specific user ID.
|
|
||||||
* @async
|
|
||||||
* @function deleteMaintenanceWindowByUserId
|
|
||||||
* @param {String} userId - The ID of the user.
|
|
||||||
* @returns {Promise<Object>} The result of the delete operation. This object contains information about the operation, such as the number of documents deleted.
|
|
||||||
* @throws {Error} If there is an error deleting the documents.
|
|
||||||
* @example
|
|
||||||
*/
|
|
||||||
const deleteMaintenanceWindowByUserId = async (userId) => {
|
|
||||||
try {
|
|
||||||
const result = await MaintenanceWindow.deleteMany({ userId: userId });
|
|
||||||
return result;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "deleteMaintenanceWindowByUserId";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const editMaintenanceWindowById = async ({ id, body, teamId }) => {
|
|
||||||
try {
|
|
||||||
const editedMaintenanceWindow = await MaintenanceWindow.findByIdAndUpdate(id, body, { new: true });
|
|
||||||
return editedMaintenanceWindow;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "editMaintenanceWindowById";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export {
|
|
||||||
createMaintenanceWindow,
|
|
||||||
getMaintenanceWindowById,
|
|
||||||
getMaintenanceWindowsByTeamId,
|
|
||||||
getMaintenanceWindowsByMonitorId,
|
|
||||||
deleteMaintenanceWindowById,
|
|
||||||
deleteMaintenanceWindowByMonitorId,
|
|
||||||
deleteMaintenanceWindowByUserId,
|
|
||||||
editMaintenanceWindowById,
|
|
||||||
};
|
|
||||||
@@ -25,7 +25,7 @@ class MaintenanceWindowService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const dbTransactions = monitorIds.map((monitorId) => {
|
const dbTransactions = monitorIds.map((monitorId) => {
|
||||||
return this.db.createMaintenanceWindow({
|
return this.db.maintenanceWindowModule.createMaintenanceWindow({
|
||||||
teamId,
|
teamId,
|
||||||
monitorId,
|
monitorId,
|
||||||
name: body.name,
|
name: body.name,
|
||||||
@@ -39,26 +39,26 @@ class MaintenanceWindowService {
|
|||||||
};
|
};
|
||||||
|
|
||||||
getMaintenanceWindowById = async ({ id, teamId }) => {
|
getMaintenanceWindowById = async ({ id, teamId }) => {
|
||||||
const maintenanceWindow = await this.db.getMaintenanceWindowById({ id, teamId });
|
const maintenanceWindow = await this.db.maintenanceWindowModule.getMaintenanceWindowById({ id, teamId });
|
||||||
return maintenanceWindow;
|
return maintenanceWindow;
|
||||||
};
|
};
|
||||||
|
|
||||||
getMaintenanceWindowsByTeamId = async ({ teamId, query }) => {
|
getMaintenanceWindowsByTeamId = async ({ teamId, query }) => {
|
||||||
const maintenanceWindows = await this.db.getMaintenanceWindowsByTeamId(teamId, query);
|
const maintenanceWindows = await this.db.maintenanceWindowModule.getMaintenanceWindowsByTeamId(teamId, query);
|
||||||
return maintenanceWindows;
|
return maintenanceWindows;
|
||||||
};
|
};
|
||||||
|
|
||||||
getMaintenanceWindowsByMonitorId = async ({ monitorId, teamId }) => {
|
getMaintenanceWindowsByMonitorId = async ({ monitorId, teamId }) => {
|
||||||
const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId({ monitorId, teamId });
|
const maintenanceWindows = await this.db.maintenanceWindowModule.getMaintenanceWindowsByMonitorId({ monitorId, teamId });
|
||||||
return maintenanceWindows;
|
return maintenanceWindows;
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteMaintenanceWindow = async ({ id, teamId }) => {
|
deleteMaintenanceWindow = async ({ id, teamId }) => {
|
||||||
await this.db.deleteMaintenanceWindowById({ id, teamId });
|
await this.db.maintenanceWindowModule.deleteMaintenanceWindowById({ id, teamId });
|
||||||
};
|
};
|
||||||
|
|
||||||
editMaintenanceWindow = async ({ id, teamId, body }) => {
|
editMaintenanceWindow = async ({ id, teamId, body }) => {
|
||||||
const editedMaintenanceWindow = await this.db.editMaintenanceWindowById({ id, body, teamId });
|
const editedMaintenanceWindow = await this.db.maintenanceWindowModule.editMaintenanceWindowById({ id, body, teamId });
|
||||||
return editedMaintenanceWindow;
|
return editedMaintenanceWindow;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class SuperSimpleQueueHelper {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async isInMaintenanceWindow(monitorId) {
|
async isInMaintenanceWindow(monitorId) {
|
||||||
const maintenanceWindows = await this.db.getMaintenanceWindowsByMonitorId(monitorId);
|
const maintenanceWindows = await this.db.maintenanceWindowModule.getMaintenanceWindowsByMonitorId(monitorId);
|
||||||
// Check for active maintenance window:
|
// Check for active maintenance window:
|
||||||
const maintenanceWindowIsActive = maintenanceWindows.reduce((acc, window) => {
|
const maintenanceWindowIsActive = maintenanceWindows.reduce((acc, window) => {
|
||||||
if (window.active) {
|
if (window.active) {
|
||||||
|
|||||||
Reference in new issue
Block a user