( parentWorkflowId: string, parentBlockId: string, currentInputMapping: any, childWorkflowBlocks: Record<string, any> )
| 34 | * @returns The cleaned inputMapping (only different if cleanup was needed) |
| 35 | */ |
| 36 | export async function lazyCleanupInputMapping( |
| 37 | parentWorkflowId: string, |
| 38 | parentBlockId: string, |
| 39 | currentInputMapping: any, |
| 40 | childWorkflowBlocks: Record<string, any> |
| 41 | ): Promise<any> { |
| 42 | try { |
| 43 | if ( |
| 44 | !currentInputMapping || |
| 45 | typeof currentInputMapping !== 'object' || |
| 46 | Array.isArray(currentInputMapping) |
| 47 | ) { |
| 48 | return currentInputMapping |
| 49 | } |
| 50 | |
| 51 | const validFieldNames = extractValidInputFieldNames(childWorkflowBlocks) |
| 52 | |
| 53 | if (!validFieldNames || validFieldNames.size === 0) { |
| 54 | logger.debug('Child workflow has no inputFormat fields, skipping cleanup') |
| 55 | return currentInputMapping |
| 56 | } |
| 57 | |
| 58 | const orphanedFields: string[] = [] |
| 59 | for (const fieldName of Object.keys(currentInputMapping)) { |
| 60 | if (!validFieldNames.has(fieldName)) { |
| 61 | orphanedFields.push(fieldName) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (orphanedFields.length === 0) { |
| 66 | return currentInputMapping |
| 67 | } |
| 68 | |
| 69 | const cleanedMapping: Record<string, any> = {} |
| 70 | for (const [fieldName, fieldValue] of Object.entries(currentInputMapping)) { |
| 71 | if (validFieldNames.has(fieldName)) { |
| 72 | cleanedMapping[fieldName] = fieldValue |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | logger.info( |
| 77 | `Lazy cleanup: Removing ${orphanedFields.length} orphaned field(s) from inputMapping in workflow ${parentWorkflowId}, block ${parentBlockId}: ${orphanedFields.join(', ')}` |
| 78 | ) |
| 79 | |
| 80 | persistCleanedMapping(parentWorkflowId, parentBlockId, cleanedMapping).catch((error) => { |
| 81 | logger.error('Failed to persist cleaned inputMapping:', error) |
| 82 | }) |
| 83 | |
| 84 | return cleanedMapping |
| 85 | } catch (error) { |
| 86 | logger.error('Error in lazy cleanup:', error) |
| 87 | return currentInputMapping |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Persist the cleaned inputMapping to the database |
no test coverage detected