feat: add delete all chats button to sidebar (#1267)

This commit is contained in:
josh 2025-10-09 18:51:17 +01:00 committed by GitHub
parent 9987b25964
commit ab402620df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 160 additions and 45 deletions

View file

@ -123,6 +123,37 @@ export async function deleteChatById({ id }: { id: string }) {
}
}
export async function deleteAllChatsByUserId({ userId }: { userId: string }) {
try {
const userChats = await db
.select({ id: chat.id })
.from(chat)
.where(eq(chat.userId, userId));
if (userChats.length === 0) {
return { deletedCount: 0 };
}
const chatIds = userChats.map(c => c.id);
await db.delete(vote).where(inArray(vote.chatId, chatIds));
await db.delete(message).where(inArray(message.chatId, chatIds));
await db.delete(stream).where(inArray(stream.chatId, chatIds));
const deletedChats = await db
.delete(chat)
.where(eq(chat.userId, userId))
.returning();
return { deletedCount: deletedChats.length };
} catch (_error) {
throw new ChatSDKError(
"bad_request:database",
"Failed to delete all chats by user id"
);
}
}
export async function getChatsByUserId({
id,
limit,