88 lines
3.5 KiB
TypeScript
88 lines
3.5 KiB
TypeScript
import { chats, goals, projects, type Db } from "@peppa/db";
|
||
import { and, asc, eq, isNull, or } from "drizzle-orm";
|
||
import { PeppaError } from "./errors.ts";
|
||
|
||
export type Project = typeof projects.$inferSelect;
|
||
export type Goal = typeof goals.$inferSelect;
|
||
|
||
export interface NewProject {
|
||
slug: string;
|
||
key: string;
|
||
name: string;
|
||
description?: string;
|
||
giteaOwner: string;
|
||
giteaRepo?: string;
|
||
workspacePath: string;
|
||
ownerId?: number | null;
|
||
}
|
||
|
||
export async function createProject(db: Db, p: NewProject): Promise<Project> {
|
||
const key = p.key.toUpperCase();
|
||
if (!/^[A-Z][A-Z0-9]{1,9}$/.test(key)) throw new PeppaError(`Invalid project key "${p.key}": 2–10 chars, A–Z/0–9, starts with a letter`);
|
||
if (!/^[a-z0-9][a-z0-9-]*$/.test(p.slug)) throw new PeppaError(`Invalid slug "${p.slug}": lowercase letters, digits, dashes`);
|
||
const [row] = await db
|
||
.insert(projects)
|
||
.values({ ...p, key, giteaRepo: p.giteaRepo ?? p.slug })
|
||
.returning();
|
||
// PEP-62: every project gets its chat (a channel with threads) right away.
|
||
await db.insert(chats).values({ kind: "project", projectId: row!.id }).onConflictDoNothing();
|
||
return row!;
|
||
}
|
||
|
||
/** Live projects (PEP-163: deleted ones only with `withDeleted`). */
|
||
export async function listProjects(db: Db, opts: { withDeleted?: boolean } = {}): Promise<Project[]> {
|
||
return db.select().from(projects).where(opts.withDeleted ? undefined : isNull(projects.deletedAt)).orderBy(asc(projects.key));
|
||
}
|
||
|
||
/** Finds a project by task key prefix ("PEP") or slug ("peppa"). */
|
||
export async function getProject(db: Db, keyOrSlug: string): Promise<Project> {
|
||
const [row] = await db
|
||
.select()
|
||
.from(projects)
|
||
.where(or(eq(projects.key, keyOrSlug.toUpperCase()), eq(projects.slug, keyOrSlug.toLowerCase())));
|
||
if (!row) throw new PeppaError(`Project "${keyOrSlug}" not found`);
|
||
return row;
|
||
}
|
||
|
||
export async function createGoal(db: Db, projectId: number, title: string, description?: string): Promise<Goal> {
|
||
const [row] = await db.insert(goals).values({ projectId, title, description }).returning();
|
||
return row!;
|
||
}
|
||
|
||
export async function listGoals(db: Db, projectId: number, includeClosed = false): Promise<Goal[]> {
|
||
return db
|
||
.select()
|
||
.from(goals)
|
||
.where(includeClosed ? eq(goals.projectId, projectId) : and(eq(goals.projectId, projectId), eq(goals.status, "open")))
|
||
.orderBy(asc(goals.id));
|
||
}
|
||
|
||
export async function getGoal(db: Db, projectId: number, goalId: number): Promise<Goal> {
|
||
const [row] = await db.select().from(goals).where(and(eq(goals.id, goalId), eq(goals.projectId, projectId)));
|
||
if (!row) throw new PeppaError(`Goal #${goalId} not found in this project`);
|
||
return row;
|
||
}
|
||
|
||
export interface GoalPatch {
|
||
title?: string;
|
||
description?: string | null;
|
||
status?: Goal["status"];
|
||
}
|
||
|
||
export async function updateGoal(db: Db, projectId: number, goalId: number, patch: GoalPatch): Promise<Goal> {
|
||
const cur = await getGoal(db, projectId, goalId);
|
||
// PEP-209: nothing to change → no empty SET (drizzle: «No values to set»).
|
||
if (!Object.values(patch).some((v) => v !== undefined)) return cur;
|
||
const [row] = await db.update(goals).set(patch).where(and(eq(goals.id, goalId), eq(goals.projectId, projectId))).returning();
|
||
return row!;
|
||
}
|
||
|
||
// PEP-66: goals are shown as «эпики» — optional labels that group stories. Same table and
|
||
// rows (no migration); these are the names new code should use.
|
||
export type Epic = Goal;
|
||
export type EpicPatch = GoalPatch;
|
||
export const createEpic = createGoal;
|
||
export const listEpics = listGoals;
|
||
export const getEpic = getGoal;
|
||
export const updateEpic = updateGoal;
|