|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { cancelTableRunsContract } from '@/lib/api/contracts/tables' |
| 4 | +import { parseRequest } from '@/lib/api/server' |
| 5 | +import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid' |
| 6 | +import { generateRequestId } from '@/lib/core/utils/request' |
| 7 | +import { withRouteHandler } from '@/lib/core/utils/with-route-handler' |
| 8 | +import { cancelWorkflowGroupRuns } from '@/lib/table/workflow-columns' |
| 9 | +import { accessError, checkAccess } from '@/app/api/table/utils' |
| 10 | + |
| 11 | +const logger = createLogger('TableCancelRunsAPI') |
| 12 | + |
| 13 | +interface RouteParams { |
| 14 | + params: Promise<{ tableId: string }> |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * POST /api/table/[tableId]/cancel-runs |
| 19 | + * |
| 20 | + * Cancels in-flight and pending workflow-column runs for this table. Scopes: |
| 21 | + * `all` (every cell) or `row` (every cell for `rowId`). |
| 22 | + */ |
| 23 | +export const POST = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => { |
| 24 | + const requestId = generateRequestId() |
| 25 | + |
| 26 | + try { |
| 27 | + const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false }) |
| 28 | + if (!authResult.success || !authResult.userId) { |
| 29 | + return NextResponse.json({ error: 'Authentication required' }, { status: 401 }) |
| 30 | + } |
| 31 | + |
| 32 | + const parsed = await parseRequest(cancelTableRunsContract, request, { params }) |
| 33 | + if (!parsed.success) return parsed.response |
| 34 | + const { tableId } = parsed.data.params |
| 35 | + const { workspaceId, scope, rowId } = parsed.data.body |
| 36 | + |
| 37 | + const result = await checkAccess(tableId, authResult.userId, 'write') |
| 38 | + if (!result.ok) return accessError(result, requestId, tableId) |
| 39 | + const { table } = result |
| 40 | + |
| 41 | + if (table.workspaceId !== workspaceId) { |
| 42 | + return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 }) |
| 43 | + } |
| 44 | + |
| 45 | + const cancelled = await cancelWorkflowGroupRuns(tableId, scope === 'row' ? rowId : undefined) |
| 46 | + logger.info( |
| 47 | + `[${requestId}] cancel-runs: tableId=${tableId} scope=${scope}${ |
| 48 | + rowId ? ` rowId=${rowId}` : '' |
| 49 | + } cancelled=${cancelled}` |
| 50 | + ) |
| 51 | + |
| 52 | + return NextResponse.json({ success: true, data: { cancelled } }) |
| 53 | + } catch (error) { |
| 54 | + logger.error(`[${requestId}] cancel-runs failed:`, error) |
| 55 | + return NextResponse.json({ error: 'Failed to cancel runs' }, { status: 500 }) |
| 56 | + } |
| 57 | +}) |
0 commit comments