Add app settings model, routes, db ops, service, use settings for issueToken

This commit is contained in:
Alex Holliday committed 2024-09-26 13:41:50 +08:00
1 parent b2bc441c7f
commit 992bf099fa
12 files changed
+210 -19

No files matched your search

+20 -1
View File
@@ -1,5 +1,6 @@
const mongoose = require("mongoose");
const UserModel = require("../../models/User");
const AppSettings = require("../../models/AppSettings");
//****************************************
// DB Connection
@@ -7,7 +8,16 @@ const UserModel = require("../../models/User");
const connect = async () => {
try {
await mongoose.connect(process.env.DB_CONNECTION_STRING);
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");
@@ -115,12 +125,20 @@ const {
deleteMaintenanceWindowByUserId,
} = require("./modules/maintenaceWindowModule");
//****************************************
// Notifications
//****************************************
const {
createNotification,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
} = require("./modules/notificationModule");
//****************************************
// AppSettings
//****************************************
const { getAppSettings } = require("./modules/settingsModule");
module.exports = {
connect,
insertUser,
@@ -168,4 +186,5 @@ module.exports = {
createNotification,
getNotificationsByMonitorId,
deleteNotificationsByMonitorId,
getAppSettings,
};
+17
View File
@@ -0,0 +1,17 @@
const AppSettings = require("../../../models/AppSettings");
const SERVICE_NAME = "SettingsModule";
const getAppSettings = async () => {
try {
const settings = AppSettings.findOne();
return settings;
} catch (error) {
error.service = SERVICE_NAME;
error.method = "getSettings";
throw error;
}
};
module.exports = {
getAppSettings,
};
+1
View File
@@ -35,6 +35,7 @@ const insertUser = async (userData, imageFile) => {
email: userData.email,
});
userData.teamId = team._id;
userData.checkTTL = 60 * 60 * 24 * 30;
await team.save();
}