wwf
2025-05-20 938c3e5a587ce950a94964ea509b9e7f8834dfae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import type { InputForm } from './type'
import { InputVarType } from '@/app/components/workflow/types'
import { getProcessedFiles } from '@/app/components/base/file-uploader/utils'
 
export const processOpeningStatement = (openingStatement: string, inputs: Record<string, any>, inputsForm: InputForm[]) => {
  if (!openingStatement)
    return openingStatement
 
  return openingStatement.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
    const name = inputs[key]
    if (name) { // has set value
      return name
    }
 
    const valueObj = inputsForm.find(v => v.variable === key)
    return valueObj ? `{{${valueObj.label}}}` : match
  })
}
 
export const processInputFileFromServer = (fileItem: Record<string, any>) => {
  return {
    type: fileItem.type,
    transfer_method: fileItem.transfer_method,
    url: fileItem.remote_url,
    upload_file_id: fileItem.related_id,
  }
}
 
export const getProcessedInputs = (inputs: Record<string, any>, inputsForm: InputForm[]) => {
  const processedInputs = { ...inputs }
 
  inputsForm.forEach((item) => {
    const inputValue = inputs[item.variable]
    if (!inputValue)
      return
 
    if (item.type === InputVarType.singleFile) {
      if ('transfer_method' in inputValue)
        processedInputs[item.variable] = processInputFileFromServer(inputValue)
      else
        processedInputs[item.variable] = getProcessedFiles([inputValue])[0]
    }
    else if (item.type === InputVarType.multiFiles) {
      if ('transfer_method' in inputValue[0])
        processedInputs[item.variable] = inputValue.map(processInputFileFromServer)
      else
        processedInputs[item.variable] = getProcessedFiles(inputValue)
    }
  })
 
  return processedInputs
}