2025-04-20 11:29:53 -07:00
|
|
|
import AppSettings from "../../models/AppSettings.js";
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateAppSettings = async (newSettings) => {
|
|
|
|
|
try {
|
2025-05-09 13:26:39 -07:00
|
|
|
const update = { $set: { ...newSettings } };
|
|
|
|
|
|
|
|
|
|
if (newSettings.pagespeedApiKey === "") {
|
|
|
|
|
update.$unset = { pagespeedApiKey: "" };
|
|
|
|
|
delete update.$set.pagespeedApiKey;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 14:53:03 -07:00
|
|
|
if (newSettings.systemEmailPassword === "") {
|
|
|
|
|
update.$unset = { systemEmailPassword: "" };
|
|
|
|
|
delete update.$set.systemEmailPassword;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 13:26:39 -07:00
|
|
|
const settings = await AppSettings.findOneAndUpdate({}, update, {
|
|
|
|
|
new: true,
|
|
|
|
|
upsert: true,
|
|
|
|
|
});
|
2025-04-20 11:29:53 -07:00
|
|
|
return settings;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
error.service = SERVICE_NAME;
|
|
|
|
|
error.method = "updateAppSettings";
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { getAppSettings, updateAppSettings };
|