( params: PerformActivateVersionParams )
| 348 | * PATCH handler and the admin activate route must use this function. |
| 349 | */ |
| 350 | export async function performActivateVersion( |
| 351 | params: PerformActivateVersionParams |
| 352 | ): Promise<PerformActivateVersionResult> { |
| 353 | const { workflowId, version, userId, workflow } = params |
| 354 | const actorId = params.actorId ?? userId |
| 355 | const requestId = params.requestId ?? generateRequestId() |
| 356 | |
| 357 | const [versionRow] = await db |
| 358 | .select({ |
| 359 | id: workflowDeploymentVersion.id, |
| 360 | state: workflowDeploymentVersion.state, |
| 361 | isActive: workflowDeploymentVersion.isActive, |
| 362 | }) |
| 363 | .from(workflowDeploymentVersion) |
| 364 | .where( |
| 365 | and( |
| 366 | eq(workflowDeploymentVersion.workflowId, workflowId), |
| 367 | eq(workflowDeploymentVersion.version, version) |
| 368 | ) |
| 369 | ) |
| 370 | .limit(1) |
| 371 | |
| 372 | if (!versionRow?.state) { |
| 373 | return { success: false, error: 'Deployment version not found', errorCode: 'not_found' } |
| 374 | } |
| 375 | |
| 376 | if (versionRow.isActive) { |
| 377 | const [workflowDeployment] = await db |
| 378 | .select({ deployedAt: workflowTable.deployedAt }) |
| 379 | .from(workflowTable) |
| 380 | .where(eq(workflowTable.id, workflowId)) |
| 381 | .limit(1) |
| 382 | |
| 383 | return { success: true, deployedAt: workflowDeployment?.deployedAt ?? new Date(), warnings: [] } |
| 384 | } |
| 385 | |
| 386 | const deployedState = versionRow.state as { blocks?: Record<string, unknown> } |
| 387 | const blocks = deployedState.blocks |
| 388 | if (!blocks || typeof blocks !== 'object') { |
| 389 | return { success: false, error: 'Invalid deployed state structure', errorCode: 'validation' } |
| 390 | } |
| 391 | |
| 392 | const scheduleValidation = validateWorkflowSchedules(blocks as Record<string, BlockState>) |
| 393 | if (!scheduleValidation.isValid) { |
| 394 | return { |
| 395 | success: false, |
| 396 | error: `Invalid schedule configuration: ${scheduleValidation.error}`, |
| 397 | errorCode: 'validation', |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | const triggerValidation = await validateTriggerWebhookConfigForDeploy( |
| 402 | blocks as Record<string, BlockState> |
| 403 | ) |
| 404 | if (!triggerValidation.success) { |
| 405 | return { |
| 406 | success: false, |
| 407 | error: triggerValidation.error?.message || 'Invalid trigger configuration', |
no test coverage detected