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

201 lines
4.6 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";
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();
}
console.log("Connected to MongoDB");
} catch (error) {
console.error("Failed to connect to MongoDB");
throw error;
}
2024-07-26 18:25:20 -07:00
};
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,
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,
getPageSpeedChecks,
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-10-15 11:57:37 +08:00
export default {
2024-10-17 12:07:30 +08:00
connect,
insertUser,
getUserByEmail,
updateUser,
deleteUser,
deleteTeam,
deleteAllOtherUsers,
getAllUsers,
logoutUser,
requestInviteToken,
getInviteToken,
getInviteTokenAndDelete,
requestRecoveryToken,
validateRecoveryToken,
resetPassword,
checkSuperadmin,
getAllMonitors,
getMonitorStatsById,
getMonitorById,
getMonitorsAndSummaryByTeamId,
getMonitorsByTeamId,
createMonitor,
deleteMonitor,
deleteAllMonitors,
editMonitor,
addDemoMonitors,
createCheck,
getChecksCount,
getChecks,
getTeamChecks,
deleteChecks,
deleteChecksByTeamId,
updateChecksTTL,
deleteMonitorsByUserId,
createPageSpeedCheck,
getPageSpeedChecks,
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-07-26 18:25:20 -07:00
};