d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import { createLogger } from '@sim/logger'
|
|
import { getErrorMessage } from '@sim/utils/errors'
|
|
import { type NextRequest, NextResponse } from 'next/server'
|
|
import { tableIdParamsSchema } from '@/lib/api/contracts/tables'
|
|
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
|
import { generateRequestId } from '@/lib/core/utils/request'
|
|
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
|
import { getTableById } from '@/lib/table'
|
|
import { performRestoreTable } from '@/lib/table/orchestration'
|
|
import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
|
|
|
|
const logger = createLogger('RestoreTableAPI')
|
|
|
|
export const POST = withRouteHandler(
|
|
async (request: NextRequest, { params }: { params: Promise<{ tableId: string }> }) => {
|
|
const requestId = generateRequestId()
|
|
const { tableId } = tableIdParamsSchema.parse(await params)
|
|
|
|
try {
|
|
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
|
if (!auth.success || !auth.userId) {
|
|
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
|
}
|
|
|
|
const table = await getTableById(tableId, { includeArchived: true })
|
|
if (!table) {
|
|
return NextResponse.json({ error: 'Table not found' }, { status: 404 })
|
|
}
|
|
|
|
const permission = await getUserEntityPermissions(auth.userId, 'workspace', table.workspaceId)
|
|
if (permission !== 'admin' && permission !== 'write') {
|
|
return NextResponse.json({ error: 'Insufficient permissions' }, { status: 403 })
|
|
}
|
|
|
|
const result = await performRestoreTable({ tableId, userId: auth.userId, requestId })
|
|
if (!result.success) {
|
|
const status =
|
|
result.errorCode === 'not_found' ? 404 : result.errorCode === 'conflict' ? 409 : 500
|
|
return NextResponse.json({ error: result.error }, { status })
|
|
}
|
|
|
|
logger.info(`[${requestId}] Restored table ${tableId}`)
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: { table: result.table },
|
|
})
|
|
} catch (error) {
|
|
logger.error(`[${requestId}] Error restoring table ${tableId}`, error)
|
|
return NextResponse.json(
|
|
{ error: getErrorMessage(error, 'Internal server error') },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
)
|