Files
kspguti-schedule/src/pages/api/admin/settings.ts
2025-11-26 00:10:55 +04:00

61 lines
1.6 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from 'next'
import { requireAuth } from '@/shared/utils/auth'
import { loadSettings, saveSettings, AppSettings } from '@/shared/data/settings-loader'
type ResponseData = {
settings?: AppSettings
success?: boolean
error?: string
}
async function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
if (req.method === 'GET') {
// Получение настроек
const settings = loadSettings()
res.status(200).json({ settings })
return
}
if (req.method === 'PUT') {
// Обновление настроек
const { weekNavigationEnabled } = req.body
if (typeof weekNavigationEnabled !== 'boolean') {
res.status(400).json({ error: 'weekNavigationEnabled must be a boolean' })
return
}
const settings: AppSettings = {
weekNavigationEnabled
}
try {
saveSettings(settings)
// Сбрасываем кеш и загружаем свежие настройки для подтверждения
const { clearSettingsCache } = await import('@/shared/data/settings-loader')
clearSettingsCache()
const savedSettings = loadSettings()
res.status(200).json({ success: true, settings: savedSettings })
} catch (error) {
console.error('Error saving settings:', error)
res.status(500).json({ error: 'Failed to save settings' })
}
return
}
res.status(405).json({ error: 'Method not allowed' })
}
export default function protectedHandler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
return requireAuth(req, res, handler)
}