feat: add delete all chats button to sidebar (#1267)
This commit is contained in:
parent
9987b25964
commit
ab402620df
3 changed files with 160 additions and 45 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue