|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { z } from 'zod' |
| 4 | +import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' |
| 5 | +import { generateRequestId } from '@/lib/core/utils/request' |
| 6 | +import { withRouteHandler } from '@/lib/core/utils/with-route-handler' |
| 7 | +import { addWorkflowGroup, deleteWorkflowGroup, updateWorkflowGroup } from '@/lib/table/service' |
| 8 | +import { |
| 9 | + accessError, |
| 10 | + AddWorkflowGroupSchema, |
| 11 | + checkAccess, |
| 12 | + DeleteWorkflowGroupSchema, |
| 13 | + normalizeColumn, |
| 14 | + UpdateWorkflowGroupSchema, |
| 15 | +} from '@/app/api/table/utils' |
| 16 | + |
| 17 | +const logger = createLogger('TableWorkflowGroupsAPI') |
| 18 | + |
| 19 | +interface RouteParams { |
| 20 | + params: Promise<{ tableId: string }> |
| 21 | +} |
| 22 | + |
| 23 | +/** POST /api/table/[tableId]/groups — create a workflow group + its output columns. */ |
| 24 | +export const POST = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => { |
| 25 | + const requestId = generateRequestId() |
| 26 | + const { tableId } = await params |
| 27 | + try { |
| 28 | + const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) |
| 29 | + if (!authResult.success || !authResult.userId) { |
| 30 | + return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) |
| 31 | + } |
| 32 | + const body = await request.json() |
| 33 | + const validated = AddWorkflowGroupSchema.parse(body) |
| 34 | + const result = await checkAccess(tableId, authResult.userId, 'write') |
| 35 | + if (!result.ok) return accessError(result, requestId, tableId) |
| 36 | + if (result.table.workspaceId !== validated.workspaceId) { |
| 37 | + return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 }) |
| 38 | + } |
| 39 | + const updatedTable = await addWorkflowGroup( |
| 40 | + { tableId, group: validated.group, outputColumns: validated.outputColumns }, |
| 41 | + requestId |
| 42 | + ) |
| 43 | + return NextResponse.json({ |
| 44 | + success: true, |
| 45 | + data: { |
| 46 | + columns: updatedTable.schema.columns.map(normalizeColumn), |
| 47 | + workflowGroups: updatedTable.schema.workflowGroups ?? [], |
| 48 | + }, |
| 49 | + }) |
| 50 | + } catch (error) { |
| 51 | + if (error instanceof z.ZodError) { |
| 52 | + return NextResponse.json( |
| 53 | + { error: 'Invalid request data', details: error.errors }, |
| 54 | + { status: 400 } |
| 55 | + ) |
| 56 | + } |
| 57 | + if (error instanceof Error) { |
| 58 | + const msg = error.message |
| 59 | + if (msg === 'Table not found') return NextResponse.json({ error: msg }, { status: 404 }) |
| 60 | + if ( |
| 61 | + msg.includes('already exists') || |
| 62 | + msg.includes('Schema validation') || |
| 63 | + msg.includes('exceed') |
| 64 | + ) { |
| 65 | + return NextResponse.json({ error: msg }, { status: 400 }) |
| 66 | + } |
| 67 | + } |
| 68 | + logger.error(`POST groups failed for ${tableId}:`, error) |
| 69 | + return NextResponse.json({ error: 'Failed to add workflow group' }, { status: 500 }) |
| 70 | + } |
| 71 | +}) |
| 72 | + |
| 73 | +/** PATCH /api/table/[tableId]/groups — update a workflow group (deps / outputs). */ |
| 74 | +export const PATCH = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => { |
| 75 | + const requestId = generateRequestId() |
| 76 | + const { tableId } = await params |
| 77 | + try { |
| 78 | + const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) |
| 79 | + if (!authResult.success || !authResult.userId) { |
| 80 | + return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) |
| 81 | + } |
| 82 | + const body = await request.json() |
| 83 | + const validated = UpdateWorkflowGroupSchema.parse(body) |
| 84 | + const result = await checkAccess(tableId, authResult.userId, 'write') |
| 85 | + if (!result.ok) return accessError(result, requestId, tableId) |
| 86 | + if (result.table.workspaceId !== validated.workspaceId) { |
| 87 | + return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 }) |
| 88 | + } |
| 89 | + const updatedTable = await updateWorkflowGroup( |
| 90 | + { |
| 91 | + tableId, |
| 92 | + groupId: validated.groupId, |
| 93 | + ...(validated.workflowId !== undefined ? { workflowId: validated.workflowId } : {}), |
| 94 | + ...(validated.name !== undefined ? { name: validated.name } : {}), |
| 95 | + ...(validated.dependencies !== undefined |
| 96 | + ? { dependencies: validated.dependencies } |
| 97 | + : {}), |
| 98 | + ...(validated.outputs !== undefined ? { outputs: validated.outputs } : {}), |
| 99 | + ...(validated.newOutputColumns !== undefined |
| 100 | + ? { newOutputColumns: validated.newOutputColumns } |
| 101 | + : {}), |
| 102 | + }, |
| 103 | + requestId |
| 104 | + ) |
| 105 | + return NextResponse.json({ |
| 106 | + success: true, |
| 107 | + data: { |
| 108 | + columns: updatedTable.schema.columns.map(normalizeColumn), |
| 109 | + workflowGroups: updatedTable.schema.workflowGroups ?? [], |
| 110 | + }, |
| 111 | + }) |
| 112 | + } catch (error) { |
| 113 | + if (error instanceof z.ZodError) { |
| 114 | + return NextResponse.json( |
| 115 | + { error: 'Invalid request data', details: error.errors }, |
| 116 | + { status: 400 } |
| 117 | + ) |
| 118 | + } |
| 119 | + if (error instanceof Error) { |
| 120 | + const msg = error.message |
| 121 | + if (msg.includes('not found')) return NextResponse.json({ error: msg }, { status: 404 }) |
| 122 | + if ( |
| 123 | + msg.includes('Schema validation') || |
| 124 | + msg.includes('Missing column definition') || |
| 125 | + msg.includes('already exists') |
| 126 | + ) { |
| 127 | + return NextResponse.json({ error: msg }, { status: 400 }) |
| 128 | + } |
| 129 | + } |
| 130 | + logger.error(`PATCH groups failed for ${tableId}:`, error) |
| 131 | + return NextResponse.json({ error: 'Failed to update workflow group' }, { status: 500 }) |
| 132 | + } |
| 133 | +}) |
| 134 | + |
| 135 | +/** DELETE /api/table/[tableId]/groups — remove a workflow group + its columns. */ |
| 136 | +export const DELETE = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => { |
| 137 | + const requestId = generateRequestId() |
| 138 | + const { tableId } = await params |
| 139 | + try { |
| 140 | + const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) |
| 141 | + if (!authResult.success || !authResult.userId) { |
| 142 | + return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) |
| 143 | + } |
| 144 | + const body = await request.json() |
| 145 | + const validated = DeleteWorkflowGroupSchema.parse(body) |
| 146 | + const result = await checkAccess(tableId, authResult.userId, 'write') |
| 147 | + if (!result.ok) return accessError(result, requestId, tableId) |
| 148 | + if (result.table.workspaceId !== validated.workspaceId) { |
| 149 | + return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 }) |
| 150 | + } |
| 151 | + const updatedTable = await deleteWorkflowGroup( |
| 152 | + { tableId, groupId: validated.groupId }, |
| 153 | + requestId |
| 154 | + ) |
| 155 | + return NextResponse.json({ |
| 156 | + success: true, |
| 157 | + data: { |
| 158 | + columns: updatedTable.schema.columns.map(normalizeColumn), |
| 159 | + workflowGroups: updatedTable.schema.workflowGroups ?? [], |
| 160 | + }, |
| 161 | + }) |
| 162 | + } catch (error) { |
| 163 | + if (error instanceof z.ZodError) { |
| 164 | + return NextResponse.json( |
| 165 | + { error: 'Invalid request data', details: error.errors }, |
| 166 | + { status: 400 } |
| 167 | + ) |
| 168 | + } |
| 169 | + if (error instanceof Error && error.message.includes('not found')) { |
| 170 | + return NextResponse.json({ error: error.message }, { status: 404 }) |
| 171 | + } |
| 172 | + logger.error(`DELETE groups failed for ${tableId}:`, error) |
| 173 | + return NextResponse.json({ error: 'Failed to delete workflow group' }, { status: 500 }) |
| 174 | + } |
| 175 | +}) |
0 commit comments