kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { asc, eq } from "drizzle-orm";
2import { HTTPException } from "hono/http-exception";
3import db from "../database";
4import { columnTable } from "../database/schema";
5
6export const VALID_PRIORITIES = [
7 "no-priority",
8 "low",
9 "medium",
10 "high",
11 "urgent",
12] as const;
13
14export const VIRTUAL_STATUSES = ["planned", "archived"] as const;
15
16export function assertValidPriority(priority: string): void {
17 if (!(VALID_PRIORITIES as readonly string[]).includes(priority)) {
18 throw new HTTPException(400, {
19 message: `Invalid priority "${priority}". Valid values: ${VALID_PRIORITIES.join(", ")}`,
20 });
21 }
22}
23
24export async function getValidTaskStatuses(
25 projectId: string,
26): Promise<string[]> {
27 const columns = await db
28 .select({ slug: columnTable.slug })
29 .from(columnTable)
30 .where(eq(columnTable.projectId, projectId))
31 .orderBy(asc(columnTable.position));
32
33 return [...columns.map((c) => c.slug), ...VIRTUAL_STATUSES];
34}
35
36export async function assertValidTaskStatus(
37 status: string,
38 projectId: string,
39): Promise<void> {
40 const validStatuses = await getValidTaskStatuses(projectId);
41
42 if (!validStatuses.includes(status)) {
43 throw new HTTPException(400, {
44 message: `Invalid status "${status}". Valid statuses for this project: ${validStatuses.join(", ")}`,
45 });
46 }
47}
48
49export function coerceStatus(
50 status: string,
51 validStatuses: string[],
52): { status: string; warning?: string } {
53 if (validStatuses.includes(status)) {
54 return { status };
55 }
56 return {
57 status: "planned",
58 warning: `Unknown status "${status}" mapped to "planned"`,
59 };
60}
61
62export function coercePriority(priority: string): {
63 priority: string;
64 warning?: string;
65} {
66 if ((VALID_PRIORITIES as readonly string[]).includes(priority)) {
67 return { priority };
68 }
69 return {
70 priority: "no-priority",
71 warning: `Unknown priority "${priority}" mapped to "no-priority"`,
72 };
73}