Main's new wording (PEP-123 «Продакт» words) kept and wrapped in t()/m();
wording(mode, locale) gives English words for {task}/{runner} placeholders;
glossary check (pnpm lint) passes; English for all new keys (en/merge-*.ts).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
5.5 KiB
TypeScript
124 lines
5.5 KiB
TypeScript
"use server";
|
||
// Board order and archive (PEP-34), story dependencies and the epic planner (PEP-42).
|
||
// Thin wrappers over core/runner with the owner/member check, like actions.ts.
|
||
|
||
import { getEpic, PeppaError, projectForUser, resolveTask, setDependencies, type TaskRef } from "@peppa/core";
|
||
import { applyPlan, archiveTask, discardPlanDraft, parseDepsInput, planEpic, reorderBoardTask, type PlanDep, type PlannedStory } from "@peppa/runner";
|
||
import { revalidatePath } from "next/cache";
|
||
import { redirect } from "next/navigation";
|
||
import { requireUser } from "@/lib/auth";
|
||
import { db } from "@/lib/db";
|
||
import { m } from "@peppa/shared/i18n";
|
||
|
||
const str = (f: FormData, k: string) => String(f.get(k) ?? "").trim();
|
||
|
||
async function attempt(f: FormData, fn: () => Promise<string | void>): Promise<never> {
|
||
let target = str(f, "back") || "/";
|
||
try {
|
||
target = (await fn()) || target;
|
||
} catch (e) {
|
||
const msg = e instanceof PeppaError ? e.message : m("Ошибка: {message}", { message: (e as Error).message });
|
||
const [path, query] = target.split("?");
|
||
const params = new URLSearchParams(query);
|
||
params.set("error", msg.slice(0, 300));
|
||
target = `${path}?${params}`;
|
||
}
|
||
revalidatePath("/", "layout");
|
||
redirect(target);
|
||
}
|
||
|
||
async function ownedTask(userId: number, key: string): Promise<TaskRef> {
|
||
const ref = await resolveTask(db, key);
|
||
await projectForUser(db, userId, ref.project.id);
|
||
return ref;
|
||
}
|
||
|
||
/** Drag within a column (before/after another card) or the card menu (top/bottom). */
|
||
export async function reorderTaskCard(f: FormData) {
|
||
const user = await requireUser();
|
||
await attempt(f, async () => {
|
||
const ref = await ownedTask(user.id, str(f, "taskKey"));
|
||
const to = str(f, "to");
|
||
if (to === "top" || to === "bottom") await reorderBoardTask(db, ref, { to });
|
||
else if (to === "before" || to === "after") await reorderBoardTask(db, ref, { to, other: await ownedTask(user.id, str(f, "other")) });
|
||
else throw new PeppaError(m("Неизвестное место"));
|
||
});
|
||
}
|
||
|
||
/** Called from the board without a page reload (drag and drop); errors are returned. */
|
||
export async function reorderTaskInline(taskKey: string, to: "top" | "bottom" | "before" | "after", other?: string): Promise<string | null> {
|
||
const user = await requireUser();
|
||
try {
|
||
const ref = await ownedTask(user.id, taskKey);
|
||
if (to === "before" || to === "after") await reorderBoardTask(db, ref, { to, other: await ownedTask(user.id, other ?? "") });
|
||
else await reorderBoardTask(db, ref, { to });
|
||
revalidatePath("/", "layout");
|
||
return null;
|
||
} catch (e) {
|
||
return e instanceof PeppaError ? e.message : (e as Error).message;
|
||
}
|
||
}
|
||
|
||
export async function archiveTaskAction(f: FormData) {
|
||
const user = await requireUser();
|
||
await attempt(f, async () => {
|
||
const ref = await ownedTask(user.id, str(f, "taskKey"));
|
||
await archiveTask(db, ref, str(f, "archived") !== "0");
|
||
});
|
||
}
|
||
|
||
export async function setDependenciesAction(f: FormData) {
|
||
const user = await requireUser();
|
||
await attempt(f, async () => {
|
||
const ref = await ownedTask(user.id, str(f, "taskKey"));
|
||
const keys = str(f, "blockedBy").split(/[\s,;]+/).filter(Boolean);
|
||
// "remove" drops one key from the current list; otherwise the field adds to it.
|
||
const current = (f.getAll("current") as string[]).map(String);
|
||
const remove = str(f, "remove");
|
||
await setDependencies(db, ref, remove ? current.filter((k) => k !== remove) : [...current, ...keys]);
|
||
});
|
||
}
|
||
|
||
export async function planEpicAction(f: FormData) {
|
||
const user = await requireUser();
|
||
await attempt(f, async () => {
|
||
const p = await projectForUser(db, user.id, Number(str(f, "projectId")));
|
||
const epic = await getEpic(db, p.id, Number(str(f, "epicId")));
|
||
const r = await planEpic(db, p, epic.id, { wishes: str(f, "wishes") || null, by: user.login });
|
||
if (!r.ok) throw new PeppaError(m("Планировщик: {error}", { error: r.error }));
|
||
});
|
||
}
|
||
|
||
export async function applyPlanAction(f: FormData) {
|
||
const user = await requireUser();
|
||
await attempt(f, async () => {
|
||
const p = await projectForUser(db, user.id, Number(str(f, "projectId")));
|
||
const epicId = Number(str(f, "epicId"));
|
||
const n = Number(str(f, "count"));
|
||
// Plan numbers in the form refer to the full draft; renumber them for the kept stories.
|
||
const kept: number[] = [];
|
||
for (let i = 1; i <= n; i++) if (f.get(`keep_${i}`)) kept.push(i);
|
||
if (!kept.length) throw new PeppaError(m("Не выбрано ни одной задачи. Отметьте хотя бы одну."));
|
||
const renum = new Map(kept.map((orig, i) => [orig, i + 1]));
|
||
const stories: PlannedStory[] = kept.map((i) => {
|
||
const deps = parseDepsInput(str(f, `deps_${i}`)).map((d): PlanDep => {
|
||
if (typeof d !== "number") return d;
|
||
if (!renum.has(d)) throw new PeppaError(m("Задача {i} зависит от №{d}, которую вы сняли. Уберите зависимость или оставьте задачу.", { i, d }));
|
||
return renum.get(d)!;
|
||
});
|
||
return { title: str(f, `title_${i}`), description: str(f, `description_${i}`), dependsOn: deps };
|
||
});
|
||
await applyPlan(db, p, epicId, stories);
|
||
return `/p/${p.key}?epic=${epicId}`;
|
||
});
|
||
}
|
||
|
||
export async function discardPlanAction(f: FormData) {
|
||
const user = await requireUser();
|
||
await attempt(f, async () => {
|
||
const p = await projectForUser(db, user.id, Number(str(f, "projectId")));
|
||
const epic = await getEpic(db, p.id, Number(str(f, "epicId")));
|
||
await discardPlanDraft(db, epic.id);
|
||
});
|
||
}
|