peppa/packages/runner/src/proposals.ts

22 lines
1.5 KiB
TypeScript

// 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, PeppaError, projectForUser, rejectProposal, resolveProposals, type User } from "@peppa/core";
import { tasks, type Db } from "@peppa/db";
import { inArray } from "drizzle-orm";
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;
}