@@ -4,6 +4,7 @@ import { createLogger } from '@sim/logger'
44import { eq } from 'drizzle-orm'
55import type { NextRequest } from 'next/server'
66import { z } from 'zod'
7+ import { AuditAction , AuditResourceType , recordAudit } from '@/lib/audit/log'
78import { getSession } from '@/lib/auth'
89import { isDev } from '@/lib/core/config/feature-flags'
910import { encryptSecret } from '@/lib/core/security/encryption'
@@ -103,7 +104,11 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
103104 try {
104105 const validatedData = chatUpdateSchema . parse ( body )
105106
106- const { hasAccess, chat : existingChatRecord } = await checkChatAccess ( chatId , session . user . id )
107+ const {
108+ hasAccess,
109+ chat : existingChatRecord ,
110+ workspaceId : chatWorkspaceId ,
111+ } = await checkChatAccess ( chatId , session . user . id )
107112
108113 if ( ! hasAccess || ! existingChatRecord ) {
109114 return createErrorResponse ( 'Chat not found or access denied' , 404 )
@@ -217,6 +222,19 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
217222
218223 logger . info ( `Chat "${ chatId } " updated successfully` )
219224
225+ recordAudit ( {
226+ workspaceId : chatWorkspaceId || null ,
227+ actorId : session . user . id ,
228+ actorName : session . user . name ,
229+ actorEmail : session . user . email ,
230+ action : AuditAction . CHAT_UPDATED ,
231+ resourceType : AuditResourceType . CHAT ,
232+ resourceId : chatId ,
233+ resourceName : title || existingChatRecord . title ,
234+ description : `Updated chat deployment "${ title || existingChatRecord . title } "` ,
235+ request,
236+ } )
237+
220238 return createSuccessResponse ( {
221239 id : chatId ,
222240 chatUrl,
@@ -252,7 +270,11 @@ export async function DELETE(
252270 return createErrorResponse ( 'Unauthorized' , 401 )
253271 }
254272
255- const { hasAccess } = await checkChatAccess ( chatId , session . user . id )
273+ const {
274+ hasAccess,
275+ chat : chatRecord ,
276+ workspaceId : chatWorkspaceId ,
277+ } = await checkChatAccess ( chatId , session . user . id )
256278
257279 if ( ! hasAccess ) {
258280 return createErrorResponse ( 'Chat not found or access denied' , 404 )
@@ -262,6 +284,19 @@ export async function DELETE(
262284
263285 logger . info ( `Chat "${ chatId } " deleted successfully` )
264286
287+ recordAudit ( {
288+ workspaceId : chatWorkspaceId || null ,
289+ actorId : session . user . id ,
290+ actorName : session . user . name ,
291+ actorEmail : session . user . email ,
292+ action : AuditAction . CHAT_DELETED ,
293+ resourceType : AuditResourceType . CHAT ,
294+ resourceId : chatId ,
295+ resourceName : chatRecord ?. title || chatId ,
296+ description : `Deleted chat deployment "${ chatRecord ?. title || chatId } "` ,
297+ request : _request ,
298+ } )
299+
265300 return createSuccessResponse ( {
266301 message : 'Chat deployment deleted successfully' ,
267302 } )
0 commit comments