11import { db } from '@sim/db'
22import { copilotChats } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
4- import { and , eq , sql } from 'drizzle-orm'
4+ import { and , eq , isNull , sql } from 'drizzle-orm'
55import { type NextRequest , NextResponse } from 'next/server'
66import {
77 addCopilotChatResourceContract ,
@@ -64,7 +64,13 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
6464 const [ chat ] = await db
6565 . select ( { resources : copilotChats . resources } )
6666 . from ( copilotChats )
67- . where ( and ( eq ( copilotChats . id , chatId ) , eq ( copilotChats . userId , userId ) ) )
67+ . where (
68+ and (
69+ eq ( copilotChats . id , chatId ) ,
70+ eq ( copilotChats . userId , userId ) ,
71+ isNull ( copilotChats . deletedAt )
72+ )
73+ )
6874 . limit ( 1 )
6975
7076 if ( ! chat ) {
@@ -91,7 +97,13 @@ export const POST = withRouteHandler(async (req: NextRequest) => {
9197 await db
9298 . update ( copilotChats )
9399 . set ( { resources : sql `${ JSON . stringify ( merged ) } ::jsonb` , updatedAt : new Date ( ) } )
94- . where ( and ( eq ( copilotChats . id , chatId ) , eq ( copilotChats . userId , userId ) ) )
100+ . where (
101+ and (
102+ eq ( copilotChats . id , chatId ) ,
103+ eq ( copilotChats . userId , userId ) ,
104+ isNull ( copilotChats . deletedAt )
105+ )
106+ )
95107
96108 logger . info ( 'Added resource to chat' , { chatId, resource } )
97109
@@ -124,7 +136,13 @@ export const PATCH = withRouteHandler(async (req: NextRequest) => {
124136 const [ chat ] = await db
125137 . select ( { resources : copilotChats . resources } )
126138 . from ( copilotChats )
127- . where ( and ( eq ( copilotChats . id , chatId ) , eq ( copilotChats . userId , userId ) ) )
139+ . where (
140+ and (
141+ eq ( copilotChats . id , chatId ) ,
142+ eq ( copilotChats . userId , userId ) ,
143+ isNull ( copilotChats . deletedAt )
144+ )
145+ )
128146 . limit ( 1 )
129147
130148 if ( ! chat ) {
@@ -142,7 +160,13 @@ export const PATCH = withRouteHandler(async (req: NextRequest) => {
142160 await db
143161 . update ( copilotChats )
144162 . set ( { resources : sql `${ JSON . stringify ( newOrder ) } ::jsonb` , updatedAt : new Date ( ) } )
145- . where ( and ( eq ( copilotChats . id , chatId ) , eq ( copilotChats . userId , userId ) ) )
163+ . where (
164+ and (
165+ eq ( copilotChats . id , chatId ) ,
166+ eq ( copilotChats . userId , userId ) ,
167+ isNull ( copilotChats . deletedAt )
168+ )
169+ )
146170
147171 logger . info ( 'Reordered resources for chat' , { chatId, count : newOrder . length } )
148172
@@ -182,7 +206,13 @@ export const DELETE = withRouteHandler(async (req: NextRequest) => {
182206 ), '[]'::jsonb)` ,
183207 updatedAt : new Date ( ) ,
184208 } )
185- . where ( and ( eq ( copilotChats . id , chatId ) , eq ( copilotChats . userId , userId ) ) )
209+ . where (
210+ and (
211+ eq ( copilotChats . id , chatId ) ,
212+ eq ( copilotChats . userId , userId ) ,
213+ isNull ( copilotChats . deletedAt )
214+ )
215+ )
186216 . returning ( { resources : copilotChats . resources } )
187217
188218 if ( ! updated ) {
0 commit comments