peppa/packages/runner/src/proposals.ts

51 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-197: the owner decides on stories the factory suggests: «Делать» / «Не надо», and for the
// whole list «Делать важные» / «Отклонить всё». The same for the web (chat, board) and Telegram.
import { approveProposal, logEvent, notify, PeppaError, projectForUser, rejectProposal, resolveProposals, type User } from "@peppa/core";
import { tasks, type Db } from "@peppa/db";
import { formatTaskKey } from "@peppa/shared";
import { and, eq, inArray } from "drizzle-orm";
import { projects } from "@peppa/db";
import { m } from "@peppa/shared/i18n";
export type ProposalChoice = "do" | "no" | "important" | "none";
export async function decideProposals(db: Db, user: Pick<User, "id" | "login">, taskIds: number[], choice: ProposalChoice): Promise<number> {
if (!taskIds.length) throw new PeppaError(m("Эта кнопка уже не работает."));
const rows = await db.select({ id: tasks.id, projectId: tasks.projectId, proposed: tasks.proposed }).from(tasks).where(inArray(tasks.id, taskIds));
// Only stories of the owner's own projects (or ones they take part in).
for (const projectId of new Set(rows.map((r) => r.projectId))) await projectForUser(db, user.id, projectId);
const open = rows.filter((r) => r.proposed).map((r) => r.id);
if (!open.length) throw new PeppaError(m("По этим задачам решение уже принято."));
if (choice === "do") for (const id of open) await approveProposal(db, id, user.login);
else if (choice === "no") for (const id of open) await rejectProposal(db, id, user.login);
else await resolveProposals(db, open, choice, user.login);
return open.length;
}
/**
* PEP-209: «Объединить в одну» — several small proposals (usually fixes of one page) become one
* story: the first in board order keeps its title, gets the others' points in its description and
* goes to the backlog; the others are archived with «Объединена в KEY». Only on the owner's button.
*/
export async function mergeProposals(db: Db, user: Pick<User, "id" | "login">, taskIds: number[]): Promise<{ key: string; merged: number }> {
const rows = (await db.select().from(tasks).where(inArray(tasks.id, taskIds))).filter((t) => t.proposed && !t.archivedAt);
if (rows.length < 2) throw new PeppaError(m("Отметьте хотя бы две задачи из «Предложено»."));
if (new Set(rows.map((r) => r.projectId)).size > 1) throw new PeppaError(m("Объединять можно задачи одного проекта."));
await projectForUser(db, user.id, rows[0]!.projectId);
const [project] = await db.select({ key: projects.key }).from(projects).where(eq(projects.id, rows[0]!.projectId));
rows.sort((a, b) => a.position - b.position || a.number - b.number);
const [main, ...rest] = rows as [typeof rows[0], ...typeof rows];
const keyOf = (t: { number: number }) => formatTaskKey(project!.key, t.number);
const points = rest.map((t) => `- ${t.title}${t.description?.trim() ? `\n ${t.description.trim().replace(/\n/g, "\n ")}` : ""}`).join("\n");
const description = `${(main.description ?? "").trim()}\n\n${m("Объединено с задачами {keys}:", { keys: rest.map(keyOf).join(", ") })}\n${points}`.trim();
await db.update(tasks).set({ description, updatedAt: new Date() }).where(eq(tasks.id, main.id));
await approveProposal(db, main.id, user.login);
for (const t of rest) {
await db.update(tasks).set({ proposed: false, archivedAt: new Date() }).where(and(eq(tasks.id, t.id), eq(tasks.proposed, true)));
await logEvent(db, { projectId: t.projectId, taskId: t.id, type: "proposal_merged", payload: { into: keyOf(main), by: user.login } });
await notify(db, { type: "task_updated", taskId: t.id });
}
await logEvent(db, { projectId: main.projectId, taskId: main.id, type: "proposals_merged", payload: { from: rest.map(keyOf), by: user.login } });
return { key: keyOf(main), merged: rest.length };
}