refacgtor MongoDB to be class based
This commit is contained in:
1 parent
0861595d39
commit
7f6e096491
1 file changed
+76
-181
+76
-181
@@ -2,234 +2,129 @@ import mongoose from "mongoose";
|
|||||||
import UserModel from "../models/User.js";
|
import UserModel from "../models/User.js";
|
||||||
import AppSettings from "../models/AppSettings.js";
|
import AppSettings from "../models/AppSettings.js";
|
||||||
import logger from "../../utils/logger.js";
|
import logger from "../../utils/logger.js";
|
||||||
const SERVICE_NAME = "MongoDB";
|
|
||||||
|
|
||||||
//****************************************
|
|
||||||
// DB Connection
|
|
||||||
//****************************************
|
|
||||||
|
|
||||||
const connect = async () => {
|
|
||||||
try {
|
|
||||||
const connectionString =
|
|
||||||
process.env.DB_CONNECTION_STRING || "mongodb://localhost:27017/uptime_db";
|
|
||||||
await mongoose.connect(connectionString);
|
|
||||||
// If there are no AppSettings, create one
|
|
||||||
let appSettings = await AppSettings.find();
|
|
||||||
if (appSettings.length === 0) {
|
|
||||||
appSettings = new AppSettings({});
|
|
||||||
await appSettings.save();
|
|
||||||
}
|
|
||||||
logger.info({ message: "Connected to MongoDB" });
|
|
||||||
} catch (error) {
|
|
||||||
logger.error({
|
|
||||||
message: error.message,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "connect",
|
|
||||||
stack: error.stack,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const disconnect = async () => {
|
|
||||||
try {
|
|
||||||
logger.info({ message: "Disconnecting from MongoDB" });
|
|
||||||
await mongoose.disconnect();
|
|
||||||
logger.info({ message: "Disconnected from MongoDB" });
|
|
||||||
return;
|
|
||||||
} catch (error) {
|
|
||||||
logger.error({
|
|
||||||
message: error.message,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "disconnect",
|
|
||||||
stack: error.stack,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const checkSuperadmin = async (req, res) => {
|
|
||||||
try {
|
|
||||||
const superAdmin = await UserModel.findOne({ role: "superadmin" });
|
|
||||||
if (superAdmin !== null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// User Operations
|
// User Operations
|
||||||
//****************************************
|
//****************************************
|
||||||
|
|
||||||
import {
|
import * as userModule from "./modules/userModule.js";
|
||||||
insertUser,
|
|
||||||
getUserByEmail,
|
|
||||||
updateUser,
|
|
||||||
deleteUser,
|
|
||||||
deleteTeam,
|
|
||||||
deleteAllOtherUsers,
|
|
||||||
getAllUsers,
|
|
||||||
logoutUser,
|
|
||||||
} from "./modules/userModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Invite Token Operations
|
// Invite Token Operations
|
||||||
//****************************************
|
//****************************************
|
||||||
|
|
||||||
import {
|
import * as inviteModule from "./modules/inviteModule.js";
|
||||||
requestInviteToken,
|
|
||||||
getInviteToken,
|
|
||||||
getInviteTokenAndDelete,
|
|
||||||
} from "./modules/inviteModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Recovery Operations
|
// Recovery Operations
|
||||||
//****************************************
|
//****************************************
|
||||||
import {
|
import * as recoveryModule from "./modules/recoveryModule.js";
|
||||||
requestRecoveryToken,
|
|
||||||
validateRecoveryToken,
|
|
||||||
resetPassword,
|
|
||||||
} from "./modules/recoveryModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Monitors
|
// Monitors
|
||||||
//****************************************
|
//****************************************
|
||||||
|
|
||||||
import {
|
import * as monitorModule from "./modules/monitorModule.js";
|
||||||
getAllMonitors,
|
|
||||||
getAllMonitorsWithUptimeStats,
|
|
||||||
getMonitorStatsById,
|
|
||||||
getMonitorById,
|
|
||||||
getMonitorsAndSummaryByTeamId,
|
|
||||||
getMonitorsByTeamId,
|
|
||||||
createMonitor,
|
|
||||||
deleteMonitor,
|
|
||||||
deleteAllMonitors,
|
|
||||||
deleteMonitorsByUserId,
|
|
||||||
editMonitor,
|
|
||||||
addDemoMonitors,
|
|
||||||
} from "./modules/monitorModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Page Speed Checks
|
// Page Speed Checks
|
||||||
//****************************************
|
//****************************************
|
||||||
|
|
||||||
import {
|
import * as pageSpeedCheckModule from "./modules/pageSpeedCheckModule.js";
|
||||||
createPageSpeedCheck,
|
|
||||||
deletePageSpeedChecksByMonitorId,
|
|
||||||
} from "./modules/pageSpeedCheckModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Hardware Checks
|
// Hardware Checks
|
||||||
//****************************************
|
//****************************************
|
||||||
import {
|
import * as hardwareCheckModule from "./modules/hardwareCheckModule.js";
|
||||||
createHardwareCheck,
|
|
||||||
deleteHardwareChecksByMonitorId,
|
|
||||||
} from "./modules/hardwareCheckModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Checks
|
// Checks
|
||||||
//****************************************
|
//****************************************
|
||||||
|
|
||||||
import {
|
import * as checkModule from "./modules/checkModule.js";
|
||||||
createCheck,
|
|
||||||
getChecksCount,
|
|
||||||
getChecks,
|
|
||||||
getTeamChecks,
|
|
||||||
deleteChecks,
|
|
||||||
deleteChecksByTeamId,
|
|
||||||
updateChecksTTL,
|
|
||||||
} from "./modules/checkModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Maintenance Window
|
// Maintenance Window
|
||||||
//****************************************
|
//****************************************
|
||||||
import {
|
import * as maintenanceWindowModule from "./modules/maintenanceWindowModule.js";
|
||||||
createMaintenanceWindow,
|
|
||||||
getMaintenanceWindowById,
|
|
||||||
getMaintenanceWindowsByTeamId,
|
|
||||||
getMaintenanceWindowsByMonitorId,
|
|
||||||
deleteMaintenanceWindowById,
|
|
||||||
deleteMaintenanceWindowByMonitorId,
|
|
||||||
deleteMaintenanceWindowByUserId,
|
|
||||||
editMaintenanceWindowById,
|
|
||||||
} from "./modules/maintenanceWindowModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Notifications
|
// Notifications
|
||||||
//****************************************
|
//****************************************
|
||||||
import {
|
import * as notificationModule from "./modules/notificationModule.js";
|
||||||
createNotification,
|
|
||||||
getNotificationsByMonitorId,
|
|
||||||
deleteNotificationsByMonitorId,
|
|
||||||
} from "./modules/notificationModule.js";
|
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// AppSettings
|
// AppSettings
|
||||||
//****************************************
|
//****************************************
|
||||||
import { getAppSettings, updateAppSettings } from "./modules/settingsModule.js";
|
import * as settingsModule from "./modules/settingsModule.js";
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
// Status Page
|
// Status Page
|
||||||
//****************************************
|
//****************************************
|
||||||
import { createStatusPage, getStatusPageByUrl } from "./modules/statusPageModule.js";
|
import * as statusPageModule from "./modules/statusPageModule.js";
|
||||||
|
|
||||||
export default {
|
class MongoDB {
|
||||||
connect,
|
static SERVICE_NAME = "MongoDB";
|
||||||
disconnect,
|
|
||||||
insertUser,
|
constructor() {
|
||||||
getUserByEmail,
|
Object.assign(this, userModule);
|
||||||
updateUser,
|
Object.assign(this, inviteModule);
|
||||||
deleteUser,
|
Object.assign(this, recoveryModule);
|
||||||
deleteTeam,
|
Object.assign(this, monitorModule);
|
||||||
deleteAllOtherUsers,
|
Object.assign(this, pageSpeedCheckModule);
|
||||||
getAllUsers,
|
Object.assign(this, hardwareCheckModule);
|
||||||
logoutUser,
|
Object.assign(this, checkModule);
|
||||||
requestInviteToken,
|
Object.assign(this, maintenanceWindowModule);
|
||||||
getInviteToken,
|
Object.assign(this, notificationModule);
|
||||||
getInviteTokenAndDelete,
|
Object.assign(this, settingsModule);
|
||||||
requestRecoveryToken,
|
Object.assign(this, statusPageModule);
|
||||||
validateRecoveryToken,
|
}
|
||||||
resetPassword,
|
|
||||||
checkSuperadmin,
|
connect = async () => {
|
||||||
getAllMonitors,
|
try {
|
||||||
getAllMonitorsWithUptimeStats,
|
const connectionString =
|
||||||
getMonitorStatsById,
|
process.env.DB_CONNECTION_STRING || "mongodb://localhost:27017/uptime_db";
|
||||||
getMonitorById,
|
await mongoose.connect(connectionString);
|
||||||
getMonitorsAndSummaryByTeamId,
|
// If there are no AppSettings, create one
|
||||||
getMonitorsByTeamId,
|
let appSettings = await AppSettings.find();
|
||||||
createMonitor,
|
if (appSettings.length === 0) {
|
||||||
deleteMonitor,
|
appSettings = new AppSettings({});
|
||||||
deleteAllMonitors,
|
await appSettings.save();
|
||||||
editMonitor,
|
}
|
||||||
addDemoMonitors,
|
logger.info({ message: "Connected to MongoDB" });
|
||||||
createCheck,
|
} catch (error) {
|
||||||
getChecksCount,
|
logger.error({
|
||||||
getChecks,
|
message: error.message,
|
||||||
getTeamChecks,
|
service: this.SERVICE_NAME,
|
||||||
deleteChecks,
|
method: "connect",
|
||||||
deleteChecksByTeamId,
|
stack: error.stack,
|
||||||
updateChecksTTL,
|
});
|
||||||
deleteMonitorsByUserId,
|
throw error;
|
||||||
createPageSpeedCheck,
|
}
|
||||||
deletePageSpeedChecksByMonitorId,
|
};
|
||||||
createHardwareCheck,
|
|
||||||
deleteHardwareChecksByMonitorId,
|
disconnect = async () => {
|
||||||
createMaintenanceWindow,
|
try {
|
||||||
getMaintenanceWindowsByTeamId,
|
logger.info({ message: "Disconnecting from MongoDB" });
|
||||||
getMaintenanceWindowById,
|
await mongoose.disconnect();
|
||||||
getMaintenanceWindowsByMonitorId,
|
logger.info({ message: "Disconnected from MongoDB" });
|
||||||
deleteMaintenanceWindowById,
|
return;
|
||||||
deleteMaintenanceWindowByMonitorId,
|
} catch (error) {
|
||||||
deleteMaintenanceWindowByUserId,
|
logger.error({
|
||||||
editMaintenanceWindowById,
|
message: error.message,
|
||||||
createNotification,
|
service: this.SERVICE_NAME,
|
||||||
getNotificationsByMonitorId,
|
method: "disconnect",
|
||||||
deleteNotificationsByMonitorId,
|
stack: error.stack,
|
||||||
getAppSettings,
|
});
|
||||||
updateAppSettings,
|
}
|
||||||
createStatusPage,
|
};
|
||||||
getStatusPageByUrl,
|
checkSuperadmin = async (req, res) => {
|
||||||
};
|
const superAdmin = await UserModel.findOne({ role: "superadmin" });
|
||||||
|
if (superAdmin !== null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MongoDB;
|
||||||
Reference in new issue
Block a user