63 lines
3.4 KiB
TypeScript
63 lines
3.4 KiB
TypeScript
// PEP-198: finished stories reach the owner in the analyst's voice — at once when the owner is
|
||
// waiting for the story, otherwise in one message at the end of the working day. A story is
|
||
// awaited when a person asked for it (origin human), commented on it or said «Делать» to the
|
||
// factory's proposal. The «готово» card of a story nobody waits for is still on the site
|
||
// («Ждут „Принять“»); only Telegram and push hold it for the evening (`readyDigestTick`).
|
||
import { events, questions, tasks, type Db } from "@peppa/db";
|
||
import { and, eq, inArray, isNull } from "drizzle-orm";
|
||
import { m as tr, asIs } from "@peppa/shared/i18n";
|
||
import { formatTaskKey } from "@peppa/shared";
|
||
import { questionType, type MessageType } from "./questions.ts";
|
||
import { getSetting, setSetting } from "./settings.ts";
|
||
import { COMMENT_EVENT } from "./tasks.ts";
|
||
import { splitParent } from "./task-decisions.ts";
|
||
|
||
/** Does a person wait for this story (its «готово» goes at once)? A part of a split — when its first story is awaited. */
|
||
export async function taskAwaited(db: Db, taskId: number, depth = 0): Promise<boolean> {
|
||
const [t] = await db.select({ origin: tasks.origin }).from(tasks).where(eq(tasks.id, taskId));
|
||
if (!t || t.origin === "human") return true;
|
||
const parent = depth < 3 ? await splitParent(db, taskId) : null;
|
||
if (parent && (await taskAwaited(db, parent, depth + 1))) return true;
|
||
const [e] = await db
|
||
.select({ id: events.id })
|
||
.from(events)
|
||
.where(and(eq(events.taskId, taskId), inArray(events.type, [COMMENT_EVENT, "proposal_approved"])))
|
||
.limit(1);
|
||
return !!e;
|
||
}
|
||
|
||
/** The bot's question: is this message held for the evening digest (not sent one by one)? */
|
||
export async function heldForDigest(db: Db, q: { taskId: number | null; kind: string; msgType?: MessageType | null; options?: string[] | null }, taskStatus: string | null): Promise<boolean> {
|
||
if (!q.taskId || questionType(q, taskStatus) !== "done") return false;
|
||
return !(await taskAwaited(db, q.taskId));
|
||
}
|
||
|
||
const sentKey = (projectId: number) => `ready.digest:${projectId}`;
|
||
export const readyDigestDay = (db: Db, projectId: number) => getSetting<string>(db, sentKey(projectId));
|
||
export const markReadyDigest = (db: Db, projectId: number, day: string) => setSetting(db, sentKey(projectId), day);
|
||
|
||
export interface ReadyItem {
|
||
key: string;
|
||
title: string;
|
||
}
|
||
|
||
/** Stories of a project in review with an open «готово» nobody waits for. */
|
||
export async function heldReadyStories(db: Db, project: { id: number; key: string }): Promise<ReadyItem[]> {
|
||
const rows = await db
|
||
.select({ q: questions, t: tasks })
|
||
.from(questions)
|
||
.innerJoin(tasks, eq(questions.taskId, tasks.id))
|
||
.where(and(eq(questions.projectId, project.id), isNull(questions.answeredAt), eq(tasks.status, "review"), isNull(tasks.archivedAt)));
|
||
const out = new Map<number, ReadyItem>();
|
||
for (const { q, t } of rows) if (!out.has(t.id) && (await heldForDigest(db, q, t.status))) out.set(t.id, { key: formatTaskKey(project.key, t.number), title: t.title });
|
||
return [...out.values()];
|
||
}
|
||
|
||
export function readyDigestText(items: ReadyItem[], url: string): string {
|
||
return [
|
||
tr("За день готовы задачи: {n}. Примите их или напишите, что поправить.", { n: items.length }),
|
||
items.map((i) => `• ${i.key} «${asIs(i.title)}»`).join("\n"),
|
||
tr("Где посмотреть: {url}", { url }),
|
||
].join("\n");
|
||
}
|