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
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ const otpVerifySchema = z.object({
// Send OTP endpoint
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ subdomain: string }> }
{ params }: { params: Promise<{ identifier: string }> }
) {
const { subdomain } = await params
const { identifier } = await params
const requestId = generateRequestId()

try {
logger.debug(`[${requestId}] Processing OTP request for subdomain: ${subdomain}`)
logger.debug(`[${requestId}] Processing OTP request for identifier: ${identifier}`)

// Parse request body
let body
Expand All @@ -136,11 +136,11 @@ export async function POST(
title: chat.title,
})
.from(chat)
.where(eq(chat.subdomain, subdomain))
.where(eq(chat.identifier, identifier))
.limit(1)

if (deploymentResult.length === 0) {
logger.warn(`[${requestId}] Chat not found for subdomain: ${subdomain}`)
logger.warn(`[${requestId}] Chat not found for identifier: ${identifier}`)
return addCorsHeaders(createErrorResponse('Chat not found', 404), request)
}

Expand Down Expand Up @@ -227,13 +227,13 @@ export async function POST(
// Verify OTP endpoint
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ subdomain: string }> }
{ params }: { params: Promise<{ identifier: string }> }
) {
const { subdomain } = await params
const { identifier } = await params
const requestId = generateRequestId()

try {
logger.debug(`[${requestId}] Verifying OTP for subdomain: ${subdomain}`)
logger.debug(`[${requestId}] Verifying OTP for identifier: ${identifier}`)

// Parse request body
let body
Expand All @@ -248,11 +248,11 @@ export async function PUT(
authType: chat.authType,
})
.from(chat)
.where(eq(chat.subdomain, subdomain))
.where(eq(chat.identifier, identifier))
.limit(1)

if (deploymentResult.length === 0) {
logger.warn(`[${requestId}] Chat not found for subdomain: ${subdomain}`)
logger.warn(`[${requestId}] Chat not found for identifier: ${identifier}`)
return addCorsHeaders(createErrorResponse('Chat not found', 404), request)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Tests for chat subdomain API route
* Tests for chat identifier API route
*
* @vitest-environment node
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockRequest } from '@/app/api/__test-utils__/utils'

describe('Chat Subdomain API Route', () => {
describe('Chat Identifier API Route', () => {
const createMockStream = () => {
return new ReadableStream({
start(controller) {
Expand Down Expand Up @@ -134,11 +134,11 @@ describe('Chat Subdomain API Route', () => {
})

describe('GET endpoint', () => {
it('should return chat info for a valid subdomain', async () => {
it('should return chat info for a valid identifier', async () => {
const req = createMockRequest('GET')
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { GET } = await import('@/app/api/chat/[subdomain]/route')
const { GET } = await import('@/app/api/chat/[identifier]/route')

const response = await GET(req, { params })

Expand All @@ -152,7 +152,7 @@ describe('Chat Subdomain API Route', () => {
expect(data.customizations).toHaveProperty('welcomeMessage', 'Welcome to the test chat')
})

it('should return 404 for non-existent subdomain', async () => {
it('should return 404 for non-existent identifier', async () => {
vi.doMock('@sim/db', () => {
const mockLimit = vi.fn().mockReturnValue([])
const mockWhere = vi.fn().mockReturnValue({ limit: mockLimit })
Expand All @@ -167,9 +167,9 @@ describe('Chat Subdomain API Route', () => {
})

const req = createMockRequest('GET')
const params = Promise.resolve({ subdomain: 'nonexistent' })
const params = Promise.resolve({ identifier: 'nonexistent' })

const { GET } = await import('@/app/api/chat/[subdomain]/route')
const { GET } = await import('@/app/api/chat/[identifier]/route')

const response = await GET(req, { params })

Expand Down Expand Up @@ -201,9 +201,9 @@ describe('Chat Subdomain API Route', () => {
})

const req = createMockRequest('GET')
const params = Promise.resolve({ subdomain: 'inactive-chat' })
const params = Promise.resolve({ identifier: 'inactive-chat' })

const { GET } = await import('@/app/api/chat/[subdomain]/route')
const { GET } = await import('@/app/api/chat/[identifier]/route')

const response = await GET(req, { params })

Expand All @@ -222,9 +222,9 @@ describe('Chat Subdomain API Route', () => {
}))

const req = createMockRequest('GET')
const params = Promise.resolve({ subdomain: 'password-protected-chat' })
const params = Promise.resolve({ identifier: 'password-protected-chat' })

const { GET } = await import('@/app/api/chat/[subdomain]/route')
const { GET } = await import('@/app/api/chat/[identifier]/route')

const response = await GET(req, { params })

Expand All @@ -243,9 +243,9 @@ describe('Chat Subdomain API Route', () => {
describe('POST endpoint', () => {
it('should handle authentication requests without input', async () => {
const req = createMockRequest('POST', { password: 'test-password' })
const params = Promise.resolve({ subdomain: 'password-protected-chat' })
const params = Promise.resolve({ identifier: 'password-protected-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand All @@ -259,9 +259,9 @@ describe('Chat Subdomain API Route', () => {

it('should return 400 for requests without input', async () => {
const req = createMockRequest('POST', {})
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand All @@ -280,9 +280,9 @@ describe('Chat Subdomain API Route', () => {
}))

const req = createMockRequest('POST', { input: 'Hello' })
const params = Promise.resolve({ subdomain: 'protected-chat' })
const params = Promise.resolve({ identifier: 'protected-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand Down Expand Up @@ -343,9 +343,9 @@ describe('Chat Subdomain API Route', () => {
})

const req = createMockRequest('POST', { input: 'Hello' })
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand All @@ -358,9 +358,9 @@ describe('Chat Subdomain API Route', () => {

it('should return streaming response for valid chat messages', async () => {
const req = createMockRequest('POST', { input: 'Hello world', conversationId: 'conv-123' })
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand All @@ -375,9 +375,9 @@ describe('Chat Subdomain API Route', () => {

it('should handle streaming response body correctly', async () => {
const req = createMockRequest('POST', { input: 'Hello world' })
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand Down Expand Up @@ -405,9 +405,9 @@ describe('Chat Subdomain API Route', () => {
})

const req = createMockRequest('POST', { input: 'Trigger error' })
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand All @@ -430,9 +430,9 @@ describe('Chat Subdomain API Route', () => {
json: vi.fn().mockRejectedValue(new Error('Invalid JSON')),
} as any

const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

const response = await POST(req, { params })

Expand All @@ -448,9 +448,9 @@ describe('Chat Subdomain API Route', () => {
input: 'Hello world',
conversationId: 'test-conversation-123',
})
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

await POST(req, { params })

Expand All @@ -463,9 +463,9 @@ describe('Chat Subdomain API Route', () => {

it('should handle missing conversationId gracefully', async () => {
const req = createMockRequest('POST', { input: 'Hello world' })
const params = Promise.resolve({ subdomain: 'test-chat' })
const params = Promise.resolve({ identifier: 'test-chat' })

const { POST } = await import('@/app/api/chat/[subdomain]/route')
const { POST } = await import('@/app/api/chat/[identifier]/route')

await POST(req, { params })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import {
} from '@/app/api/chat/utils'
import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/utils'

const logger = createLogger('ChatSubdomainAPI')
const logger = createLogger('ChatIdentifierAPI')

// This endpoint handles chat interactions via the subdomain
// This endpoint handles chat interactions via the identifier
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ subdomain: string }> }
{ params }: { params: Promise<{ identifier: string }> }
) {
const { subdomain } = await params
const { identifier } = await params
const requestId = generateRequestId()

try {
logger.debug(`[${requestId}] Processing chat request for subdomain: ${subdomain}`)
logger.debug(`[${requestId}] Processing chat request for identifier: ${identifier}`)

// Parse the request body once
let parsedBody
Expand All @@ -34,7 +34,7 @@ export async function POST(
return addCorsHeaders(createErrorResponse('Invalid request body', 400), request)
}

// Find the chat deployment for this subdomain
// Find the chat deployment for this identifier
const deploymentResult = await db
.select({
id: chat.id,
Expand All @@ -47,19 +47,19 @@ export async function POST(
outputConfigs: chat.outputConfigs,
})
.from(chat)
.where(eq(chat.subdomain, subdomain))
.where(eq(chat.identifier, identifier))
.limit(1)

if (deploymentResult.length === 0) {
logger.warn(`[${requestId}] Chat not found for subdomain: ${subdomain}`)
logger.warn(`[${requestId}] Chat not found for identifier: ${identifier}`)
return addCorsHeaders(createErrorResponse('Chat not found', 404), request)
}

const deployment = deploymentResult[0]

// Check if the chat is active
if (!deployment.isActive) {
logger.warn(`[${requestId}] Chat is not active: ${subdomain}`)
logger.warn(`[${requestId}] Chat is not active: ${identifier}`)
return addCorsHeaders(createErrorResponse('This chat is currently unavailable', 403), request)
}

Expand Down Expand Up @@ -139,15 +139,15 @@ export async function POST(
// This endpoint returns information about the chat
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ subdomain: string }> }
{ params }: { params: Promise<{ identifier: string }> }
) {
const { subdomain } = await params
const { identifier } = await params
const requestId = generateRequestId()

try {
logger.debug(`[${requestId}] Fetching chat info for subdomain: ${subdomain}`)
logger.debug(`[${requestId}] Fetching chat info for identifier: ${identifier}`)

// Find the chat deployment for this subdomain
// Find the chat deployment for this identifier
const deploymentResult = await db
.select({
id: chat.id,
Expand All @@ -162,19 +162,19 @@ export async function GET(
outputConfigs: chat.outputConfigs,
})
.from(chat)
.where(eq(chat.subdomain, subdomain))
.where(eq(chat.identifier, identifier))
.limit(1)

if (deploymentResult.length === 0) {
logger.warn(`[${requestId}] Chat not found for subdomain: ${subdomain}`)
logger.warn(`[${requestId}] Chat not found for identifier: ${identifier}`)
return addCorsHeaders(createErrorResponse('Chat not found', 404), request)
}

const deployment = deploymentResult[0]

// Check if the chat is active
if (!deployment.isActive) {
logger.warn(`[${requestId}] Chat is not active: ${subdomain}`)
logger.warn(`[${requestId}] Chat is not active: ${identifier}`)
return addCorsHeaders(createErrorResponse('This chat is currently unavailable', 403), request)
}

Expand Down Expand Up @@ -205,7 +205,7 @@ export async function GET(
const authResult = await validateChatAuth(requestId, deployment, request)
if (!authResult.authorized) {
logger.info(
`[${requestId}] Authentication required for chat: ${subdomain}, type: ${deployment.authType}`
`[${requestId}] Authentication required for chat: ${identifier}, type: ${deployment.authType}`
)
return addCorsHeaders(
createErrorResponse(authResult.error || 'Authentication required', 401),
Expand Down
Loading