{
  "name": "Intelligent Freight Order Distribution with WhatsApp Group Routing",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      },
      "id": "c59e736e-78d3-45e0-a087-849f2d027b48",
      "name": "Rendszeres megrendelés lekérdezés",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        288,
        96
      ]
    },
    {
      "parameters": {
        "url": "<__mockup_api_endpoint__>",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "0180a4d1-220f-4491-84ba-33a498602cbe",
      "name": "Mockup API - Megrendelések lekérése",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        432,
        400
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "orders",
        "options": {}
      },
      "id": "c6671add-ca5b-4371-a744-c0618ad240f1",
      "name": "Megrendelések szétbontása",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        576,
        160
      ]
    },
    {
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "<__PLACEHOLDER_VALUE__fuvarozok_foglaltsaga_table__>"
        },
        "returnAll": true
      },
      "id": "d2eea90d-03ea-4525-8458-dc9f633338fc",
      "name": "Fuvarozók foglaltsága - lekérdezés",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        784,
        160
      ]
    },
    {
      "parameters": {
        "text": "={{ $json }}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "Te egy fuvarszervező AI vagy. Elemezd a megrendelési adatokat, használd az útvonal API-t az optimális útvonal kiszámításához, és határozd meg a legjobb időpontokat. Add vissza a strukturált eredményt."
        }
      },
      "id": "c063c365-ce71-42e3-b5c2-a5de9173e90b",
      "name": "AI Agent - Útvonal optimalizálás",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1072,
        160
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "gpt-4o"
        },
        "builtInTools": {},
        "options": {
          "temperature": 0.2
        }
      },
      "id": "7f720246-616e-40e2-a8b3-c18131769485",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        800,
        400
      ]
    },
    {
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n\t\"type\": \"object\",\n\t\"properties\": {\n\t\t\"optimizedRoute\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n\t\t\"estimatedTime\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n\t\t\"pickupTime\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n\t\t\"deliveryTime\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n\t\t\"distance\": {\n\t\t\t\"type\": \"number\"\n\t\t}\n\t}\n}"
      },
      "id": "e41fbb27-89d4-40ce-9e53-0e404d0fe01f",
      "name": "Strukturált kimenet - Optimalizált adatok",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        1248,
        400
      ]
    },
    {
      "parameters": {
        "toolDescription": "Calculates optimal route between pickup and delivery locations. Requires origin and destination addresses.",
        "method": "POST",
        "url": "<__PLACEHOLDER_VALUE__route_optimization_api__>",
        "options": {}
      },
      "id": "5579bc5c-03fd-4114-bb9b-2883ca59ad77",
      "name": "Útvonal API Tool",
      "type": "n8n-nodes-base.httpRequestTool",
      "typeVersion": 4.4,
      "position": [
        1040,
        400
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "id-1",
              "name": "messageText",
              "value": "=🚚 Új fuvarszolgálatos megrendelés\n\n📦 Megrendelés: {{ $('Megrendelések szétbontása').item.json.orderNumber }}\n📍 Felvétel: {{ $('Megrendelések szétbontása').item.json.pickupAddress }}\n📍 Szállítás: {{ $('Megrendelések szétbontása').item.json.deliveryAddress }}\n\n🛣️ Optimális útvonal: {{ $('AI Agent - Útvonal optimalizálás').item.json.output.optimizedRoute }}\n⏱️ Becsült idő: {{ $('AI Agent - Útvonal optimalizálás').item.json.output.estimatedTime }}\n🕐 Felvételi idő: {{ $('AI Agent - Útvonal optimalizálás').item.json.output.pickupTime }}\n🕐 Szállítási idő: {{ $('AI Agent - Útvonal optimalizálás').item.json.output.deliveryTime }}\n📏 Távolság: {{ $('AI Agent - Útvonal optimalizálás').item.json.output.distance }} km",
              "type": "string"
            },
            {
              "id": "id-2",
              "name": "carrierGroupId",
              "value": "={{ $('Fuvarozók foglaltsága - lekérdezés').item.json.whatsappGroupId }}",
              "type": "string"
            },
            {
              "id": "id-3",
              "name": "carrierName",
              "value": "={{ $('Fuvarozók foglaltsága - lekérdezés').item.json.carrierName }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "429fcb11-7872-4259-a77e-d8a1147b4ba0",
      "name": "WhatsApp üzenet előkészítése",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1600,
        144
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "id-1",
              "leftValue": "={{ $('Fuvarozók foglaltsága - lekérdezés').item.json.status }}",
              "rightValue": "available",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "cda7610a-6a9e-4bef-9dc0-8feb92a5395a",
      "name": "Fuvarozó elérhető?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1376,
        160
      ]
    },
    {
      "parameters": {
        "operation": "send",
        "phoneNumberId": "<__PLACEHOLDER_VALUE__whatsapp_phone_number_id__>",
        "recipientPhoneNumber": "={{ $json.carrierGroupId }}",
        "textBody": "={{ $json.messageText }}",
        "additionalFields": {}
      },
      "id": "9b7bc79b-186a-4eb5-9412-e9089f19a737",
      "name": "WhatsApp - Megrendelés küldése",
      "type": "n8n-nodes-base.whatsApp",
      "typeVersion": 1.1,
      "position": [
        1776,
        320
      ],
      "webhookId": "307e8ddd-b04d-4375-939d-cd385d2b3dfc"
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "mode": "id",
          "value": "<__PLACEHOLDER_VALUE__fuvarozok_foglaltsaga_table__>"
        },
        "filters": {
          "conditions": [
            {
              "keyValue": "={{ $json.carrierId }}"
            }
          ]
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": null
        },
        "options": {}
      },
      "id": "203419e2-6188-41ef-997f-a5400ba572c5",
      "name": "Fuvarozó státusz frissítése",
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        2176,
        320
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "id-1",
              "name": "status",
              "value": "busy",
              "type": "string"
            },
            {
              "id": "id-2",
              "name": "carrierId",
              "value": "={{ $('Fuvarozók foglaltsága - lekérdezés').item.json.id }}",
              "type": "string"
            },
            {
              "id": "id-3",
              "name": "lastAssignedOrder",
              "value": "={{ $('Megrendelések szétbontása').item.json.orderNumber }}",
              "type": "string"
            },
            {
              "id": "id-4",
              "name": "assignedAt",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "f4a2ceda-ee74-4c6d-8fb7-4c453e7bbc31",
      "name": "Státusz előkészítése",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1968,
        320
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Rendszeres megrendelés lekérdezés": {
      "main": [
        [
          {
            "node": "Mockup API - Megrendelések lekérése",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mockup API - Megrendelések lekérése": {
      "main": [
        [
          {
            "node": "Megrendelések szétbontása",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Megrendelések szétbontása": {
      "main": [
        [
          {
            "node": "Fuvarozók foglaltsága - lekérdezés",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent - Útvonal optimalizálás",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Strukturált kimenet - Optimalizált adatok": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent - Útvonal optimalizálás",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Útvonal API Tool": {
      "ai_tool": [
        [
          {
            "node": "AI Agent - Útvonal optimalizálás",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Fuvarozók foglaltsága - lekérdezés": {
      "main": [
        [
          {
            "node": "AI Agent - Útvonal optimalizálás",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent - Útvonal optimalizálás": {
      "main": [
        [
          {
            "node": "Fuvarozó elérhető?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fuvarozó elérhető?": {
      "main": [
        [
          {
            "node": "WhatsApp üzenet előkészítése",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "WhatsApp üzenet előkészítése": {
      "main": [
        [
          {
            "node": "WhatsApp - Megrendelés küldése",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "WhatsApp - Megrendelés küldése": {
      "main": [
        [
          {
            "node": "Státusz előkészítése",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Státusz előkészítése": {
      "main": [
        [
          {
            "node": "Fuvarozó státusz frissítése",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "3b4495e0-700a-4076-895a-1a182eadaf17",
  "meta": {
    "instanceId": "1ba0e779608fabcd69faca382101ec17bf81e8d793716f2454ee93ae23331a22"
  },
  "id": "56xxNerphxjrKXKG",
  "tags": []
}