Merge pull request #2729 from bluewave-labs/feat/settings-module
feat: settubgs module -> class
This commit is contained in:
5 files changed
+37
-38
No files matched your search
@@ -54,6 +54,7 @@ import MonitorStats from "../db/models/MonitorStats.js";
|
|||||||
import NetworkCheck from "../db/models/NetworkCheck.js";
|
import NetworkCheck from "../db/models/NetworkCheck.js";
|
||||||
import Notification from "../db/models/Notification.js";
|
import Notification from "../db/models/Notification.js";
|
||||||
import RecoveryToken from "../db/models/RecoveryToken.js";
|
import RecoveryToken from "../db/models/RecoveryToken.js";
|
||||||
|
import AppSettings from "../db/models/AppSettings.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";
|
||||||
@@ -66,6 +67,7 @@ import NetworkCheckModule from "../db/mongo/modules/networkCheckModule.js";
|
|||||||
import NotificationModule from "../db/mongo/modules/notificationModule.js";
|
import NotificationModule from "../db/mongo/modules/notificationModule.js";
|
||||||
import PageSpeedCheckModule from "../db/mongo/modules/pageSpeedCheckModule.js";
|
import PageSpeedCheckModule from "../db/mongo/modules/pageSpeedCheckModule.js";
|
||||||
import RecoveryModule from "../db/mongo/modules/recoveryModule.js";
|
import RecoveryModule from "../db/mongo/modules/recoveryModule.js";
|
||||||
|
import SettingsModule from "../db/mongo/modules/settingsModule.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 });
|
||||||
@@ -101,6 +103,7 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
|||||||
const notificationModule = new NotificationModule({ Notification, Monitor });
|
const notificationModule = new NotificationModule({ Notification, Monitor });
|
||||||
const pageSpeedCheckModule = new PageSpeedCheckModule({ PageSpeedCheck });
|
const pageSpeedCheckModule = new PageSpeedCheckModule({ PageSpeedCheck });
|
||||||
const recoveryModule = new RecoveryModule({ User, RecoveryToken, crypto, stringService });
|
const recoveryModule = new RecoveryModule({ User, RecoveryToken, crypto, stringService });
|
||||||
|
const settingsModule = new SettingsModule({ AppSettings });
|
||||||
|
|
||||||
const db = new MongoDB({
|
const db = new MongoDB({
|
||||||
logger,
|
logger,
|
||||||
@@ -116,6 +119,7 @@ export const initializeServices = async ({ logger, envSettings, settingsService
|
|||||||
notificationModule,
|
notificationModule,
|
||||||
pageSpeedCheckModule,
|
pageSpeedCheckModule,
|
||||||
recoveryModule,
|
recoveryModule,
|
||||||
|
settingsModule,
|
||||||
});
|
});
|
||||||
|
|
||||||
await db.connect();
|
await db.connect();
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ class SettingsController extends BaseController {
|
|||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
await updateAppSettingsBodyValidation.validateAsync(req.body);
|
await updateAppSettingsBodyValidation.validateAsync(req.body);
|
||||||
|
|
||||||
const updatedSettings = await this.db.updateAppSettings(req.body);
|
const updatedSettings = await this.db.settingsModule.updateAppSettings(req.body);
|
||||||
const returnSettings = this.buildAppSettings(updatedSettings);
|
const returnSettings = this.buildAppSettings(updatedSettings);
|
||||||
return res.success({
|
return res.success({
|
||||||
msg: this.stringService.updateAppSettings,
|
msg: this.stringService.updateAppSettings,
|
||||||
|
|||||||
@@ -44,20 +44,20 @@ class MongoDB {
|
|||||||
notificationModule,
|
notificationModule,
|
||||||
pageSpeedCheckModule,
|
pageSpeedCheckModule,
|
||||||
recoveryModule,
|
recoveryModule,
|
||||||
|
settingsModule,
|
||||||
}) {
|
}) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.envSettings = envSettings;
|
this.envSettings = envSettings;
|
||||||
this.userModule = userModule;
|
this.userModule = userModule;
|
||||||
this.inviteModule = inviteModule;
|
this.inviteModule = inviteModule;
|
||||||
this.recoveryModule = recoveryModule;
|
this.recoveryModule = recoveryModule;
|
||||||
Object.assign(this, monitorModule);
|
|
||||||
this.pageSpeedCheckModule = pageSpeedCheckModule;
|
this.pageSpeedCheckModule = pageSpeedCheckModule;
|
||||||
this.hardwareCheckModule = hardwareCheckModule;
|
this.hardwareCheckModule = hardwareCheckModule;
|
||||||
this.checkModule = checkModule;
|
this.checkModule = checkModule;
|
||||||
this.maintenanceWindowModule = maintenanceWindowModule;
|
this.maintenanceWindowModule = maintenanceWindowModule;
|
||||||
this.monitorModule = monitorModule;
|
this.monitorModule = monitorModule;
|
||||||
this.notificationModule = notificationModule;
|
this.notificationModule = notificationModule;
|
||||||
Object.assign(this, settingsModule);
|
this.settingsModule = settingsModule;
|
||||||
this.statusPageModule = statusPageModule;
|
this.statusPageModule = statusPageModule;
|
||||||
Object.assign(this, diagnosticModule);
|
Object.assign(this, diagnosticModule);
|
||||||
this.networkCheckModule = networkCheckModule;
|
this.networkCheckModule = networkCheckModule;
|
||||||
|
|||||||
@@ -1,41 +1,36 @@
|
|||||||
import AppSettings from "../../models/AppSettings.js";
|
// import AppSettings from "../../models/AppSettings.js";
|
||||||
const SERVICE_NAME = "SettingsModule";
|
const SERVICE_NAME = "SettingsModule";
|
||||||
|
|
||||||
const getAppSettings = async () => {
|
class SettingsModule {
|
||||||
try {
|
constructor({ AppSettings }) {
|
||||||
const settings = AppSettings.findOne();
|
this.AppSettings = AppSettings;
|
||||||
return settings;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "getSettings";
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
const updateAppSettings = async (newSettings) => {
|
updateAppSettings = async (newSettings) => {
|
||||||
try {
|
try {
|
||||||
const update = { $set: { ...newSettings } };
|
const update = { $set: { ...newSettings } };
|
||||||
|
|
||||||
if (newSettings.pagespeedApiKey === "") {
|
if (newSettings.pagespeedApiKey === "") {
|
||||||
update.$unset = { pagespeedApiKey: "" };
|
update.$unset = { pagespeedApiKey: "" };
|
||||||
delete update.$set.pagespeedApiKey;
|
delete update.$set.pagespeedApiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newSettings.systemEmailPassword === "") {
|
||||||
|
update.$unset = { systemEmailPassword: "" };
|
||||||
|
delete update.$set.systemEmailPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.AppSettings.findOneAndUpdate({}, update, {
|
||||||
|
upsert: true,
|
||||||
|
});
|
||||||
|
const settings = await this.AppSettings.findOne().select("-__v -_id -createdAt -updatedAt -singleton").lean();
|
||||||
|
return settings;
|
||||||
|
} catch (error) {
|
||||||
|
error.service = SERVICE_NAME;
|
||||||
|
error.method = "updateAppSettings";
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (newSettings.systemEmailPassword === "") {
|
export default SettingsModule;
|
||||||
update.$unset = { systemEmailPassword: "" };
|
|
||||||
delete update.$set.systemEmailPassword;
|
|
||||||
}
|
|
||||||
|
|
||||||
await AppSettings.findOneAndUpdate({}, update, {
|
|
||||||
upsert: true,
|
|
||||||
});
|
|
||||||
const settings = await AppSettings.findOne().select("-__v -_id -createdAt -updatedAt -singleton").lean();
|
|
||||||
return settings;
|
|
||||||
} catch (error) {
|
|
||||||
error.service = SERVICE_NAME;
|
|
||||||
error.method = "updateAppSettings";
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export { getAppSettings, updateAppSettings };
|
|
||||||
@@ -37,7 +37,7 @@ class UserService {
|
|||||||
} else {
|
} else {
|
||||||
// This is the first account, create JWT secret to use if one is not supplied by env
|
// This is the first account, create JWT secret to use if one is not supplied by env
|
||||||
const jwtSecret = this.crypto.randomBytes(64).toString("hex");
|
const jwtSecret = this.crypto.randomBytes(64).toString("hex");
|
||||||
await this.db.updateAppSettings({ jwtSecret });
|
await this.db.settingsModule.updateAppSettings({ jwtSecret });
|
||||||
}
|
}
|
||||||
|
|
||||||
const newUser = await this.db.userModule.insertUser({ ...user }, file);
|
const newUser = await this.db.userModule.insertUser({ ...user }, file);
|
||||||
|
|||||||
Reference in new issue
Block a user