# {{displayName}} Workflow
# Generated by cxtms create workflow --template ftp-edi

workflow:
  workflowId: "{{uuid}}"
  name: "{{displayName}}"
  description: "{{displayName}} - Import orders from FTP via EDI"
  version: "1.0"
  executionMode: Async
  logLevel: Information
  isActive: true
  enableAudit: true
  filePath: "{{fileName}}"
  runAs: workflowAgent
  concurrency:
    waitTime: 0
  tags:
    - "generated"
    - "ftp"
    - "edi"

variables:
  - name: ftpConfig
    fromConfig:
      configName: "apps.myApp.ftpConfig"

schedules:
  - cron: "0/10 * * * *"
    displayName: "{{displayName}} - Every 10 minutes"

activities:
  - name: FtpImport
    conditions: "[ftpConfig?.schedulingEnabled?] = true"
    events:
      onActivityStarted:
        - task: "Utilities/Log@1"
          name: LogActivityStarted
          inputs:
            message: "Activity FtpImport started"
            level: Information
      onActivityCompleted:
        - task: "Utilities/Log@1"
          name: LogActivityCompleted
          inputs:
            message: "Activity FtpImport completed"
            level: Information
      onActivityFailed:
        - task: "Utilities/Log@1"
          name: LogActivityFailed
          inputs:
            message: "Activity FtpImport failed: {{exception.message}}"
            level: Error
    steps:
      - task: "FileTransfer/Connect@1"
        name: SftpConnect
        inputs:
          host: "{{ ftpConfig?.host? }}"
          protocol: "{{ ftpConfig?.protocol? }}"
          username: "{{ ftpConfig?.username? }}"
          authentication:
            type: "password"
            password: "{{ ftpConfig?.password? }}"
          options:
            timeout: 30
        outputs:
          - name: connection
            mapping: "connection?"
          - name: connectionId
            mapping: "connectionId?"

      - task: "FileTransfer/ListFiles@1"
        name: SftpListFiles
        inputs:
          connection: "{{ FtpImport?.SftpConnect?.connection? }}"
          directory: "inbound"
          recursive: false
          includeDirectories: false
          sortBy: "name"
          sortOrder: "asc"
        outputs:
          - name: files
            mapping: "files?"
          - name: fileCount
            mapping: "fileCount?"

      - task: "foreach"
        name: ProcessFiles
        collection: "FtpImport?.SftpListFiles?.files?"
        steps:
          - task: "FileTransfer/DownloadFile@1"
            name: SftpDownloadFile
            inputs:
              connection: "{{ FtpImport?.SftpConnect?.connection? }}"
              remotePath: "{{ item.path }}"
            outputs:
              - name: fileInfo
                mapping: "fileInfo?"
              - name: content
                mapping: "content?"

          - task: "Workflow/Execute@1"
            name: ProcessEdi
            inputs:
              workflowName: "EDI Parse"
              workflowInputs:
                fileContent:
                  expression: "fromBase64([FtpImport?.SftpDownloadFile?.content?])"
            outputs:
              - name: parsedData
                mapping: "parsedObject?"

          # TODO: Map parsed EDI data to orders and import
          - task: "Order/Import@1"
            name: ImportOrders
            inputs:
              orders: "{{ orderData }}"
              options:
                orderMatchByFields:
                  - "orderNumber"
                commodityMatchByFields:
                  - "description"

          - task: "FileTransfer/MoveFile@1"
            name: SftpMoveFile
            inputs:
              connection: "{{ FtpImport?.SftpConnect?.connection? }}"
              sourcePath: "{{ item.path }}"
              destinationPath:
                expression: "format('processed/{0}-{1}', formatDate(now(), 'yyyyMMdd_HHmmss', 'en-US'), [item.name])"
              options:
                createDirectories: true
                overwrite: true

      - task: "FileTransfer/Disconnect@1"
        name: SftpDisconnect
        conditions: "isNullOrEmpty([FtpImport?.SftpConnect?.connection?]) = false"
        inputs:
          connection: "{{ FtpImport?.SftpConnect?.connection? }}"

events:
  onWorkflowStarted:
    - task: "Utilities/Log@1"
      name: LogWorkflowStarted
      inputs:
        message: "{{displayName}} workflow started"
        level: Information
  onWorkflowCompleted:
    - task: "Utilities/Log@1"
      name: LogWorkflowCompleted
      inputs:
        message: "{{displayName}} workflow completed successfully"
        level: Information
  onWorkflowFailed:
    - task: "Utilities/Log@1"
      name: LogWorkflowFailed
      inputs:
        message: "{{displayName}} workflow failed: {{exception.message}}"
        level: Error
