|
1 | 1 | import { db } from '@sim/db' |
2 | | -import { chat, workflow } from '@sim/db/schema' |
| 2 | +import { chat } from '@sim/db/schema' |
3 | 3 | import { createLogger } from '@sim/logger' |
4 | 4 | import { eq } from 'drizzle-orm' |
5 | 5 | import type { NextRequest } from 'next/server' |
6 | 6 | import { z } from 'zod' |
7 | | -import { recordAudit } from '@/lib/audit/log' |
| 7 | +import { AuditAction, AuditResourceType, recordAudit } from '@/lib/audit/log' |
8 | 8 | import { getSession } from '@/lib/auth' |
9 | 9 | import { isDev } from '@/lib/core/config/feature-flags' |
10 | 10 | import { encryptSecret } from '@/lib/core/security/encryption' |
@@ -104,7 +104,11 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise< |
104 | 104 | try { |
105 | 105 | const validatedData = chatUpdateSchema.parse(body) |
106 | 106 |
|
107 | | - 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) |
108 | 112 |
|
109 | 113 | if (!hasAccess || !existingChatRecord) { |
110 | 114 | return createErrorResponse('Chat not found or access denied', 404) |
@@ -218,19 +222,13 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise< |
218 | 222 |
|
219 | 223 | logger.info(`Chat "${chatId}" updated successfully`) |
220 | 224 |
|
221 | | - const [workflowRecord] = await db |
222 | | - .select({ workspaceId: workflow.workspaceId }) |
223 | | - .from(workflow) |
224 | | - .where(eq(workflow.id, existingChat[0].workflowId)) |
225 | | - .limit(1) |
226 | | - |
227 | 225 | recordAudit({ |
228 | | - workspaceId: workflowRecord?.workspaceId || '', |
| 226 | + workspaceId: chatWorkspaceId || null, |
229 | 227 | actorId: session.user.id, |
230 | 228 | actorName: session.user.name, |
231 | 229 | actorEmail: session.user.email, |
232 | | - action: 'chat.updated', |
233 | | - resourceType: 'chat', |
| 230 | + action: AuditAction.CHAT_UPDATED, |
| 231 | + resourceType: AuditResourceType.CHAT, |
234 | 232 | resourceId: chatId, |
235 | 233 | resourceName: title || existingChat[0].title, |
236 | 234 | description: `Updated chat deployment "${title || existingChat[0].title}"`, |
@@ -272,37 +270,27 @@ export async function DELETE( |
272 | 270 | return createErrorResponse('Unauthorized', 401) |
273 | 271 | } |
274 | 272 |
|
275 | | - 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) |
276 | 278 |
|
277 | 279 | if (!hasAccess) { |
278 | 280 | return createErrorResponse('Chat not found or access denied', 404) |
279 | 281 | } |
280 | 282 |
|
281 | | - const [chatRecord] = await db |
282 | | - .select({ workflowId: chat.workflowId, title: chat.title }) |
283 | | - .from(chat) |
284 | | - .where(eq(chat.id, chatId)) |
285 | | - .limit(1) |
286 | | - |
287 | | - const [workflowRecord] = chatRecord |
288 | | - ? await db |
289 | | - .select({ workspaceId: workflow.workspaceId }) |
290 | | - .from(workflow) |
291 | | - .where(eq(workflow.id, chatRecord.workflowId)) |
292 | | - .limit(1) |
293 | | - : [undefined] |
294 | | - |
295 | 283 | await db.delete(chat).where(eq(chat.id, chatId)) |
296 | 284 |
|
297 | 285 | logger.info(`Chat "${chatId}" deleted successfully`) |
298 | 286 |
|
299 | 287 | recordAudit({ |
300 | | - workspaceId: workflowRecord?.workspaceId || '', |
| 288 | + workspaceId: chatWorkspaceId || null, |
301 | 289 | actorId: session.user.id, |
302 | 290 | actorName: session.user.name, |
303 | 291 | actorEmail: session.user.email, |
304 | | - action: 'chat.deleted', |
305 | | - resourceType: 'chat', |
| 292 | + action: AuditAction.CHAT_DELETED, |
| 293 | + resourceType: AuditResourceType.CHAT, |
306 | 294 | resourceId: chatId, |
307 | 295 | resourceName: chatRecord?.title || chatId, |
308 | 296 | description: `Deleted chat deployment "${chatRecord?.title || chatId}"`, |
|
0 commit comments