From 30ea3aed8462282b090b64ef8fb0fea6627c2231 Mon Sep 17 00:00:00 2001 From: Kavehhn174 Date: Wed, 19 Aug 2026 07:27:07 +0330 Subject: [PATCH] feat(settings): add autosave settings API endpoints and persistence --- components/setting/settingController.js | 19 ++++++++++++ components/setting/settingRoutes.js | 12 ++++++++ components/setting/settingService.js | 41 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/components/setting/settingController.js b/components/setting/settingController.js index 0af9ae5..f32bf65 100644 --- a/components/setting/settingController.js +++ b/components/setting/settingController.js @@ -58,3 +58,22 @@ exports.seedDatabase = catchAsync(async (req, res, next) => { data: result, }) }) + +exports.getAutoSaveSetting = catchAsync(async (req, res, next) => { + const result = await settingService.getAutoSaveSetting() + + res.status(200).json({ + status: 'success', + data: result, + }) +}) + +exports.updateAutoSaveSetting = catchAsync(async (req, res, next) => { + const { enabled, intervalSeconds } = req.body + const result = await settingService.updateAutoSaveSetting({ enabled, intervalSeconds }) + + res.status(200).json({ + status: 'success', + data: result, + }) +}) diff --git a/components/setting/settingRoutes.js b/components/setting/settingRoutes.js index 2d8bdcf..dddf67c 100644 --- a/components/setting/settingRoutes.js +++ b/components/setting/settingRoutes.js @@ -43,4 +43,16 @@ router.post( settingController.seedDatabase ) +router.get( + '/autosave', + perm.requires('admindashboard', 'viewdashboard', 'getallpermissions'), + settingController.getAutoSaveSetting +) + +router.put( + '/autosave', + perm.requires('admindashboard', 'viewdashboard', 'getallpermissions'), + settingController.updateAutoSaveSetting +) + module.exports = router diff --git a/components/setting/settingService.js b/components/setting/settingService.js index e023778..04b9a69 100644 --- a/components/setting/settingService.js +++ b/components/setting/settingService.js @@ -237,6 +237,45 @@ const seedDatabase = async (userId = null) => { } } +const getAutoSaveSetting = async () => { + const record = await Setting.findOne({ key: 'general_autosave' }).lean() + if (record && record.extraConfig) { + return { + enabled: record.extraConfig.enabled !== undefined ? record.extraConfig.enabled : true, + intervalSeconds: Number(record.extraConfig.intervalSeconds) || 60, + updatedAt: record.updatedAt, + } + } + return { + enabled: true, + intervalSeconds: 60, + } +} + +const updateAutoSaveSetting = async ({ enabled = true, intervalSeconds = 60 }) => { + const validInterval = Math.max(10, Number(intervalSeconds) || 60) + const setting = await Setting.findOneAndUpdate( + { key: 'general_autosave' }, + { + key: 'general_autosave', + category: 'general', + pageKey: 'general', + title: 'تنظیمات ذخیره خودکار (Auto-Save Settings)', + description: 'پیکربندی فعال‌سازی و فاصله زمانی ذخیره خودکار فرم‌ها', + extraConfig: { + enabled: Boolean(enabled), + intervalSeconds: validInterval, + }, + }, + { upsert: true, new: true, setDefaultsOnInsert: true } + ) + return { + enabled: setting.extraConfig.enabled, + intervalSeconds: setting.extraConfig.intervalSeconds, + updatedAt: setting.updatedAt, + } +} + module.exports = { DEFAULT_SEARCH_PAGES, getSearchSettings, @@ -245,4 +284,6 @@ module.exports = { bulkUpdateSearchSettings, getSeedStatus, seedDatabase, + getAutoSaveSetting, + updateAutoSaveSetting, }