Files
Checkmate/Server/db/mongo/MongoDB.js
T

231 lines
5.3 KiB
JavaScript
Raw Normal View History

2024-10-15 11:57:37 +08:00
import mongoose from "mongoose";
import UserModel from "../models/User.js";
import AppSettings from "../models/AppSettings.js";
import logger from "../../utils/logger.js";
const SERVICE_NAME = "MongoDB";
2024-07-26 18:25:20 -07:00
//****************************************
// DB Connection
//****************************************
const connect = async () => {
2024-10-17 12:07:30 +08:00
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();
}
2024-10-24 15:28:13 +08:00
logger.info({ message: "Connected to MongoDB" });
2024-10-17 12:07:30 +08:00
} catch (error) {
2024-10-24 15:28:13 +08:00
logger.error({
message: error.message,
service: SERVICE_NAME,
method: "connect",
2024-10-24 15:28:13 +08:00
stack: error.stack,
});
2024-10-17 12:07:30 +08:00
throw error;
}
2024-07-26 18:25:20 -07:00
};
const disconnect = async () => {
try {
2024-10-24 15:28:13 +08:00
logger.info({ message: "Disconnecting from MongoDB" });
await mongoose.disconnect();
2024-10-24 15:28:13 +08:00
logger.info({ message: "Disconnected from MongoDB" });
return;
} catch (error) {
2024-10-24 15:28:13 +08:00
logger.error({
message: error.message,
service: SERVICE_NAME,
2024-10-24 15:28:13 +08:00
method: "disconnect",
stack: error.stack,
});
}
};
2024-08-26 09:46:23 -07:00
const checkSuperadmin = async (req, res) => {
2024-10-17 12:07:30 +08:00
try {
const superAdmin = await UserModel.findOne({ role: "superadmin" });
if (superAdmin !== null) {
return true;
}
return false;
} catch (error) {
throw error;
}
2024-07-26 18:25:20 -07:00
};
//****************************************
// User Operations
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
insertUser,
getUserByEmail,
updateUser,
deleteUser,
deleteTeam,
deleteAllOtherUsers,
getAllUsers,
logoutUser,
2024-10-15 11:57:37 +08:00
} from "./modules/userModule.js";
2024-07-26 18:25:20 -07:00
//****************************************
// Invite Token Operations
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
requestInviteToken,
getInviteToken,
getInviteTokenAndDelete,
2024-10-15 11:57:37 +08:00
} from "./modules/inviteModule.js";
2024-07-26 18:25:20 -07:00
//****************************************
// Recovery Operations
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
requestRecoveryToken,
validateRecoveryToken,
resetPassword,
2024-10-15 11:57:37 +08:00
} from "./modules/recoveryModule.js";
2024-07-26 18:25:20 -07:00
//****************************************
// Monitors
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
getAllMonitors,
2024-11-06 11:18:34 +08:00
getAllMonitorsWithUptimeStats,
2024-10-17 12:07:30 +08:00
getMonitorStatsById,
getMonitorById,
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
deleteMonitor,
deleteAllMonitors,
deleteMonitorsByUserId,
editMonitor,
addDemoMonitors,
2024-10-15 11:57:37 +08:00
} from "./modules/monitorModule.js";
2024-07-26 18:25:20 -07:00
//****************************************
// Page Speed Checks
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
createPageSpeedCheck,
deletePageSpeedChecksByMonitorId,
2024-10-15 11:57:37 +08:00
} from "./modules/pageSpeedCheckModule.js";
2024-07-26 18:25:20 -07:00
2024-10-21 11:55:18 +08:00
//****************************************
// Hardware Checks
//****************************************
import { createHardwareCheck } from "./modules/hardwareCheckModule.js";
2024-07-26 18:25:20 -07:00
//****************************************
// Checks
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
createCheck,
getChecksCount,
getChecks,
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
2024-10-15 11:57:37 +08:00
} from "./modules/checkModule.js";
2024-07-26 18:25:20 -07:00
2024-07-30 10:01:52 -07:00
//****************************************
// Maintenance Window
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
createMaintenanceWindow,
getMaintenanceWindowById,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindowById,
deleteMaintenanceWindowByMonitorId,
deleteMaintenanceWindowByUserId,
editMaintenanceWindowById,
2024-10-15 11:57:37 +08:00
} from "./modules/maintenanceWindowModule.js";
2024-07-30 10:01:52 -07:00
//****************************************
// Notifications
//****************************************
2024-10-15 11:57:37 +08:00
import {
2024-10-17 12:07:30 +08:00
createNotification,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
2024-10-15 11:57:37 +08:00
} from "./modules/notificationModule.js";
2024-07-30 13:23:14 -07:00
//****************************************
// AppSettings
//****************************************
2024-10-15 11:57:37 +08:00
import { getAppSettings, updateAppSettings } from "./modules/settingsModule.js";
2024-11-11 12:16:44 +08:00
//****************************************
// Status Page
//****************************************
import { createStatusPage, getStatusPageByUrl } from "./modules/statusPageModule.js";
2024-10-15 11:57:37 +08:00
export default {
2024-10-17 12:07:30 +08:00
connect,
disconnect,
2024-10-17 12:07:30 +08:00
insertUser,
getUserByEmail,
updateUser,
deleteUser,
deleteTeam,
deleteAllOtherUsers,
getAllUsers,
logoutUser,
requestInviteToken,
getInviteToken,
getInviteTokenAndDelete,
requestRecoveryToken,
validateRecoveryToken,
resetPassword,
checkSuperadmin,
getAllMonitors,
2024-11-06 11:18:34 +08:00
getAllMonitorsWithUptimeStats,
2024-10-17 12:07:30 +08:00
getMonitorStatsById,
getMonitorById,
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
deleteMonitor,
deleteAllMonitors,
editMonitor,
addDemoMonitors,
createCheck,
getChecksCount,
getChecks,
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
deleteMonitorsByUserId,
createPageSpeedCheck,
deletePageSpeedChecksByMonitorId,
2024-10-21 11:55:18 +08:00
createHardwareCheck,
2024-10-17 12:07:30 +08:00
createMaintenanceWindow,
getMaintenanceWindowsByTeamId,
getMaintenanceWindowById,
getMaintenanceWindowsByMonitorId,
deleteMaintenanceWindowById,
deleteMaintenanceWindowByMonitorId,
deleteMaintenanceWindowByUserId,
editMaintenanceWindowById,
createNotification,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
getAppSettings,
updateAppSettings,
2024-11-11 12:16:44 +08:00
createStatusPage,
getStatusPageByUrl,
2024-07-26 18:25:20 -07:00
};