// PM-side moves on the task board and review acceptance (web and Telegram use these). import { answerQuestion, getQuestion, listSessions, PeppaError, resolveTask, updateTask, type Question, type TaskRef } from "@peppa/core"; import { tasks, projects, type Db } from "@peppa/db"; import { formatTaskKey } from "@peppa/shared"; import { eq } from "drizzle-orm"; import { startSession, stopSession, type StartOptions } from "./sessions.ts"; export type BoardColumn = "backlog" | "in_progress" | "review" | "done"; /** Which board column a task status belongs to. */ export function columnOf(status: string): BoardColumn { if (status === "backlog") return "backlog"; if (status === "review") return "review"; if (status === "done") return "done"; return "in_progress"; // proposing, awaiting_approval, in_progress, blocked } async function activeSessions(db: Db, taskId: number) { return (await listSessions(db, { active: true, taskId })); } /** * Moves a task card. Into "in progress": status in_progress and an agent session is * started unless one is running. Into "backlog"/"done": active sessions are stopped. */ export async function moveTask(db: Db, ref: TaskRef, to: BoardColumn, start: StartOptions = {}): Promise<{ started?: number; stopped: number }> { const from = columnOf(ref.task.status); let stopped = 0; if (to === "in_progress") { if (from !== "in_progress") await updateTask(db, ref, { status: "in_progress" }); if ((await activeSessions(db, ref.task.id)).length === 0) { const s = await startSession(db, ref.key, start); return { started: s.id, stopped }; } return { stopped }; } if (to === "backlog" || to === "done") { for (const s of await activeSessions(db, ref.task.id)) { await stopSession(db, s.id); stopped++; } } if (from !== to || ref.task.status !== to) await updateTask(db, ref, { status: to }); return { stopped }; } /** "Accept" on a ready-for-review message: task done, session stopped, question closed. */ export async function acceptTask(db: Db, questionId: number, via: NonNullable): Promise { const q = await getQuestion(db, questionId); if (!q.taskId) throw new PeppaError("Вопрос не привязан к задаче"); if (q.answeredAt) throw new PeppaError(`Уже отвечено (${q.answeredVia}): ${q.answer}`); const [t] = await db .select({ key: projects.key, number: tasks.number }) .from(tasks) .innerJoin(projects, eq(tasks.projectId, projects.id)) .where(eq(tasks.id, q.taskId)); const ref = await resolveTask(db, formatTaskKey(t!.key, t!.number)); if (ref.task.status !== "review") throw new PeppaError(`${ref.key} не в ревью (сейчас ${ref.task.status})`); await answerQuestion(db, q.id, "✅ Принято", via); await moveTask(db, ref, "done"); return ref; } /** True when an "attention" question is the ready-for-review message of its task. */ export async function isReviewQuestion(db: Db, q: Question): Promise { if (q.kind !== "attention" || !q.taskId || q.answeredAt) return false; const [t] = await db.select({ status: tasks.status }).from(tasks).where(eq(tasks.id, q.taskId)); return t?.status === "review"; }