Skip to content

Commit aca3b8b

Browse files
committed
fix(batch-add): on batch add persist subblock values
1 parent 468ec2e commit aca3b8b

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

apps/sim/socket/database/operations.ts

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,33 @@ function isSubflowBlockType(blockType: string): blockType is SubflowType {
7474
return Object.values(SubflowType).includes(blockType as SubflowType)
7575
}
7676

77+
function mergeSubBlockValues(
78+
subBlocks: Record<string, unknown> | undefined,
79+
values: Record<string, unknown> | undefined
80+
): Record<string, unknown> {
81+
const merged = { ...(subBlocks || {}) } as Record<string, any>
82+
83+
if (!values) return merged
84+
85+
Object.entries(values).forEach(([subBlockId, value]) => {
86+
if (merged[subBlockId] && typeof merged[subBlockId] === 'object') {
87+
merged[subBlockId] = {
88+
...(merged[subBlockId] as Record<string, unknown>),
89+
value,
90+
}
91+
return
92+
}
93+
94+
merged[subBlockId] = {
95+
id: subBlockId,
96+
type: 'short-input',
97+
value,
98+
}
99+
})
100+
101+
return merged
102+
}
103+
77104
export async function updateSubflowNodeList(dbOrTx: any, workflowId: string, parentId: string) {
78105
try {
79106
// Get all child blocks of this parent
@@ -455,7 +482,7 @@ async function handleBlocksOperationTx(
455482
}
456483

457484
case BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS: {
458-
const { blocks, edges, loops, parallels } = payload
485+
const { blocks, edges, loops, parallels, subBlockValues } = payload
459486

460487
logger.info(`Batch adding blocks to workflow ${workflowId}`, {
461488
blockCount: blocks?.length || 0,
@@ -465,22 +492,30 @@ async function handleBlocksOperationTx(
465492
})
466493

467494
if (blocks && blocks.length > 0) {
468-
const blockValues = blocks.map((block: Record<string, unknown>) => ({
469-
id: block.id as string,
470-
workflowId,
471-
type: block.type as string,
472-
name: block.name as string,
473-
positionX: (block.position as { x: number; y: number }).x,
474-
positionY: (block.position as { x: number; y: number }).y,
475-
data: (block.data as Record<string, unknown>) || {},
476-
subBlocks: (block.subBlocks as Record<string, unknown>) || {},
477-
outputs: (block.outputs as Record<string, unknown>) || {},
478-
enabled: (block.enabled as boolean) ?? true,
479-
horizontalHandles: (block.horizontalHandles as boolean) ?? true,
480-
advancedMode: (block.advancedMode as boolean) ?? false,
481-
triggerMode: (block.triggerMode as boolean) ?? false,
482-
height: (block.height as number) || 0,
483-
}))
495+
const blockValues = blocks.map((block: Record<string, unknown>) => {
496+
const blockId = block.id as string
497+
const mergedSubBlocks = mergeSubBlockValues(
498+
block.subBlocks as Record<string, unknown>,
499+
subBlockValues?.[blockId]
500+
)
501+
502+
return {
503+
id: blockId,
504+
workflowId,
505+
type: block.type as string,
506+
name: block.name as string,
507+
positionX: (block.position as { x: number; y: number }).x,
508+
positionY: (block.position as { x: number; y: number }).y,
509+
data: (block.data as Record<string, unknown>) || {},
510+
subBlocks: mergedSubBlocks,
511+
outputs: (block.outputs as Record<string, unknown>) || {},
512+
enabled: (block.enabled as boolean) ?? true,
513+
horizontalHandles: (block.horizontalHandles as boolean) ?? true,
514+
advancedMode: (block.advancedMode as boolean) ?? false,
515+
triggerMode: (block.triggerMode as boolean) ?? false,
516+
height: (block.height as number) || 0,
517+
}
518+
})
484519

485520
await tx.insert(workflowBlocks).values(blockValues)
486521

0 commit comments

Comments
 (0)