{
  "format": "rpai-station-workflow",
  "version": 1,
  "name": "Products report (JSON to CSV)",
  "variables": [
    {
      "id": "var-folder",
      "name": "folder",
      "type": "string",
      "value": "~/Documents/Station Academy"
    }
  ],
  "activities": [
    {
      "id": "academy-001",
      "type": "comment",
      "name": "About this workflow",
      "properties": {
        "text": "Station Academy intermediate project. Turns products.json into a structured CSV report with a status column. Put products.json into the practice folder first."
      }
    },
    {
      "id": "academy-002",
      "type": "file_operation",
      "name": "Make sure the practice folder exists",
      "properties": {
        "operation": "create_folder",
        "sourcePath": "{{folder}}"
      }
    },
    {
      "id": "academy-003",
      "type": "json_operation",
      "name": "Read the products",
      "properties": {
        "operation": "read",
        "filePath": "{{folder}}/products.json",
        "variable": "products"
      }
    },
    {
      "id": "academy-004",
      "type": "parse_data",
      "name": "The list of items",
      "properties": {
        "format": "json",
        "sourceVariable": "products",
        "query": "$.items",
        "saveToVariable": "items"
      }
    },
    {
      "id": "academy-005",
      "type": "datatable_create",
      "name": "Empty report table",
      "properties": {
        "columns": "SKU,Name,Price,Stock,Status",
        "saveToVariable": "report"
      }
    },
    {
      "id": "academy-006",
      "type": "set_variable",
      "name": "Products to reorder",
      "properties": {
        "variableName": "reorder",
        "valueType": "static",
        "value": "0"
      }
    },
    {
      "id": "academy-016",
      "type": "loop",
      "name": "For each product",
      "properties": {
        "loopType": "collection",
        "collection": "items",
        "itemVariable": "item"
      },
      "children": [
        {
          "id": "academy-007",
          "type": "dict_get_key",
          "name": "SKU",
          "properties": {
            "dictVariable": "item",
            "key": "sku",
            "saveToVariable": "sku"
          }
        },
        {
          "id": "academy-008",
          "type": "dict_get_key",
          "name": "Name",
          "properties": {
            "dictVariable": "item",
            "key": "name",
            "saveToVariable": "pname"
          }
        },
        {
          "id": "academy-009",
          "type": "dict_get_key",
          "name": "Price",
          "properties": {
            "dictVariable": "item",
            "key": "price",
            "saveToVariable": "price"
          }
        },
        {
          "id": "academy-010",
          "type": "dict_get_key",
          "name": "Stock",
          "properties": {
            "dictVariable": "item",
            "key": "stock",
            "saveToVariable": "stock"
          }
        },
        {
          "id": "academy-014",
          "type": "if",
          "name": "Out of stock?",
          "properties": {
            "variable": "stock",
            "operator": "equals",
            "value": "0"
          },
          "thenBranch": [
            {
              "id": "academy-011",
              "type": "set_variable",
              "name": "Status reorder",
              "properties": {
                "variableName": "status",
                "valueType": "static",
                "value": "Reorder"
              }
            },
            {
              "id": "academy-012",
              "type": "math_operation",
              "name": "Count it",
              "properties": {
                "operation": "add",
                "operand1": "reorder",
                "operand2": "1",
                "saveToVariable": "reorder"
              }
            }
          ],
          "elseBranch": [
            {
              "id": "academy-013",
              "type": "set_variable",
              "name": "Status OK",
              "properties": {
                "variableName": "status",
                "valueType": "static",
                "value": "OK"
              }
            }
          ]
        },
        {
          "id": "academy-015",
          "type": "datatable_add_row",
          "name": "Add a report row",
          "properties": {
            "tableVariable": "report",
            "rowData": "{\"SKU\": \"{{sku}}\", \"Name\": \"{{pname}}\", \"Price\": \"{{price}}\", \"Stock\": \"{{stock}}\", \"Status\": \"{{status}}\"}"
          }
        }
      ]
    },
    {
      "id": "academy-017",
      "type": "datatable_sort",
      "name": "Sort by name",
      "properties": {
        "tableVariable": "report",
        "column": "Name",
        "direction": "asc"
      }
    },
    {
      "id": "academy-018",
      "type": "csv_operation",
      "name": "Write the report",
      "properties": {
        "operation": "write",
        "filePath": "{{folder}}/products-report.csv",
        "delimiter": ",",
        "variable": "report"
      }
    },
    {
      "id": "academy-019",
      "type": "log",
      "name": "Result",
      "properties": {
        "message": "Report written: {{reorder}} product(s) to reorder.",
        "level": "info"
      }
    }
  ]
}
