Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apps/sim/app/api/chat/[identifier]/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,27 @@ describe('Chat Identifier API Route', () => {
})
})

it('should return 403 for an inactive chat without loading the workflow or writing a log', async () => {
dbChainMockFns.select.mockImplementation(() => ({
from: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
limit: vi.fn().mockReturnValue([{ ...mockChatResult[0], isActive: false }]),
}),
}),
}))
const req = createMockNextRequest('POST', { input: 'x' })

const response = await POST(req, { params: Promise.resolve({ identifier: 'paused-chat' }) })

expect(response.status).toBe(403)
const data = await response.json()
expect(data).toHaveProperty('message', 'This chat is currently unavailable')
expect(dbChainMockFns.select).toHaveBeenCalledTimes(1)
expect(loggingSessionMockFns.mockSafeStart).not.toHaveBeenCalled()
expect(loggingSessionMockFns.mockSafeCompleteWithError).not.toHaveBeenCalled()
expect(mockValidateChatAuth).not.toHaveBeenCalled()
})

it('should return 400 for requests without input', async () => {
const req = createMockNextRequest('POST', {})
const params = Promise.resolve({ identifier: 'test-chat' })
Expand Down
40 changes: 1 addition & 39 deletions apps/sim/app/api/chat/[identifier]/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { db } from '@sim/db'
import { chat, workflow } from '@sim/db/schema'
import { chat } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { generateId } from '@sim/utils/id'
import { and, eq, isNull } from 'drizzle-orm'
Expand Down Expand Up @@ -156,44 +156,6 @@ export const POST = withRouteHandler(

if (!deployment.isActive) {
logger.warn(`[${requestId}] Chat is not active: ${identifier}`)

const [workflowRecord] = await db
.select({ workspaceId: workflow.workspaceId })
.from(workflow)
.where(and(eq(workflow.id, deployment.workflowId), isNull(workflow.archivedAt)))
.limit(1)

const workspaceId = workflowRecord?.workspaceId
if (!workspaceId) {
logger.warn(
`[${requestId}] Cannot log: workflow ${deployment.workflowId} has no workspace`
)
return createErrorResponse('This chat is currently unavailable', 403)
}

const executionId = generateId()
const loggingSession = new LoggingSession(
deployment.workflowId,
executionId,
'chat',
requestId
)

await loggingSession.safeStart({
userId: deployment.userId,
workspaceId,
variables: {},
})

await loggingSession.safeCompleteWithError({
error: {
message: 'This chat is currently unavailable. The chat has been disabled.',
stackTrace: undefined,
},
traceSpans: [],
skipCost: true,
})

return createErrorResponse('This chat is currently unavailable', 403)
}

Expand Down
Loading