peppa/apps/web/app/(app)/team/actions.ts
peppa-bot db1dd78a9e PEP-183: свежий main (PEP-179/182)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 17:00:54 +05:00

61 lines
2.2 KiB
TypeScript

"use server";
import { createAgent, isSuperadmin, PeppaError, setTeamChaseHidden, setTeamSettings, updateAgent } from "@peppa/core";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { asWebActor, requireUser } from "@/lib/auth";
import { db } from "@/lib/db";
import { getT } from "@/lib/i18n";
import { m } from "@peppa/shared/i18n";
/** PEP-75: the team is platform-wide, so only a superadmin changes it. */
async function run(fn: () => Promise<string | void>): Promise<never> {
let target = "/team";
try {
const user = asWebActor(await requireUser());
if (!(await isSuperadmin(db, user))) throw new PeppaError(m("Команду меняет только суперадмин"));
target = (await fn()) || target;
} catch (e) {
const t = await getT();
const msg = e instanceof PeppaError ? t.text(e.message) : t("Ошибка: {message}", { message: (e as Error).message });
target = `/team?error=${encodeURIComponent(msg.slice(0, 300))}`;
}
revalidatePath("/", "layout");
redirect(target);
}
export async function addAgentAction(f: FormData) {
await run(async () => {
const a = await createAgent(db, { name: String(f.get("name") ?? "") });
return `/team?saved=${a.id}#agent-${a.id}`;
});
}
export async function saveAgentAction(f: FormData) {
await run(async () => {
const id = Number(f.get("id"));
await updateAgent(db, id, { name: String(f.get("name") ?? ""), character: String(f.get("character") ?? "") });
return `/team?saved=${id}#agent-${id}`;
});
}
export async function toggleAgentAction(f: FormData) {
await run(async () => {
await updateAgent(db, Number(f.get("id")), { disabled: f.get("disabled") === "1" });
});
}
/** PEP-183: how long a story waits for its busy specialist. */
export async function saveTeamWaitAction(f: FormData) {
await run(async () => {
await setTeamSettings(db, { waitMin: Number(f.get("waitMin")) });
return "/team?saved=wait";
});
}
/** PEP-179: «Скрыть» / «Показать» on the team picture — the user's own choice, anyone may. */
export async function setTeamChaseHiddenAction(hidden: boolean): Promise<void> {
const user = await requireUser();
await setTeamChaseHidden(db, user.id, hidden);
}