Skip to content

Commit 63e36de

Browse files
committed
fix(chat): return 403 for inactive chats without writing an execution log
1 parent c448b87 commit 63e36de

2 files changed

Lines changed: 22 additions & 39 deletions

File tree

‎apps/sim/app/api/chat/[identifier]/route.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,27 @@ describe('Chat Identifier API Route', () => {
453453
})
454454
})
455455

456+
it('should return 403 for an inactive chat without loading the workflow or writing a log', async () => {
457+
dbChainMockFns.select.mockImplementation(() => ({
458+
from: vi.fn().mockReturnValue({
459+
where: vi.fn().mockReturnValue({
460+
limit: vi.fn().mockReturnValue([{ ...mockChatResult[0], isActive: false }]),
461+
}),
462+
}),
463+
}))
464+
const req = createMockNextRequest('POST', { input: 'x' })
465+
466+
const response = await POST(req, { params: Promise.resolve({ identifier: 'paused-chat' }) })
467+
468+
expect(response.status).toBe(403)
469+
const data = await response.json()
470+
expect(data).toHaveProperty('message', 'This chat is currently unavailable')
471+
expect(dbChainMockFns.select).toHaveBeenCalledTimes(1)
472+
expect(loggingSessionMockFns.mockSafeStart).not.toHaveBeenCalled()
473+
expect(loggingSessionMockFns.mockSafeCompleteWithError).not.toHaveBeenCalled()
474+
expect(mockValidateChatAuth).not.toHaveBeenCalled()
475+
})
476+
456477
it('should return 400 for requests without input', async () => {
457478
const req = createMockNextRequest('POST', {})
458479
const params = Promise.resolve({ identifier: 'test-chat' })

‎apps/sim/app/api/chat/[identifier]/route.ts‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { db } from '@sim/db'
2-
import { chat, workflow } from '@sim/db/schema'
2+
import { chat } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
44
import { generateId } from '@sim/utils/id'
55
import { and, eq, isNull } from 'drizzle-orm'
@@ -156,44 +156,6 @@ export const POST = withRouteHandler(
156156

157157
if (!deployment.isActive) {
158158
logger.warn(`[${requestId}] Chat is not active: ${identifier}`)
159-
160-
const [workflowRecord] = await db
161-
.select({ workspaceId: workflow.workspaceId })
162-
.from(workflow)
163-
.where(and(eq(workflow.id, deployment.workflowId), isNull(workflow.archivedAt)))
164-
.limit(1)
165-
166-
const workspaceId = workflowRecord?.workspaceId
167-
if (!workspaceId) {
168-
logger.warn(
169-
`[${requestId}] Cannot log: workflow ${deployment.workflowId} has no workspace`
170-
)
171-
return createErrorResponse('This chat is currently unavailable', 403)
172-
}
173-
174-
const executionId = generateId()
175-
const loggingSession = new LoggingSession(
176-
deployment.workflowId,
177-
executionId,
178-
'chat',
179-
requestId
180-
)
181-
182-
await loggingSession.safeStart({
183-
userId: deployment.userId,
184-
workspaceId,
185-
variables: {},
186-
})
187-
188-
await loggingSession.safeCompleteWithError({
189-
error: {
190-
message: 'This chat is currently unavailable. The chat has been disabled.',
191-
stackTrace: undefined,
192-
},
193-
traceSpans: [],
194-
skipCost: true,
195-
})
196-
197159
return createErrorResponse('This chat is currently unavailable', 403)
198160
}
199161

0 commit comments

Comments
 (0)