|
1 | | -/** |
2 | | - * @fileoverview Chat session list management. GET returns all chat sessions |
3 | | - * for the authenticated user with message counts. DELETE removes all of the |
4 | | - * user's chat sessions and their associated messages. |
5 | | - */ |
6 | | -import { getDB } from "@/lib/database"; |
7 | | -import { chats, messages } from "@/lib/database/schema"; |
8 | | -import { eq, and, inArray, sql, count } from "drizzle-orm"; |
9 | | -import { requireUserId } from "@/lib/auth/session"; |
10 | | - |
11 | | -export const GET = async (req: Request) => { |
12 | | - try { |
13 | | - const db = getDB(); |
14 | | - // Require authentication |
15 | | - const userId = await requireUserId(); |
16 | | - |
17 | | - // Get user's chats with message counts |
18 | | - const userChats = await db |
19 | | - .select({ |
20 | | - id: chats.id, |
21 | | - title: chats.title, |
22 | | - createdAt: chats.createdAt, |
23 | | - focusMode: chats.focusMode, |
24 | | - userId: chats.userId, |
25 | | - files: chats.files, |
26 | | - messageCount: count(messages.id), |
27 | | - }) |
28 | | - .from(chats) |
29 | | - .leftJoin( |
30 | | - messages, |
31 | | - and(eq(messages.chatId, chats.id), eq(messages.role, "user")), |
32 | | - ) |
33 | | - .where(eq(chats.userId, userId)) |
34 | | - .groupBy( |
35 | | - chats.id, |
36 | | - chats.title, |
37 | | - chats.createdAt, |
38 | | - chats.focusMode, |
39 | | - chats.userId, |
40 | | - chats.files, |
41 | | - ) |
42 | | - .orderBy(sql`${chats.createdAt} DESC`); |
43 | | - |
44 | | - return Response.json({ chats: userChats }, { status: 200 }); |
45 | | - } catch (err) { |
46 | | - // Handle auth errors |
47 | | - if (err instanceof Error && err.message === "Unauthorized") { |
48 | | - return Response.json( |
49 | | - { message: "Authentication required" }, |
50 | | - { status: 401 }, |
51 | | - ); |
52 | | - } |
53 | | - |
54 | | - console.error("Error in getting chats: ", err); |
55 | | - return Response.json( |
56 | | - { message: "An error has occurred." }, |
57 | | - { status: 500 }, |
58 | | - ); |
59 | | - } |
60 | | -}; |
61 | | - |
62 | | -export const DELETE = async (req: Request) => { |
63 | | - try { |
64 | | - const db = getDB(); |
65 | | - // Require authentication |
66 | | - const userId = await requireUserId(); |
67 | | - |
68 | | - // Get all chat IDs for the user |
69 | | - const userChats = await db.query.chats.findMany({ |
70 | | - where: eq(chats.userId, userId), |
71 | | - columns: { id: true }, |
72 | | - }); |
73 | | - |
74 | | - if (userChats.length > 0) { |
75 | | - const chatIds = userChats.map((chat) => chat.id); |
76 | | - |
77 | | - // Delete all messages for these chats first |
78 | | - await db.delete(messages).where(inArray(messages.chatId, chatIds)); |
79 | | - |
80 | | - // Delete all chats for the user |
81 | | - await db.delete(chats).where(eq(chats.userId, userId)); |
82 | | - } |
83 | | - |
84 | | - return Response.json({ message: "All chats deleted" }, { status: 200 }); |
85 | | - } catch (err) { |
86 | | - // Handle auth errors |
87 | | - if (err instanceof Error && err.message === "Unauthorized") { |
88 | | - return Response.json( |
89 | | - { message: "Authentication required" }, |
90 | | - { status: 401 }, |
91 | | - ); |
92 | | - } |
93 | | - |
94 | | - console.error("Error in deleting all chats: ", err); |
95 | | - return Response.json( |
96 | | - { message: "An error has occurred." }, |
97 | | - { status: 500 }, |
98 | | - ); |
99 | | - } |
100 | | -}; |
| 1 | +/** |
| 2 | + * @fileoverview Chat session list management. GET returns all chat sessions |
| 3 | + * for the authenticated user with message counts. DELETE removes all of the |
| 4 | + * user's chat sessions and their associated messages. |
| 5 | + */ |
| 6 | +import { getDB } from "@/lib/database"; |
| 7 | +import { chats, messages } from "@/lib/database/schema"; |
| 8 | +import { eq, and, inArray, sql, count } from "drizzle-orm"; |
| 9 | +import { requireUserId } from "@/lib/auth/session"; |
| 10 | + |
| 11 | +export const GET = async (req: Request) => { |
| 12 | + try { |
| 13 | + const db = getDB(); |
| 14 | + // Require authentication |
| 15 | + const userId = await requireUserId(); |
| 16 | + |
| 17 | + // Get user's chats with message counts |
| 18 | + const userChats = await db |
| 19 | + .select({ |
| 20 | + id: chats.id, |
| 21 | + title: chats.title, |
| 22 | + createdAt: chats.createdAt, |
| 23 | + focusMode: chats.focusMode, |
| 24 | + userId: chats.userId, |
| 25 | + files: chats.files, |
| 26 | + messageCount: count(messages.id), |
| 27 | + }) |
| 28 | + .from(chats) |
| 29 | + .leftJoin( |
| 30 | + messages, |
| 31 | + and(eq(messages.chatId, chats.id), eq(messages.role, "user")), |
| 32 | + ) |
| 33 | + .where(eq(chats.userId, userId)) |
| 34 | + .groupBy( |
| 35 | + chats.id, |
| 36 | + chats.title, |
| 37 | + chats.createdAt, |
| 38 | + chats.focusMode, |
| 39 | + chats.userId, |
| 40 | + chats.files, |
| 41 | + ) |
| 42 | + .orderBy(sql`${chats.createdAt} DESC`); |
| 43 | + |
| 44 | + return Response.json({ chats: userChats }, { status: 200 }); |
| 45 | + } catch (err) { |
| 46 | + // Handle auth errors |
| 47 | + if (err instanceof Error && err.message === "Unauthorized") { |
| 48 | + return Response.json( |
| 49 | + { message: "Authentication required" }, |
| 50 | + { status: 401 }, |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + console.error("Error in getting chats: ", err); |
| 55 | + return Response.json( |
| 56 | + { message: "An error has occurred." }, |
| 57 | + { status: 500 }, |
| 58 | + ); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +export const DELETE = async (req: Request) => { |
| 63 | + try { |
| 64 | + const db = getDB(); |
| 65 | + // Require authentication |
| 66 | + const userId = await requireUserId(); |
| 67 | + |
| 68 | + // Get all chat IDs for the user |
| 69 | + const userChats = await db.query.chats.findMany({ |
| 70 | + where: eq(chats.userId, userId), |
| 71 | + columns: { id: true }, |
| 72 | + }); |
| 73 | + |
| 74 | + if (userChats.length > 0) { |
| 75 | + const chatIds = userChats.map((chat) => chat.id); |
| 76 | + |
| 77 | + // Delete all messages for these chats first |
| 78 | + await db.delete(messages).where(inArray(messages.chatId, chatIds)); |
| 79 | + |
| 80 | + // Delete all chats for the user |
| 81 | + await db.delete(chats).where(eq(chats.userId, userId)); |
| 82 | + } |
| 83 | + |
| 84 | + return Response.json({ message: "All chats deleted" }, { status: 200 }); |
| 85 | + } catch (err) { |
| 86 | + // Handle auth errors |
| 87 | + if (err instanceof Error && err.message === "Unauthorized") { |
| 88 | + return Response.json( |
| 89 | + { message: "Authentication required" }, |
| 90 | + { status: 401 }, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + console.error("Error in deleting all chats: ", err); |
| 95 | + return Response.json( |
| 96 | + { message: "An error has occurred." }, |
| 97 | + { status: 500 }, |
| 98 | + ); |
| 99 | + } |
| 100 | +}; |
0 commit comments