"use server"; import { headers } from "next/headers"; import { auth } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; import { revalidatePath } from "next/cache"; import { SETTINGS_DEFAULTS, type SettingsKey } from "@/lib/settings"; export async function saveSettings(data: Record) { const session = await auth.api.getSession({ headers: await headers() }); if (!session || session.user.role !== "admin") throw new Error("Нет доступа"); const validKeys = Object.keys(SETTINGS_DEFAULTS) as SettingsKey[]; const ops = validKeys .filter((key) => key in data) .map((key) => prisma.settings.upsert({ where: { key }, update: { value: data[key] }, create: { key, value: data[key] }, }) ); await Promise.all(ops); revalidatePath("/admin/settings"); revalidatePath("/", "layout"); }