50 lines
2.4 KiB
TypeScript
50 lines
2.4 KiB
TypeScript
"use server";
|
||
// PEP-209: the superadmin's buttons on a delay card («Дашборд → Задержки»); PEP-279: old questions.
|
||
|
||
import { DELAY_REASON_NAMES, PeppaError, requireSuperadmin, type DelayAction, type DelayReason } from "@peppa/core";
|
||
import { closeStaleQuestions, delayAction } from "@peppa/runner";
|
||
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";
|
||
|
||
const BASE = "/dashboard/delays";
|
||
const str = (f: FormData, k: string) => String(f.get(k) ?? "").trim();
|
||
|
||
export async function delayActionForm(f: FormData) {
|
||
const user = asWebActor(await requireUser());
|
||
const params = new URLSearchParams();
|
||
try {
|
||
await requireSuperadmin(db, user);
|
||
const reason = str(f, "reason") as DelayReason;
|
||
const action = str(f, "action") as DelayAction;
|
||
const projectId = Number(str(f, "project"));
|
||
const taskId = str(f, "task") ? Number(str(f, "task")) : null;
|
||
if (!(reason in DELAY_REASON_NAMES) || !["restart", "review", "close"].includes(action) || !Number.isInteger(projectId)) throw new PeppaError("?");
|
||
const t = await getT();
|
||
params.set("ok", t.text(await delayAction(db, { projectId, taskId }, reason, action)).slice(0, 300));
|
||
} catch (e) {
|
||
const t = await getT();
|
||
params.set("error", (e instanceof PeppaError ? t.text(e.message) : t("Ошибка: {message}", { message: (e as Error).message })).slice(0, 300));
|
||
}
|
||
revalidatePath("/", "layout");
|
||
redirect(`${BASE}?${params}`);
|
||
}
|
||
|
||
/** PEP-279: «Закрыть все» on the old questions page — their default option, one summary per person. */
|
||
export async function closeStaleQuestionsAction() {
|
||
const user = asWebActor(await requireUser());
|
||
const params = new URLSearchParams();
|
||
const t = await getT();
|
||
try {
|
||
await requireSuperadmin(db, user);
|
||
const r = await closeStaleQuestions(db);
|
||
params.set("ok", t("Закрыто вопросов: {n}. Итоговое сообщение получили людей: {m}.", { n: r.closed.length, m: r.told }) + (r.failed ? " " + t("Не удалось закрыть: {n}.", { n: r.failed }) : ""));
|
||
} catch (e) {
|
||
params.set("error", (e instanceof PeppaError ? t.text(e.message) : t("Ошибка: {message}", { message: (e as Error).message })).slice(0, 300));
|
||
}
|
||
revalidatePath("/", "layout");
|
||
redirect(`/dashboard/questions?${params}`);
|
||
}
|