@@ -7,6 +7,16 @@ import postgres from 'postgres'
77import { env } from '@/lib/core/config/env'
88import { cleanupExternalWebhook } from '@/lib/webhooks/provider-subscriptions'
99import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
10+ import {
11+ BLOCK_OPERATIONS ,
12+ BLOCKS_OPERATIONS ,
13+ EDGE_OPERATIONS ,
14+ EDGES_OPERATIONS ,
15+ OPERATION_TARGETS ,
16+ SUBFLOW_OPERATIONS ,
17+ VARIABLE_OPERATIONS ,
18+ WORKFLOW_OPERATIONS ,
19+ } from '@/socket/constants'
1020
1121const logger = createLogger ( 'SocketDatabase' )
1222
@@ -155,7 +165,7 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
155165 try {
156166 const { operation : op , target, payload, timestamp, userId } = operation
157167
158- if ( op === 'update-position' && Math . random ( ) < 0.01 ) {
168+ if ( op === BLOCK_OPERATIONS . UPDATE_POSITION && Math . random ( ) < 0.01 ) {
159169 logger . debug ( 'Socket DB operation sample:' , {
160170 operation : op ,
161171 target,
@@ -170,25 +180,25 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
170180 . where ( eq ( workflow . id , workflowId ) )
171181
172182 switch ( target ) {
173- case 'block' :
183+ case OPERATION_TARGETS . BLOCK :
174184 await handleBlockOperationTx ( tx , workflowId , op , payload )
175185 break
176- case 'blocks' :
186+ case OPERATION_TARGETS . BLOCKS :
177187 await handleBlocksOperationTx ( tx , workflowId , op , payload )
178188 break
179- case 'edge' :
189+ case OPERATION_TARGETS . EDGE :
180190 await handleEdgeOperationTx ( tx , workflowId , op , payload )
181191 break
182- case 'edges' :
192+ case OPERATION_TARGETS . EDGES :
183193 await handleEdgesOperationTx ( tx , workflowId , op , payload )
184194 break
185- case 'subflow' :
195+ case OPERATION_TARGETS . SUBFLOW :
186196 await handleSubflowOperationTx ( tx , workflowId , op , payload )
187197 break
188- case 'variable' :
198+ case OPERATION_TARGETS . VARIABLE :
189199 await handleVariableOperationTx ( tx , workflowId , op , payload )
190200 break
191- case 'workflow' :
201+ case OPERATION_TARGETS . WORKFLOW :
192202 await handleWorkflowOperationTx ( tx , workflowId , op , payload )
193203 break
194204 default :
@@ -222,7 +232,7 @@ async function handleBlockOperationTx(
222232 payload : any
223233) {
224234 switch ( operation ) {
225- case 'update-position' : {
235+ case BLOCK_OPERATIONS . UPDATE_POSITION : {
226236 if ( ! payload . id || ! payload . position ) {
227237 throw new Error ( 'Missing required fields for update position operation' )
228238 }
@@ -247,7 +257,7 @@ async function handleBlockOperationTx(
247257 break
248258 }
249259
250- case 'update-name' : {
260+ case BLOCK_OPERATIONS . UPDATE_NAME : {
251261 if ( ! payload . id || ! payload . name ) {
252262 throw new Error ( 'Missing required fields for update name operation' )
253263 }
@@ -269,7 +279,7 @@ async function handleBlockOperationTx(
269279 break
270280 }
271281
272- case 'toggle-enabled' : {
282+ case BLOCK_OPERATIONS . TOGGLE_ENABLED : {
273283 if ( ! payload . id ) {
274284 throw new Error ( 'Missing block ID for toggle enabled operation' )
275285 }
@@ -299,7 +309,7 @@ async function handleBlockOperationTx(
299309 break
300310 }
301311
302- case 'update-parent' : {
312+ case BLOCK_OPERATIONS . UPDATE_PARENT : {
303313 if ( ! payload . id ) {
304314 throw new Error ( 'Missing block ID for update parent operation' )
305315 }
@@ -364,7 +374,7 @@ async function handleBlockOperationTx(
364374 break
365375 }
366376
367- case 'update-advanced-mode' : {
377+ case BLOCK_OPERATIONS . UPDATE_ADVANCED_MODE : {
368378 if ( ! payload . id || payload . advancedMode === undefined ) {
369379 throw new Error ( 'Missing required fields for update advanced mode operation' )
370380 }
@@ -386,7 +396,7 @@ async function handleBlockOperationTx(
386396 break
387397 }
388398
389- case 'update-trigger-mode' : {
399+ case BLOCK_OPERATIONS . UPDATE_TRIGGER_MODE : {
390400 if ( ! payload . id || payload . triggerMode === undefined ) {
391401 throw new Error ( 'Missing required fields for update trigger mode operation' )
392402 }
@@ -408,7 +418,7 @@ async function handleBlockOperationTx(
408418 break
409419 }
410420
411- case 'toggle-handles' : {
421+ case BLOCK_OPERATIONS . TOGGLE_HANDLES : {
412422 if ( ! payload . id || payload . horizontalHandles === undefined ) {
413423 throw new Error ( 'Missing required fields for toggle handles operation' )
414424 }
@@ -445,7 +455,7 @@ async function handleBlocksOperationTx(
445455 payload : any
446456) {
447457 switch ( operation ) {
448- case 'batch-update-positions' : {
458+ case BLOCKS_OPERATIONS . BATCH_UPDATE_POSITIONS : {
449459 const { updates } = payload
450460 if ( ! Array . isArray ( updates ) || updates . length === 0 ) {
451461 return
@@ -466,7 +476,7 @@ async function handleBlocksOperationTx(
466476 break
467477 }
468478
469- case 'batch-add-blocks' : {
479+ case BLOCKS_OPERATIONS . BATCH_ADD_BLOCKS : {
470480 const { blocks, edges, loops, parallels } = payload
471481
472482 logger . info ( `Batch adding blocks to workflow ${ workflowId } ` , {
@@ -578,7 +588,7 @@ async function handleBlocksOperationTx(
578588 break
579589 }
580590
581- case 'batch-remove-blocks' : {
591+ case BLOCKS_OPERATIONS . BATCH_REMOVE_BLOCKS : {
582592 const { ids } = payload
583593 if ( ! Array . isArray ( ids ) || ids . length === 0 ) {
584594 return
@@ -693,7 +703,7 @@ async function handleBlocksOperationTx(
693703 break
694704 }
695705
696- case 'batch-toggle-enabled' : {
706+ case BLOCKS_OPERATIONS . BATCH_TOGGLE_ENABLED : {
697707 const { blockIds } = payload
698708 if ( ! Array . isArray ( blockIds ) || blockIds . length === 0 ) {
699709 return
@@ -722,7 +732,7 @@ async function handleBlocksOperationTx(
722732 break
723733 }
724734
725- case 'batch-toggle-handles' : {
735+ case BLOCKS_OPERATIONS . BATCH_TOGGLE_HANDLES : {
726736 const { blockIds } = payload
727737 if ( ! Array . isArray ( blockIds ) || blockIds . length === 0 ) {
728738 return
@@ -749,7 +759,7 @@ async function handleBlocksOperationTx(
749759 break
750760 }
751761
752- case 'batch-update-parent' : {
762+ case BLOCKS_OPERATIONS . BATCH_UPDATE_PARENT : {
753763 const { updates } = payload
754764 if ( ! Array . isArray ( updates ) || updates . length === 0 ) {
755765 return
@@ -829,7 +839,7 @@ async function handleBlocksOperationTx(
829839
830840async function handleEdgeOperationTx ( tx : any , workflowId : string , operation : string , payload : any ) {
831841 switch ( operation ) {
832- case 'add' : {
842+ case EDGE_OPERATIONS . ADD : {
833843 // Validate required fields
834844 if ( ! payload . id || ! payload . source || ! payload . target ) {
835845 throw new Error ( 'Missing required fields for add edge operation' )
@@ -848,7 +858,7 @@ async function handleEdgeOperationTx(tx: any, workflowId: string, operation: str
848858 break
849859 }
850860
851- case 'remove' : {
861+ case EDGE_OPERATIONS . REMOVE : {
852862 if ( ! payload . id ) {
853863 throw new Error ( 'Missing edge ID for remove operation' )
854864 }
@@ -879,7 +889,7 @@ async function handleEdgesOperationTx(
879889 payload : any
880890) {
881891 switch ( operation ) {
882- case 'batch-remove-edges' : {
892+ case EDGES_OPERATIONS . BATCH_REMOVE_EDGES : {
883893 const { ids } = payload
884894 if ( ! Array . isArray ( ids ) || ids . length === 0 ) {
885895 logger . debug ( 'No edge IDs provided for batch remove' )
@@ -896,7 +906,7 @@ async function handleEdgesOperationTx(
896906 break
897907 }
898908
899- case 'batch-add-edges' : {
909+ case EDGES_OPERATIONS . BATCH_ADD_EDGES : {
900910 const { edges } = payload
901911 if ( ! Array . isArray ( edges ) || edges . length === 0 ) {
902912 logger . debug ( 'No edges provided for batch add' )
@@ -933,7 +943,7 @@ async function handleSubflowOperationTx(
933943 payload : any
934944) {
935945 switch ( operation ) {
936- case 'update' : {
946+ case SUBFLOW_OPERATIONS . UPDATE : {
937947 if ( ! payload . id || ! payload . config ) {
938948 throw new Error ( 'Missing required fields for update subflow operation' )
939949 }
@@ -1060,7 +1070,7 @@ async function handleVariableOperationTx(
10601070 const currentVariables = ( workflowData [ 0 ] . variables as Record < string , any > ) || { }
10611071
10621072 switch ( operation ) {
1063- case 'add' : {
1073+ case VARIABLE_OPERATIONS . ADD : {
10641074 if ( ! payload . id || ! payload . name || payload . type === undefined ) {
10651075 throw new Error ( 'Missing required fields for add variable operation' )
10661076 }
@@ -1089,7 +1099,7 @@ async function handleVariableOperationTx(
10891099 break
10901100 }
10911101
1092- case 'remove' : {
1102+ case VARIABLE_OPERATIONS . REMOVE : {
10931103 if ( ! payload . variableId ) {
10941104 throw new Error ( 'Missing variable ID for remove operation' )
10951105 }
@@ -1123,7 +1133,7 @@ async function handleWorkflowOperationTx(
11231133 payload : any
11241134) {
11251135 switch ( operation ) {
1126- case 'replace-state' : {
1136+ case WORKFLOW_OPERATIONS . REPLACE_STATE : {
11271137 if ( ! payload . state ) {
11281138 throw new Error ( 'Missing state for replace-state operation' )
11291139 }
0 commit comments