52 lines
2.6 KiB
TypeScript
52 lines
2.6 KiB
TypeScript
// PEP-111: «Ваш продукт» — where the result of the project lives, in words for a product
|
|
// manager: the working version everyone sees (prod), the version to check (staging), or the
|
|
// fact that the project is not deployed as a site at all.
|
|
import { releases, tasks, type Db } from "@peppa/db";
|
|
import { and, desc, eq, inArray, isNull } from "drizzle-orm";
|
|
|
|
export interface ProductVersion {
|
|
url: string;
|
|
at: Date;
|
|
/** Story keys that went into it. */
|
|
stories: string[];
|
|
}
|
|
|
|
export interface ProjectProduct {
|
|
/** The working product, open to everyone. */
|
|
live: ProductVersion | null;
|
|
/** The version to check before showing it to everyone — only when newer than `live`. */
|
|
check: ProductVersion | null;
|
|
/** Stories accepted so far (a product without a site still has results). */
|
|
doneStories: number;
|
|
}
|
|
|
|
export async function projectProduct(db: Db, projectId: number): Promise<ProjectProduct> {
|
|
const latest = async (env: "prod" | "staging") =>
|
|
(await db.select().from(releases).where(and(eq(releases.projectId, projectId), eq(releases.env, env), eq(releases.status, "healthy"))).orderBy(desc(releases.id)).limit(1))[0];
|
|
const [prod, staging] = [await latest("prod"), await latest("staging")];
|
|
const version = (r: NonNullable<typeof prod>): ProductVersion | null => (r.host ? { url: `https://${r.host}`, at: r.updatedAt ?? r.createdAt, stories: r.tasks } : null);
|
|
const done = await db.select({ id: tasks.id }).from(tasks).where(and(eq(tasks.projectId, projectId), eq(tasks.status, "done"), isNull(tasks.archivedAt)));
|
|
return {
|
|
live: prod ? version(prod) : null,
|
|
check: staging && (!prod || (staging.id > prod.id && staging.sha !== prod.sha)) ? version(staging) : null,
|
|
doneStories: done.length,
|
|
};
|
|
}
|
|
|
|
/** Product links of several projects at once (the Home page): projectId → URL of the best version. */
|
|
export async function productUrls(db: Db, projectIds: number[]): Promise<Map<number, { url: string; live: boolean }>> {
|
|
const out = new Map<number, { url: string; live: boolean }>();
|
|
if (!projectIds.length) return out;
|
|
const rows = await db
|
|
.select({ projectId: releases.projectId, env: releases.env, host: releases.host })
|
|
.from(releases)
|
|
.where(and(inArray(releases.projectId, projectIds), inArray(releases.env, ["prod", "staging"]), eq(releases.status, "healthy")))
|
|
.orderBy(desc(releases.id));
|
|
for (const r of rows) {
|
|
if (!r.host) continue;
|
|
const cur = out.get(r.projectId);
|
|
if (!cur || (!cur.live && r.env === "prod")) out.set(r.projectId, { url: `https://${r.host}`, live: r.env === "prod" });
|
|
}
|
|
return out;
|
|
}
|