{
  "format": "janus-integrations-v1",
  "definitions": [
    {
      "id": "sap",
      "content": {
        "name": "SAP S/4 goods receipt",
        "kind": "http",
        "endpoint": "https://sap-gw.danutek.local/sap/opu/odata/sap/API_MATERIAL_DOCUMENT_SRV/A_MaterialDocumentHeader",
        "method": "POST",
        "body": "{\"GoodsMovementCode\":\"01\",\"to_MaterialDocumentItem\":[{\"Material\":\"${partNumber}\",\"QuantityInEntryUnit\":\"${quantity}\",\"Plant\":\"DN02\",\"StorageLocation\":\"SMT2\"}],\"MaterialDocumentHeaderText\":\"Janus ${reelId}\"}",
        "headers": [
          {
            "name": "Content-Type",
            "value": "application/json"
          },
          {
            "name": "Idempotency-Key",
            "value": "${idempotencyKey}"
          },
          {
            "name": "Authorization",
            "value": "",
            "secret": true
          }
        ],
        "inputs": [
          {
            "name": "eventId",
            "type": "text"
          },
          {
            "name": "trigger",
            "type": "text"
          },
          {
            "name": "reelId",
            "type": "text"
          },
          {
            "name": "partNumber",
            "type": "text"
          },
          {
            "name": "quantity",
            "type": "text"
          },
          {
            "name": "bay",
            "type": "text"
          },
          {
            "name": "at",
            "type": "text"
          },
          {
            "name": "idempotencyKey",
            "type": "hash",
            "sources": [
              "eventId",
              "reelId"
            ]
          }
        ],
        "mappings": [
          {
            "from": "d.MaterialDocument",
            "to": "erpDocumentId",
            "rule": "external"
          },
          {
            "from": "d.MaterialDocumentYear",
            "to": "erpDocumentYear",
            "rule": "external"
          }
        ],
        "success": {
          "field": "d.MaterialDocument",
          "op": "exists",
          "value": ""
        }
      }
    },
    {
      "id": "counter",
      "content": {
        "name": "Line counter events",
        "kind": "http",
        "endpoint": "https://10.20.0.5:9443/janus/events",
        "method": "POST",
        "body": "{\"specversion\":\"1.0\",\"type\":\"janus.${trigger}\",\"id\":\"${eventId}\",\"source\":\"janus-smt-line2\",\"data\":{\"reel\":\"${reelId}\",\"bay\":\"${bay}\",\"quantity\":\"${quantity}\",\"sequence\":${sequence}}}",
        "headers": [
          {
            "name": "Content-Type",
            "value": "application/cloudevents+json"
          },
          {
            "name": "Webhook-Signature",
            "value": "",
            "secret": true
          }
        ],
        "inputs": [
          {
            "name": "eventId",
            "type": "text"
          },
          {
            "name": "trigger",
            "type": "text"
          },
          {
            "name": "reelId",
            "type": "text"
          },
          {
            "name": "partNumber",
            "type": "text"
          },
          {
            "name": "quantity",
            "type": "text"
          },
          {
            "name": "bay",
            "type": "text"
          },
          {
            "name": "at",
            "type": "text"
          },
          {
            "name": "sequence",
            "type": "running"
          }
        ],
        "mappings": [],
        "success": {
          "field": "status",
          "op": "starts-with",
          "value": "accepted"
        }
      }
    },
    {
      "id": "mes-events",
      "content": {
        "name": "MES events",
        "kind": "http",
        "endpoint": "https://mes.danutek.local/janus/events",
        "method": "POST",
        "body": "{\"deliveryId\":\"${deliveryId}\",\"sequence\":${sequence},\"sentAt\":\"${sentAt}\",\"event\":\"${eventId}\",\"trigger\":\"${trigger}\",\"reel\":\"${reelId}\",\"part\":\"${partNumber}\",\"digest\":\"${digest}\"}",
        "headers": [
          {
            "name": "Content-Type",
            "value": "application/json"
          }
        ],
        "inputs": [
          {
            "name": "eventId",
            "type": "text"
          },
          {
            "name": "trigger",
            "type": "text"
          },
          {
            "name": "reelId",
            "type": "text"
          },
          {
            "name": "partNumber",
            "type": "text"
          },
          {
            "name": "quantity",
            "type": "text"
          },
          {
            "name": "bay",
            "type": "text"
          },
          {
            "name": "at",
            "type": "text"
          },
          {
            "name": "deliveryId",
            "type": "uuid"
          },
          {
            "name": "sequence",
            "type": "running"
          },
          {
            "name": "sentAt",
            "type": "datetime",
            "format": "YYYY-MM-DD HH:mm:ss"
          },
          {
            "name": "digest",
            "type": "hash",
            "sources": [
              "eventId",
              "reelId",
              "sequence"
            ]
          }
        ],
        "mappings": [
          {
            "from": "receipt",
            "to": "mesReceiptId",
            "rule": "external"
          }
        ],
        "success": {
          "field": "result",
          "op": "eq",
          "value": "OK",
          "messageField": "message"
        }
      }
    },
    {
      "id": "lookup",
      "content": {
        "name": "MES item lookup",
        "kind": "sql",
        "endpoint": "MES_PROD",
        "method": "",
        "body": "",
        "headers": [],
        "inputs": [
          {
            "name": "reelId",
            "type": "text"
          }
        ],
        "mappings": [
          {
            "from": "ReelId",
            "to": "reelId",
            "rule": "must-match"
          },
          {
            "from": "ItemNo",
            "to": "partNumber",
            "rule": "must-match"
          },
          {
            "from": "Qty",
            "to": "quantity",
            "rule": "external"
          },
          {
            "from": "LotNo",
            "to": "lot",
            "rule": "label-first"
          }
        ],
        "success": {
          "field": "ItemNo",
          "op": "exists",
          "value": ""
        },
        "sql": {
          "server": "mes-sql01",
          "database": "MES_PROD",
          "account": "janus_mes_read",
          "statement": "SELECT ReelId, ItemNo, Qty, LotNo FROM dbo.ReelLabel WHERE ReelId = @reelId",
          "readOnlyProfile": true
        }
      }
    },
    {
      "id": "mes-feed",
      "content": {
        "name": "MES work-order feed",
        "kind": "sql",
        "endpoint": "MES_PROD",
        "method": "",
        "body": "",
        "headers": [],
        "inputs": [
          {
            "name": "cursor",
            "type": "number"
          }
        ],
        "mappings": [
          {
            "from": "WorkOrder",
            "to": "workOrder",
            "rule": "external"
          },
          {
            "from": "PartNo",
            "to": "item",
            "rule": "external"
          },
          {
            "from": "RequiredQty",
            "to": "required",
            "rule": "external"
          }
        ],
        "success": {
          "field": "WorkOrder",
          "op": "exists",
          "value": ""
        },
        "sql": {
          "server": "mes-sql01",
          "database": "MES_PROD",
          "account": "janus_mes_read",
          "statement": "SELECT WorkOrder, PartNo, RequiredQty, ChangeSeq FROM dbo.WorkOrderMaterial WHERE ChangeSeq > @cursor",
          "readOnlyProfile": true
        }
      }
    }
  ]
}