mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
* updates * required * trashy table viewer * updates * updates * filtering ui * updates * updates * updates * one input mode * format * fix lints * improved errors * updates * updates * chages * doc strings * breaking down file * update comments with ai * updates * comments * changes * revert * updates * dedupe * updates * updates * updates * refactoring * renames & refactors * refactoring * updates * undo * update db * wand * updates * fix comments * fixes * simplify comments * u[dates * renames * better comments * validation * updates * updates * updates * fix sorting * fix appearnce * updating prompt to make it user sort * rm * updates * rename * comments * clean comments * simplicifcaiton * updates * updates * refactor * reduced type confusion * undo * rename * undo changes * undo * simplify * updates * updates * revert * updates * db updates * type fix * fix * fix error handling * updates * docs * docs * updates * rename * dedupe * revert * uncook * updates * fix * fix * fix * fix * prepare merge * readd migrations * add back missed code * migrate enrichment logic to general abstraction * address bugbot concerns * adhere to size limits for tables * remove conflicting migration * add back migrations * fix tables auth * fix permissive auth * fix lint * reran migrations * migrate to use tanstack query for all server state * update table-selector * update names * added tables to permission groups, updated subblock types --------- Co-authored-by: Vikhyath Mondreti <vikhyath@simstudio.ai> Co-authored-by: waleed <walif6@gmail.com>
139 lines
4.7 KiB
TypeScript
139 lines
4.7 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { deleteTable, type TableSchema } from '@/lib/table'
|
|
import { accessError, checkAccess, normalizeColumn, verifyTableWorkspace } from '../utils'
|
|
|
|
const logger = createLogger('TableDetailAPI')
|
|
|
|
const GetTableSchema = z.object({
|
|
workspaceId: z.string().min(1, 'Workspace ID is required'),
|
|
})
|
|
|
|
interface TableRouteParams {
|
|
params: Promise<{ tableId: string }>
|
|
}
|
|
|
|
/** GET /api/table/[tableId] - Retrieves a single table's details. */
|
|
export async function GET(request: NextRequest, { params }: TableRouteParams) {
|
|
const requestId = generateRequestId()
|
|
const { tableId } = await params
|
|
|
|
try {
|
|
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
|
if (!authResult.success || !authResult.userId) {
|
|
logger.warn(`[${requestId}] Unauthorized table access attempt`)
|
|
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const validated = GetTableSchema.parse({
|
|
workspaceId: searchParams.get('workspaceId'),
|
|
})
|
|
|
|
const result = await checkAccess(tableId, authResult.userId, 'read')
|
|
if (!result.ok) return accessError(result, requestId, tableId)
|
|
|
|
const { table } = result
|
|
|
|
const isValidWorkspace = await verifyTableWorkspace(tableId, validated.workspaceId)
|
|
if (!isValidWorkspace) {
|
|
logger.warn(
|
|
`[${requestId}] Workspace ID mismatch for table ${tableId}. Provided: ${validated.workspaceId}, Actual: ${table.workspaceId}`
|
|
)
|
|
return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 })
|
|
}
|
|
|
|
logger.info(`[${requestId}] Retrieved table ${tableId} for user ${authResult.userId}`)
|
|
|
|
const schemaData = table.schema as TableSchema
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: {
|
|
table: {
|
|
id: table.id,
|
|
name: table.name,
|
|
description: table.description,
|
|
schema: {
|
|
columns: schemaData.columns.map(normalizeColumn),
|
|
},
|
|
rowCount: table.rowCount,
|
|
maxRows: table.maxRows,
|
|
createdAt:
|
|
table.createdAt instanceof Date
|
|
? table.createdAt.toISOString()
|
|
: String(table.createdAt),
|
|
updatedAt:
|
|
table.updatedAt instanceof Date
|
|
? table.updatedAt.toISOString()
|
|
: String(table.updatedAt),
|
|
},
|
|
},
|
|
})
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json(
|
|
{ error: 'Validation error', details: error.errors },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
logger.error(`[${requestId}] Error getting table:`, error)
|
|
return NextResponse.json({ error: 'Failed to get table' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
/** DELETE /api/table/[tableId] - Deletes a table and all its rows. */
|
|
export async function DELETE(request: NextRequest, { params }: TableRouteParams) {
|
|
const requestId = generateRequestId()
|
|
const { tableId } = await params
|
|
|
|
try {
|
|
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
|
if (!authResult.success || !authResult.userId) {
|
|
logger.warn(`[${requestId}] Unauthorized table delete attempt`)
|
|
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const validated = GetTableSchema.parse({
|
|
workspaceId: searchParams.get('workspaceId'),
|
|
})
|
|
|
|
const result = await checkAccess(tableId, authResult.userId, 'write')
|
|
if (!result.ok) return accessError(result, requestId, tableId)
|
|
|
|
const { table } = result
|
|
|
|
const isValidWorkspace = await verifyTableWorkspace(tableId, validated.workspaceId)
|
|
if (!isValidWorkspace) {
|
|
logger.warn(
|
|
`[${requestId}] Workspace ID mismatch for table ${tableId}. Provided: ${validated.workspaceId}, Actual: ${table.workspaceId}`
|
|
)
|
|
return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 })
|
|
}
|
|
|
|
await deleteTable(tableId, requestId)
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: {
|
|
message: 'Table deleted successfully',
|
|
},
|
|
})
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json(
|
|
{ error: 'Validation error', details: error.errors },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
logger.error(`[${requestId}] Error deleting table:`, error)
|
|
return NextResponse.json({ error: 'Failed to delete table' }, { status: 500 })
|
|
}
|
|
}
|