{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "sendTo": "=cofahik172@gta5hx.com",
        "subject": "New Order",
        "emailType": "text",
        "message": "={{ $json.message.content }}",
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        672,
        0
      ],
      "id": "8ac2dce2-38fc-4686-bdb0-e72d9b5c3e7b",
      "name": "Send a message",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n    \"success\": true,\n    \"orderId\": \"{{$node[\"PlaceOrder\"].json[\"body\"][\"outboundSupplierOrderId\"]}}\"\n}",
        "options": {
          "responseCode": 201
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1168,
        0
      ],
      "id": "151dac63-5a05-4d9c-85b7-6b4acdbbef78",
      "name": "Respond to Webhook"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "/place-order",
        "responseMode": "responseNode",
        "options": {
          "rawBody": true
        }
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -336,
        0
      ],
      "id": "b046854f-6a83-4dd1-a028-864d827264ac",
      "name": "PlaceOrder"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT \n    s.name as supplier_name,\n    s.email as supplier_email,\n    s.address as supplier_address,\n    oso.outbound_supplier_order_id,\n    oso.created_at,\n    oso.status,\n    osoli.outbound_supplier_order_line_item_id,\n    osoli.quantity,\n    osoli.price,\n    osoli.component,\n    osoli.delivery_days\nFROM suppliers s\nJOIN outbound_supplier_orders oso \n    ON oso.outbound_supplier_order_id = {{$json[\"body\"][\"outboundSupplierOrderId\"]}}\nJOIN outbound_supplier_order_line_items osoli \n    ON oso.outbound_supplier_order_id = osoli.outbound_supplier_order_id\nWHERE s.supplier_id = oso.supplier_id\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        -48,
        0
      ],
      "id": "1760d06c-a4d2-49d4-81c7-da293aa18938",
      "name": "Execute a SQL query",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst firstItem = items[0].json;\n\n// Pre-format the items text\nconst itemsText = items.map(item => {\n    const json = item.json;\n    return `- ${json.quantity}x ${json.product_name || 'Custom Service'} at ${json.price} EUR`;\n}).join('\\n');\n\nconst commonData = {\n    supplier_name: firstItem.supplier_name,\n    supplier_email: firstItem.supplier_email,\n    supplier_address: firstItem.supplier_address,\n    order_id: firstItem.order_id,\n    delivery_date: firstItem.delivery_date,\n    order_status: firstItem.order_status,\n    itemsText: itemsText  // Pre-formatted text\n};\n\nreturn commonData;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        144,
        0
      ],
      "id": "06180f32-ac8c-4a3d-a748-26ff18b41478",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE outbound_supplier_orders \nSET status = 'pending_supplier_confirmation'\nWHERE outbound_supplier_order_id = {{$node[\"PlaceOrder\"].json.body.outboundSupplierOrderId}}",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        928,
        0
      ],
      "id": "f5bc67ee-b086-49b9-af64-5a74e2ad6bec",
      "name": "Execute a SQL query1",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-5",
          "mode": "list",
          "cachedResultName": "GPT-5"
        },
        "messages": {
          "values": [
            {
              "content": "=supplier name: {{ $json.supplier_name }} \nsupplier_ordered_items: {{ $json.itemsText }} \n\nBased on the above data, generate a professional email body to place an order in german.\n\nOur name is Quacc, just have that in the footer.\n\nInclude a funny joke."
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        304,
        0
      ],
      "id": "2bcf6612-6fbe-4269-96ef-3ee32c5a2f03",
      "name": "Message a model",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Send a message": {
      "main": [
        [
          {
            "node": "Execute a SQL query1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PlaceOrder": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query1": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "301e7708-5777-4172-8084-b8a813f5bd68",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "e9s2CdwxEauNCRYi",
  "tags": []
}