@@ -1133,12 +1133,10 @@ async function handleBlocksOperationTx(
11331133async function handleEdgeOperationTx ( tx : any , workflowId : string , operation : string , payload : any ) {
11341134 switch ( operation ) {
11351135 case EDGE_OPERATIONS . ADD : {
1136- // Validate required fields
11371136 if ( ! payload . id || ! payload . source || ! payload . target ) {
11381137 throw new Error ( 'Missing required fields for add edge operation' )
11391138 }
11401139
1141- // Check if source or target blocks are protected (locked or inside locked parent)
11421140 const edgeBlocks = await tx
11431141 . select ( {
11441142 id : workflowBlocks . id ,
@@ -1158,6 +1156,36 @@ async function handleEdgeOperationTx(tx: any, workflowId: string, operation: str
11581156 edgeBlocks . map ( ( b : EdgeBlockRecord ) => [ b . id , b ] )
11591157 )
11601158
1159+ const parentIds = new Set < string > ( )
1160+ for ( const block of edgeBlocks ) {
1161+ const parentId = ( block . data as Record < string , unknown > | null ) ?. parentId as
1162+ | string
1163+ | undefined
1164+ if ( parentId && ! blocksById [ parentId ] ) {
1165+ parentIds . add ( parentId )
1166+ }
1167+ }
1168+
1169+ // Fetch parent blocks if needed
1170+ if ( parentIds . size > 0 ) {
1171+ const parentBlocks = await tx
1172+ . select ( {
1173+ id : workflowBlocks . id ,
1174+ locked : workflowBlocks . locked ,
1175+ data : workflowBlocks . data ,
1176+ } )
1177+ . from ( workflowBlocks )
1178+ . where (
1179+ and (
1180+ eq ( workflowBlocks . workflowId , workflowId ) ,
1181+ inArray ( workflowBlocks . id , Array . from ( parentIds ) )
1182+ )
1183+ )
1184+ for ( const b of parentBlocks ) {
1185+ blocksById [ b . id ] = b
1186+ }
1187+ }
1188+
11611189 const isBlockProtected = ( blockId : string ) : boolean => {
11621190 const block = blocksById [ blockId ]
11631191 if ( ! block ) return false
@@ -1226,6 +1254,37 @@ async function handleEdgeOperationTx(tx: any, workflowId: string, operation: str
12261254 connectedBlocks . map ( ( b : RemoveEdgeBlockRecord ) => [ b . id , b ] )
12271255 )
12281256
1257+ // Collect parent IDs that need to be fetched
1258+ const parentIds = new Set < string > ( )
1259+ for ( const block of connectedBlocks ) {
1260+ const parentId = ( block . data as Record < string , unknown > | null ) ?. parentId as
1261+ | string
1262+ | undefined
1263+ if ( parentId && ! blocksById [ parentId ] ) {
1264+ parentIds . add ( parentId )
1265+ }
1266+ }
1267+
1268+ // Fetch parent blocks if needed
1269+ if ( parentIds . size > 0 ) {
1270+ const parentBlocks = await tx
1271+ . select ( {
1272+ id : workflowBlocks . id ,
1273+ locked : workflowBlocks . locked ,
1274+ data : workflowBlocks . data ,
1275+ } )
1276+ . from ( workflowBlocks )
1277+ . where (
1278+ and (
1279+ eq ( workflowBlocks . workflowId , workflowId ) ,
1280+ inArray ( workflowBlocks . id , Array . from ( parentIds ) )
1281+ )
1282+ )
1283+ for ( const b of parentBlocks ) {
1284+ blocksById [ b . id ] = b
1285+ }
1286+ }
1287+
12291288 const isBlockProtected = ( blockId : string ) : boolean => {
12301289 const block = blocksById [ blockId ]
12311290 if ( ! block ) return false
@@ -1319,6 +1378,37 @@ async function handleEdgesOperationTx(
13191378 connectedBlocks . map ( ( b : EdgeBlockRecord ) => [ b . id , b ] )
13201379 )
13211380
1381+ // Collect parent IDs that need to be fetched
1382+ const parentIds = new Set < string > ( )
1383+ for ( const block of connectedBlocks ) {
1384+ const parentId = ( block . data as Record < string , unknown > | null ) ?. parentId as
1385+ | string
1386+ | undefined
1387+ if ( parentId && ! blocksById [ parentId ] ) {
1388+ parentIds . add ( parentId )
1389+ }
1390+ }
1391+
1392+ // Fetch parent blocks if needed
1393+ if ( parentIds . size > 0 ) {
1394+ const parentBlocks = await tx
1395+ . select ( {
1396+ id : workflowBlocks . id ,
1397+ locked : workflowBlocks . locked ,
1398+ data : workflowBlocks . data ,
1399+ } )
1400+ . from ( workflowBlocks )
1401+ . where (
1402+ and (
1403+ eq ( workflowBlocks . workflowId , workflowId ) ,
1404+ inArray ( workflowBlocks . id , Array . from ( parentIds ) )
1405+ )
1406+ )
1407+ for ( const b of parentBlocks ) {
1408+ blocksById [ b . id ] = b
1409+ }
1410+ }
1411+
13221412 const isBlockProtected = ( blockId : string ) : boolean => {
13231413 const block = blocksById [ blockId ]
13241414 if ( ! block ) return false
@@ -1388,6 +1478,37 @@ async function handleEdgesOperationTx(
13881478 connectedBlocks . map ( ( b : AddEdgeBlockRecord ) => [ b . id , b ] )
13891479 )
13901480
1481+ // Collect parent IDs that need to be fetched
1482+ const parentIds = new Set < string > ( )
1483+ for ( const block of connectedBlocks ) {
1484+ const parentId = ( block . data as Record < string , unknown > | null ) ?. parentId as
1485+ | string
1486+ | undefined
1487+ if ( parentId && ! blocksById [ parentId ] ) {
1488+ parentIds . add ( parentId )
1489+ }
1490+ }
1491+
1492+ // Fetch parent blocks if needed
1493+ if ( parentIds . size > 0 ) {
1494+ const parentBlocks = await tx
1495+ . select ( {
1496+ id : workflowBlocks . id ,
1497+ locked : workflowBlocks . locked ,
1498+ data : workflowBlocks . data ,
1499+ } )
1500+ . from ( workflowBlocks )
1501+ . where (
1502+ and (
1503+ eq ( workflowBlocks . workflowId , workflowId ) ,
1504+ inArray ( workflowBlocks . id , Array . from ( parentIds ) )
1505+ )
1506+ )
1507+ for ( const b of parentBlocks ) {
1508+ blocksById [ b . id ] = b
1509+ }
1510+ }
1511+
13911512 const isBlockProtected = ( blockId : string ) : boolean => {
13921513 const block = blocksById [ blockId ]
13931514 if ( ! block ) return false
0 commit comments