|
| 1 | +import { db, workflow } from '@sim/db' |
| 2 | +import { createLogger } from '@sim/logger' |
| 3 | +import { eq } from 'drizzle-orm' |
| 4 | +import { |
| 5 | + deployWorkflow, |
| 6 | + loadWorkflowFromNormalizedTables, |
| 7 | + undeployWorkflow, |
| 8 | +} from '@/lib/workflows/persistence/utils' |
| 9 | +import { createSchedulesForDeploy, validateWorkflowSchedules } from '@/lib/workflows/schedules' |
| 10 | +import { withAdminAuthParams } from '@/app/api/v1/admin/middleware' |
| 11 | +import { |
| 12 | + badRequestResponse, |
| 13 | + internalErrorResponse, |
| 14 | + notFoundResponse, |
| 15 | + singleResponse, |
| 16 | +} from '@/app/api/v1/admin/responses' |
| 17 | +import type { AdminDeployResult, AdminUndeployResult } from '@/app/api/v1/admin/types' |
| 18 | + |
| 19 | +const logger = createLogger('AdminWorkflowDeployAPI') |
| 20 | + |
| 21 | +const ADMIN_ACTOR_ID = 'admin-api' |
| 22 | + |
| 23 | +interface RouteParams { |
| 24 | + id: string |
| 25 | +} |
| 26 | + |
| 27 | +export const POST = withAdminAuthParams<RouteParams>(async (request, context) => { |
| 28 | + const { id: workflowId } = await context.params |
| 29 | + |
| 30 | + try { |
| 31 | + const [workflowRecord] = await db |
| 32 | + .select({ id: workflow.id, name: workflow.name }) |
| 33 | + .from(workflow) |
| 34 | + .where(eq(workflow.id, workflowId)) |
| 35 | + .limit(1) |
| 36 | + |
| 37 | + if (!workflowRecord) { |
| 38 | + return notFoundResponse('Workflow') |
| 39 | + } |
| 40 | + |
| 41 | + const normalizedData = await loadWorkflowFromNormalizedTables(workflowId) |
| 42 | + if (!normalizedData) { |
| 43 | + return badRequestResponse('Workflow has no saved state') |
| 44 | + } |
| 45 | + |
| 46 | + const scheduleValidation = validateWorkflowSchedules(normalizedData.blocks) |
| 47 | + if (!scheduleValidation.isValid) { |
| 48 | + return badRequestResponse(`Invalid schedule configuration: ${scheduleValidation.error}`) |
| 49 | + } |
| 50 | + |
| 51 | + const deployResult = await deployWorkflow({ |
| 52 | + workflowId, |
| 53 | + deployedBy: ADMIN_ACTOR_ID, |
| 54 | + workflowName: workflowRecord.name, |
| 55 | + }) |
| 56 | + |
| 57 | + if (!deployResult.success) { |
| 58 | + return internalErrorResponse(deployResult.error || 'Failed to deploy workflow') |
| 59 | + } |
| 60 | + |
| 61 | + const scheduleResult = await createSchedulesForDeploy(workflowId, normalizedData.blocks, db) |
| 62 | + if (!scheduleResult.success) { |
| 63 | + logger.warn(`Schedule creation failed for workflow ${workflowId}: ${scheduleResult.error}`) |
| 64 | + } |
| 65 | + |
| 66 | + logger.info(`Admin API: Deployed workflow ${workflowId} as v${deployResult.version}`) |
| 67 | + |
| 68 | + const response: AdminDeployResult = { |
| 69 | + isDeployed: true, |
| 70 | + version: deployResult.version!, |
| 71 | + deployedAt: deployResult.deployedAt!.toISOString(), |
| 72 | + } |
| 73 | + |
| 74 | + return singleResponse(response) |
| 75 | + } catch (error) { |
| 76 | + logger.error(`Admin API: Failed to deploy workflow ${workflowId}`, { error }) |
| 77 | + return internalErrorResponse('Failed to deploy workflow') |
| 78 | + } |
| 79 | +}) |
| 80 | + |
| 81 | +export const DELETE = withAdminAuthParams<RouteParams>(async (request, context) => { |
| 82 | + const { id: workflowId } = await context.params |
| 83 | + |
| 84 | + try { |
| 85 | + const [workflowRecord] = await db |
| 86 | + .select({ id: workflow.id }) |
| 87 | + .from(workflow) |
| 88 | + .where(eq(workflow.id, workflowId)) |
| 89 | + .limit(1) |
| 90 | + |
| 91 | + if (!workflowRecord) { |
| 92 | + return notFoundResponse('Workflow') |
| 93 | + } |
| 94 | + |
| 95 | + const result = await undeployWorkflow({ workflowId }) |
| 96 | + if (!result.success) { |
| 97 | + return internalErrorResponse(result.error || 'Failed to undeploy workflow') |
| 98 | + } |
| 99 | + |
| 100 | + logger.info(`Admin API: Undeployed workflow ${workflowId}`) |
| 101 | + |
| 102 | + const response: AdminUndeployResult = { |
| 103 | + isDeployed: false, |
| 104 | + } |
| 105 | + |
| 106 | + return singleResponse(response) |
| 107 | + } catch (error) { |
| 108 | + logger.error(`Admin API: Failed to undeploy workflow ${workflowId}`, { error }) |
| 109 | + return internalErrorResponse('Failed to undeploy workflow') |
| 110 | + } |
| 111 | +}) |
0 commit comments