( tx: any, workflowId: string, operation: string, payload: any )
| 607 | } |
| 608 | |
| 609 | async function handleBlocksOperationTx( |
| 610 | tx: any, |
| 611 | workflowId: string, |
| 612 | operation: string, |
| 613 | payload: any |
| 614 | ) { |
| 615 | switch (operation) { |
| 616 | case BLOCKS_OPERATIONS.BATCH_UPDATE_POSITIONS: { |
| 617 | const { updates } = payload |
| 618 | if (!Array.isArray(updates) || updates.length === 0) { |
| 619 | return |
| 620 | } |
| 621 | |
| 622 | for (const update of updates) { |
| 623 | const { id, position } = update |
| 624 | if (!id || !position) continue |
| 625 | |
| 626 | await tx |
| 627 | .update(workflowBlocks) |
| 628 | .set({ |
| 629 | positionX: position.x, |
| 630 | positionY: position.y, |
| 631 | }) |
| 632 | .where(and(eq(workflowBlocks.id, id), eq(workflowBlocks.workflowId, workflowId))) |
| 633 | } |
| 634 | break |
| 635 | } |
| 636 | |
| 637 | case BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS: { |
| 638 | const { blocks, edges, loops, parallels, subBlockValues } = payload |
| 639 | |
| 640 | logger.info(`Batch adding blocks to workflow ${workflowId}`, { |
| 641 | blockCount: blocks?.length || 0, |
| 642 | edgeCount: edges?.length || 0, |
| 643 | loopCount: Object.keys(loops || {}).length, |
| 644 | parallelCount: Object.keys(parallels || {}).length, |
| 645 | }) |
| 646 | |
| 647 | if (blocks && blocks.length > 0) { |
| 648 | // Fetch existing blocks to check for locked parents |
| 649 | const existingBlocks = await tx |
| 650 | .select({ id: workflowBlocks.id, locked: workflowBlocks.locked }) |
| 651 | .from(workflowBlocks) |
| 652 | .where(eq(workflowBlocks.workflowId, workflowId)) |
| 653 | |
| 654 | type ExistingBlockRecord = (typeof existingBlocks)[number] |
| 655 | const lockedParentIds = new Set( |
| 656 | existingBlocks |
| 657 | .filter((b: ExistingBlockRecord) => b.locked) |
| 658 | .map((b: ExistingBlockRecord) => b.id) |
| 659 | ) |
| 660 | |
| 661 | // Filter out blocks being added to locked parents |
| 662 | const allowedBlocks = (blocks as Array<Record<string, unknown>>).filter((block) => { |
| 663 | const parentId = (block.data as Record<string, unknown> | null)?.parentId as |
| 664 | | string |
| 665 | | undefined |
| 666 | if (parentId && lockedParentIds.has(parentId)) { |
no test coverage detected