return updated settings

This commit is contained in:
Alex Holliday committed 2025-06-17 10:58:30 +08:00
1 parent 7c275db662
commit 7d333c6d93
3 files changed
+22 -16

No files matched your search

+12 -6
View File
@@ -11,8 +11,7 @@ class SettingsController {
this.emailService = emailService;
}
getAppSettings = async (req, res, next) => {
const dbSettings = await this.settingsService.getDBSettings();
buildAppSettings = (dbSettings) => {
const sanitizedSettings = { ...dbSettings };
delete sanitizedSettings.version;
@@ -30,6 +29,14 @@ class SettingsController {
delete sanitizedSettings.systemEmailPassword;
}
returnSettings.settings = sanitizedSettings;
return returnSettings;
};
getAppSettings = async (req, res, next) => {
const dbSettings = await this.settingsService.getDBSettings();
const returnSettings = this.buildAppSettings(dbSettings);
console.log(returnSettings);
return res.success({
msg: this.stringService.getAppSettings,
data: returnSettings,
@@ -45,12 +52,11 @@ class SettingsController {
}
try {
await this.db.updateAppSettings(req.body);
const updatedSettings = { ...(await this.settingsService.reloadSettings()) };
delete updatedSettings.jwtSecret;
const updatedSettings = await this.db.updateAppSettings(req.body);
const returnSettings = this.buildAppSettings(updatedSettings);
return res.success({
msg: this.stringService.updateAppSettings,
data: updatedSettings,
data: returnSettings,
});
} catch (error) {
next(handleError(error, SERVICE_NAME, "updateAppSettings"));
+4 -2
View File
@@ -26,10 +26,12 @@ const updateAppSettings = async (newSettings) => {
delete update.$set.systemEmailPassword;
}
const settings = await AppSettings.findOneAndUpdate({}, update, {
new: true,
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;
+6 -8
View File
@@ -28,8 +28,8 @@ class SettingsService {
* Constructs a new SettingsService
* @constructor
* @throws {Error}
*/ constructor(appSettings) {
this.appSettings = appSettings;
*/ constructor(AppSettings) {
this.AppSettings = AppSettings;
this.settings = { ...envConfig };
}
/**
@@ -60,16 +60,14 @@ class SettingsService {
async getDBSettings() {
// Remove any old settings
await this.appSettings.deleteMany({ version: { $exists: false } });
await this.AppSettings.deleteMany({ version: { $exists: false } });
let settings = await this.appSettings
.findOne({ singleton: true })
let settings = await this.AppSettings.findOne({ singleton: true })
.select("-__v -_id -createdAt -updatedAt -singleton")
.lean();
if (settings === null) {
await this.appSettings.create({});
settings = await this.appSettings
.findOne({ singleton: true })
await this.AppSettings.create({});
settings = await this.AppSettings.findOne({ singleton: true })
.select("-__v -_id -createdAt -updatedAt -singleton")
.lean();
}