Open History to see the last commit
1import { eq } from "drizzle-orm";
2import z from "zod";
3import { db } from "@/db";
4import { type TodoItemRecord, todoItemsTable } from "@/db/schema";
5import { type APIRouteHandler, json } from "./helpers";
6
7const todoItemCreateData = z.object({
8 id: z.string().min(1),
9 boardId: z.string(),
10 projectId: z.string(),
11 priority: z.number().min(0).max(3).int().optional().nullable(),
12 title: z.string(),
13 description: z.string().optional().nullable(),
14 position: z.string(),
15});
16
17export type TodoItemCreateDataType = z.infer<typeof todoItemCreateData>;
18
19const todoItemUpdateData = z.object({
20 id: z.string(),
21 boardId: z.string().optional(),
22 priority: z.number().nullable().optional(),
23 title: z.string().optional(),
24 description: z.string().nullable().optional(),
25 position: z.string().optional(),
26});
27
28export default {
29 GET: async ({ request }) => {
30 const url = new URL(request.url);
31
32 // Currently we only support filtering by projectId
33 const projectId = url.searchParams.get("projectId");
34
35 if (projectId) {
36 const results = await db
37 .select()
38 .from(todoItemsTable)
39 .where(eq(todoItemsTable.projectId, projectId));
40 return json(results);
41 } else {
42 const results = await db.select().from(todoItemsTable);
43 return json(results);
44 }
45 },
46 POST: async ({ request }) => {
47 // Create new todo item
48 let newTodoItemData: z.infer<typeof todoItemCreateData>;
49 // biome-ignore lint/suspicious/noExplicitAny: it can be any here
50 let bodyObj: any;
51
52 try {
53 bodyObj = await request.json();
54 } catch (e) {
55 console.error("Error parsing JSON body:", e);
56 return new Response(`Invalid JSON body`, { status: 400 });
57 }
58
59 try {
60 const todoItemData = todoItemCreateData.parse(bodyObj);
61 newTodoItemData = todoItemData;
62 } catch (e) {
63 console.error("Validation error:", e);
64 if (e instanceof z.ZodError) {
65 return new Response(`Invalid request data: ${z.prettifyError(e)}`, {
66 status: 400,
67 });
68 }
69 console.error("Bad format", e);
70 return new Response(`Validation error`, { status: 400 });
71 }
72
73 const { description } = newTodoItemData;
74
75 if (!description) {
76 return new Response(`Description is required`, { status: 400 });
77 }
78
79 await db.insert(todoItemsTable).values({
80 ...newTodoItemData,
81 description,
82 createdAt: new Date(),
83 priority: 0,
84 } satisfies TodoItemRecord);
85
86 return json(
87 { created: "ok" },
88 {
89 status: 201,
90 },
91 );
92 },
93 PATCH: async ({ request }) => {
94 let updatedData: z.infer<typeof todoItemUpdateData>;
95
96 // biome-ignore lint/suspicious/noExplicitAny: it can be any here
97 let bodyObj: any;
98
99 try {
100 bodyObj = await request.json();
101 } catch (e) {
102 console.error("Error parsing JSON body:", e);
103 return new Response(`Invalid JSON body`, { status: 400 });
104 }
105
106 try {
107 updatedData = todoItemUpdateData.parse(bodyObj);
108 } catch (e) {
109 console.error("Validation error:", e);
110 if (e instanceof z.ZodError) {
111 return new Response(`Invalid request data: ${z.prettifyError(e)}`, {
112 status: 400,
113 });
114 }
115 console.error("Bad format", e);
116 return new Response(`Validation error`, { status: 400 });
117 }
118
119 if (
120 Object.keys(updatedData).length === 1 // there aren't keys other than id
121 ) {
122 return new Response(`No columns to update`, { status: 400 });
123 }
124
125 const [updatedTodoItemData] = await db
126 .update(todoItemsTable)
127 .set(updatedData)
128 .where(eq(todoItemsTable.id, updatedData.id))
129 .returning();
130
131 if (!updatedTodoItemData) {
132 return new Response(`Todo item not found`, { status: 404 });
133 }
134
135 return json(updatedTodoItemData);
136 },
137} satisfies APIRouteHandler;