| 
									
										
										
										
											2025-05-15 22:39:35 +08:00
										 |  |  | import type { NodeDefault } from '../../types' | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | import { type AssignerNodeType, WriteMode } from './types' | 
					
						
							| 
									
										
										
										
											2025-04-24 16:29:58 +08:00
										 |  |  | import { genNodeMetaData } from '@/app/components/workflow/utils' | 
					
						
							|  |  |  | import { BlockEnum } from '@/app/components/workflow/types' | 
					
						
							|  |  |  | import { BlockClassificationEnum } from '@/app/components/workflow/block-selector/types' | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | const i18nPrefix = 'workflow.errorMsg' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-29 16:11:54 +08:00
										 |  |  | const metaData = genNodeMetaData({ | 
					
						
							|  |  |  |   classification: BlockClassificationEnum.Transform, | 
					
						
							|  |  |  |   sort: 5, | 
					
						
							|  |  |  |   type: BlockEnum.Assigner, | 
					
						
							|  |  |  |   helpLinkUri: 'variable-assigner', | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | const nodeDefault: NodeDefault<AssignerNodeType> = { | 
					
						
							| 
									
										
										
										
											2025-04-29 16:11:54 +08:00
										 |  |  |   metaData, | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |   defaultValue: { | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |     version: '2', | 
					
						
							|  |  |  |     items: [], | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |   }, | 
					
						
							|  |  |  |   checkValid(payload: AssignerNodeType, t: any) { | 
					
						
							|  |  |  |     let errorMessages = '' | 
					
						
							|  |  |  |     const { | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |       items: operationItems, | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  |     } = payload | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |     operationItems?.forEach((value) => { | 
					
						
							|  |  |  |       if (!errorMessages && !value.variable_selector?.length) | 
					
						
							|  |  |  |         errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.assignedVariable') }) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-30 15:50:00 +08:00
										 |  |  |       if (!errorMessages && value.operation !== WriteMode.clear && value.operation !== WriteMode.removeFirst && value.operation !== WriteMode.removeLast) { | 
					
						
							| 
									
										
										
										
											2024-12-04 15:46:54 +08:00
										 |  |  |         if (value.operation === WriteMode.set || value.operation === WriteMode.increment | 
					
						
							|  |  |  |           || value.operation === WriteMode.decrement || value.operation === WriteMode.multiply | 
					
						
							|  |  |  |           || value.operation === WriteMode.divide) { | 
					
						
							| 
									
										
										
										
											2024-12-03 13:56:40 +08:00
										 |  |  |           if (!value.value && typeof value.value !== 'number') | 
					
						
							|  |  |  |             errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (!value.value?.length) { | 
					
						
							|  |  |  |           errorMessages = t(`${i18nPrefix}.fieldRequired`, { field: t('workflow.nodes.assigner.variable') }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2024-08-13 14:44:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       isValid: !errorMessages, | 
					
						
							|  |  |  |       errorMessage: errorMessages, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default nodeDefault |