44 lines
2.3 KiB
TypeScript
44 lines
2.3 KiB
TypeScript
// PEP-247: why a story «В работе» has no agent for a lawful reason — the project is on pause, it
|
||
// has no working subscription, or the story's budget is spent. Such a story is not stuck (no
|
||
// platform delay, nothing to the superadmin), but the owner sees the reason on its card.
|
||
import type { Db } from "@peppa/db";
|
||
import { m } from "@peppa/shared/i18n";
|
||
import { getBudgetState } from "./budgets.ts";
|
||
import { projectAgentWarning } from "./onboarding.ts";
|
||
import { isProjectPaused } from "./project-pause.ts";
|
||
|
||
export type TaskHoldKind = "paused" | "subscription" | "budget";
|
||
|
||
export const TASK_HOLD_TEXT: Record<TaskHoldKind, string> = {
|
||
paused: m("Проект на паузе — агент продолжит, когда вы снимете паузу"),
|
||
subscription: m("Агент не запущен: у проекта нет рабочей подписки"),
|
||
budget: m("Агент остановлен: бюджет задачи исчерпан"),
|
||
};
|
||
|
||
export interface TaskHold {
|
||
kind: TaskHoldKind;
|
||
/** The owner's line (a dictionary key: shown through `t.text`). */
|
||
text: string;
|
||
}
|
||
|
||
/** The lawful reason no agent works on the story, or null. */
|
||
export async function taskHoldReason(db: Db, projectId: number, taskId: number, now = new Date()): Promise<TaskHold | null> {
|
||
const hold = (kind: TaskHoldKind): TaskHold => ({ kind, text: TASK_HOLD_TEXT[kind] });
|
||
if (await isProjectPaused(db, projectId)) return hold("paused");
|
||
if (await projectAgentWarning(db, projectId, now)) return hold("subscription");
|
||
if (await getBudgetState(db, taskId)) return hold("budget");
|
||
return null;
|
||
}
|
||
|
||
/** The board: lawful reasons for several stories of one project (the project's checks run once). */
|
||
export async function taskHoldReasons(db: Db, projectId: number, taskIds: number[], now = new Date()): Promise<Map<number, TaskHold>> {
|
||
const out = new Map<number, TaskHold>();
|
||
if (!taskIds.length) return out;
|
||
const project: TaskHoldKind | null = (await isProjectPaused(db, projectId)) ? "paused" : (await projectAgentWarning(db, projectId, now)) ? "subscription" : null;
|
||
for (const id of taskIds) {
|
||
const kind = project ?? ((await getBudgetState(db, id)) ? "budget" : null);
|
||
if (kind) out.set(id, { kind, text: TASK_HOLD_TEXT[kind] });
|
||
}
|
||
return out;
|
||
}
|