46 lines
2.1 KiB
TypeScript
46 lines
2.1 KiB
TypeScript
"use server";
|
||
// PEP-218: actions on a Bugsink error card. Access is the viewer's error scope (superadmin — all,
|
||
// otherwise the Bugsink projects of their own projects); outside it — «not found».
|
||
import { errorScope, PeppaError, platformFailure, setErrorState } from "@peppa/core";
|
||
import { fileErrorTask, resolveError } from "@peppa/runner";
|
||
import { m } from "@peppa/shared/i18n";
|
||
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 str = (f: FormData, k: string) => String(f.get(k) ?? "").trim();
|
||
|
||
/** Only a local path: no open redirect through `back`. */
|
||
function safeBack(v: string, fallback: string): string {
|
||
return v.startsWith("/") && !v.startsWith("//") ? v : fallback;
|
||
}
|
||
|
||
export async function errorAction(f: FormData) {
|
||
const user = asWebActor(await requireUser());
|
||
const id = str(f, "id");
|
||
let target = safeBack(str(f, "back"), `/errors/${encodeURIComponent(id)}`);
|
||
try {
|
||
const scope = await errorScope(db, user);
|
||
const action = str(f, "action");
|
||
if (action === "task") {
|
||
const r = await fileErrorTask(db, scope, id, user.login);
|
||
target = `/t/${r.task.key}`;
|
||
} else if (action === "resolve") await resolveError(db, scope, id, user.login);
|
||
else if (action === "mute" || action === "unmute" || action === "reopen") await setErrorState(db, scope, id, action);
|
||
else throw new PeppaError(m("Неизвестное действие"));
|
||
} catch (e) {
|
||
const t = await getT();
|
||
// PEP-154: the text of an unexpected exception goes to the superadmins, not to the page.
|
||
const why = await platformFailure(db, e, { service: "web", where: "error action" });
|
||
const msg = why ? t.text(why) : t("Не получилось. Попробуйте ещё раз через минуту.");
|
||
const [path, query] = target.split("?");
|
||
const params = new URLSearchParams(query);
|
||
params.set("error", msg.slice(0, 300));
|
||
target = `${path}?${params}`;
|
||
}
|
||
revalidatePath("/", "layout");
|
||
redirect(target);
|
||
}
|