( tx: any, workflowId: string, operation: string, payload: any )
| 1839 | |
| 1840 | // Variable operations - updates workflow.variables JSON field |
| 1841 | async function handleVariableOperationTx( |
| 1842 | tx: any, |
| 1843 | workflowId: string, |
| 1844 | operation: string, |
| 1845 | payload: any |
| 1846 | ) { |
| 1847 | // Get current workflow variables |
| 1848 | const workflowData = await tx |
| 1849 | .select({ variables: workflow.variables }) |
| 1850 | .from(workflow) |
| 1851 | .where(eq(workflow.id, workflowId)) |
| 1852 | .limit(1) |
| 1853 | |
| 1854 | if (workflowData.length === 0) { |
| 1855 | throw new Error(`Workflow ${workflowId} not found`) |
| 1856 | } |
| 1857 | |
| 1858 | const currentVariables = (workflowData[0].variables as Record<string, any>) || {} |
| 1859 | |
| 1860 | switch (operation) { |
| 1861 | case VARIABLE_OPERATIONS.ADD: { |
| 1862 | if (!payload.id || !payload.name || payload.type === undefined) { |
| 1863 | throw new Error('Missing required fields for add variable operation') |
| 1864 | } |
| 1865 | |
| 1866 | // Add the new variable |
| 1867 | const updatedVariables = { |
| 1868 | ...currentVariables, |
| 1869 | [payload.id]: { |
| 1870 | id: payload.id, |
| 1871 | workflowId, |
| 1872 | name: payload.name, |
| 1873 | type: payload.type, |
| 1874 | value: payload.value || '', |
| 1875 | }, |
| 1876 | } |
| 1877 | |
| 1878 | await tx |
| 1879 | .update(workflow) |
| 1880 | .set({ |
| 1881 | variables: updatedVariables, |
| 1882 | updatedAt: new Date(), |
| 1883 | }) |
| 1884 | .where(eq(workflow.id, workflowId)) |
| 1885 | |
| 1886 | logger.debug(`Added variable ${payload.id} (${payload.name}) to workflow ${workflowId}`) |
| 1887 | break |
| 1888 | } |
| 1889 | |
| 1890 | case VARIABLE_OPERATIONS.REMOVE: { |
| 1891 | if (!payload.variableId) { |
| 1892 | throw new Error('Missing variable ID for remove operation') |
| 1893 | } |
| 1894 | |
| 1895 | // Remove the variable |
| 1896 | const { [payload.variableId]: _, ...updatedVariables } = currentVariables |
| 1897 | |
| 1898 | await tx |
no test coverage detected