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

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

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

schedules:
  - cron: "*/30 * * * *"
    displayName: "{{displayName}} - Every 30 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: "tracking/inbound"
          pattern: "*.json"
          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?"

          # TODO: Parse downloaded content and map to tracking events
          - task: "Utilities/SetVariable@1"
            name: ParseContent
            inputs:
              variables:
                - name: trackingEvents
                  value: []

          - task: "Order/Import@1"
            name: ImportTrackingEvents
            inputs:
              orders:
                - trackingNumber: "{{ varTrackingNumber }}"
                  orderType: "ParcelShipment"
                  trackingEvents: "{{ trackingEvents }}"
              options:
                orderMatchByFields:
                  - "trackingNumber"
                trackingEventMatchByFields:
                  - "eventDate"
                  - "customValues.eventType"
                skipIfExists: true
                createEventDefinitions: true

          - task: "FileTransfer/MoveFile@1"
            name: SftpMoveFile
            inputs:
              connection: "{{ FtpImport?.SftpConnect?.connection? }}"
              sourcePath: "{{ item.path }}"
              destinationPath:
                expression: "format('tracking/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
