(params: {
workflowId: string
deploymentVersionId: string
})
| 1099 | } |
| 1100 | |
| 1101 | async function activateWorkflowVersionById(params: { |
| 1102 | workflowId: string |
| 1103 | deploymentVersionId: string |
| 1104 | }): Promise<{ |
| 1105 | success: boolean |
| 1106 | deployedAt?: Date |
| 1107 | state?: unknown |
| 1108 | previousVersionId?: string |
| 1109 | error?: string |
| 1110 | }> { |
| 1111 | const { workflowId, deploymentVersionId } = params |
| 1112 | |
| 1113 | try { |
| 1114 | const now = new Date() |
| 1115 | let versionState: unknown |
| 1116 | |
| 1117 | const result = await db.transaction(async (tx) => { |
| 1118 | if (!(await lockWorkflowForUpdate(tx, workflowId))) { |
| 1119 | return { success: false as const, error: 'Workflow not found' } |
| 1120 | } |
| 1121 | |
| 1122 | const [currentActiveVersion] = await tx |
| 1123 | .select({ id: workflowDeploymentVersion.id }) |
| 1124 | .from(workflowDeploymentVersion) |
| 1125 | .where( |
| 1126 | and( |
| 1127 | eq(workflowDeploymentVersion.workflowId, workflowId), |
| 1128 | eq(workflowDeploymentVersion.isActive, true) |
| 1129 | ) |
| 1130 | ) |
| 1131 | .limit(1) |
| 1132 | |
| 1133 | const [versionData] = await tx |
| 1134 | .select({ id: workflowDeploymentVersion.id, state: workflowDeploymentVersion.state }) |
| 1135 | .from(workflowDeploymentVersion) |
| 1136 | .where( |
| 1137 | and( |
| 1138 | eq(workflowDeploymentVersion.workflowId, workflowId), |
| 1139 | eq(workflowDeploymentVersion.id, deploymentVersionId) |
| 1140 | ) |
| 1141 | ) |
| 1142 | .limit(1) |
| 1143 | |
| 1144 | if (!versionData) { |
| 1145 | return { success: false as const, error: 'Deployment version not found' } |
| 1146 | } |
| 1147 | versionState = versionData.state |
| 1148 | |
| 1149 | await tx |
| 1150 | .update(workflowDeploymentVersion) |
| 1151 | .set({ isActive: false }) |
| 1152 | .where(eq(workflowDeploymentVersion.workflowId, workflowId)) |
| 1153 | |
| 1154 | await tx |
| 1155 | .update(workflowDeploymentVersion) |
| 1156 | .set({ isActive: true }) |
| 1157 | .where( |
| 1158 | and( |
nothing calls this directly
no test coverage detected