system settings
This commit is contained in:
1 parent
986e8b926a
commit
f40667615e
14 files changed
+244
-1
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { ISystemSettings, SystemSettings } from "@/db/models/index.js";
|
||||
import ApiError from "@/utils/ApiError.js";
|
||||
|
||||
const SERVICE_NAME = "SettingsService";
|
||||
export interface ISettingsService {
|
||||
get: () => Promise<ISystemSettings>;
|
||||
update: (settings: Partial<ISystemSettings>) => Promise<ISystemSettings>;
|
||||
}
|
||||
|
||||
class SettingsService implements ISettingsService {
|
||||
public SERVICE_NAME: string;
|
||||
|
||||
constructor() {
|
||||
this.SERVICE_NAME = SERVICE_NAME;
|
||||
}
|
||||
|
||||
get = async (): Promise<ISystemSettings> => {
|
||||
let settings = await SystemSettings.findById("global");
|
||||
|
||||
if (!settings) {
|
||||
settings = await SystemSettings.create({ _id: "global" });
|
||||
}
|
||||
|
||||
if (!settings) {
|
||||
throw new ApiError("Unable to load system settings", 500);
|
||||
}
|
||||
|
||||
return settings;
|
||||
};
|
||||
|
||||
update = async (
|
||||
payload: Partial<ISystemSettings>
|
||||
): Promise<ISystemSettings> => {
|
||||
const { _id, createdAt, updatedAt, ...updates } = payload as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
|
||||
const settings = await SystemSettings.findOneAndUpdate(
|
||||
{ _id: "global" },
|
||||
{ $set: updates },
|
||||
{ new: true, upsert: true, setDefaultsOnInsert: true }
|
||||
);
|
||||
|
||||
if (!settings) {
|
||||
throw new ApiError("Unable to update system settings", 500);
|
||||
}
|
||||
|
||||
return settings;
|
||||
};
|
||||
}
|
||||
|
||||
export default SettingsService;
|
||||
Reference in new issue
Block a user