{
  "name": "Terramine pilot - CRM request to ERP draft",
  "nodes": [
    {
      "parameters": {},
      "id": "manual-pilot",
      "name": "Manual pilot",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "base_url",
              "value": "https://YOUR-WORKSPACE.eu.terraminedesk.ai"
            },
            {
              "name": "crm_deal_id",
              "value": "EXAMPLE-DEAL-42"
            },
            {
              "name": "quote_request_id",
              "value": "EXAMPLE-QUOTE-REQUEST-1"
            },
            {
              "name": "customer_name",
              "value": "Example Mining Company"
            },
            {
              "name": "currency",
              "value": "USD"
            },
            {
              "name": "lines_json",
              "value": "[{\"pn\":\"EXAMPLE-PART\",\"qty\":2}]"
            }
          ]
        },
        "options": {}
      },
      "id": "simulated-crm-request",
      "name": "Simulated CRM request",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{ $('Simulated CRM request').first().json.base_url + '/api/v1/customers' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ $('Simulated CRM request').first().json.customer_name }}"
            },
            {
              "name": "limit",
              "value": "200"
            },
            {
              "name": "stable_cursor",
              "value": "true"
            }
          ]
        },
        "options": {}
      },
      "id": "lookup-customer",
      "name": "Lookup customer",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const input = $('Simulated CRM request').first().json;\nconst page = $input.first().json;\nif (page.next_updated_since) throw new Error('Customer lookup is incomplete; resolve a unique customer before creating a draft.');\nconst matches = (page.data || []).filter(c => c.name.trim().toLowerCase() === input.customer_name.trim().toLowerCase());\nif (matches.length !== 1) throw new Error('Expected one exact existing customer; found ' + matches.length + '. Resolve the customer manually.');\nif (!input.quote_request_id || !input.quote_request_id.trim()) throw new Error('Supply a stable quote_request_id; use a new value only for a new quotation request.');\nconst lines = JSON.parse(input.lines_json);\nif (!Array.isArray(lines) || !lines.length) throw new Error('Supply at least one requested position.');\nreturn [{json: {base_url: input.base_url, idempotency_key: 'crm-quote:' + input.quote_request_id, body: {customer_id: matches[0].id, currency: input.currency, external_ref: input.crm_deal_id, client_ref: input.crm_deal_id, lines}}}];"
      },
      "id": "prepare-draft",
      "name": "Prepare draft",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $json.base_url + '/api/v1/orders' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "={{ $json.idempotency_key }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.body) }}",
        "options": {}
      },
      "id": "create-draft-request",
      "name": "Create draft request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        300
      ]
    }
  ],
  "connections": {
    "Manual pilot": {
      "main": [
        [
          {
            "node": "Simulated CRM request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simulated CRM request": {
      "main": [
        [
          {
            "node": "Lookup customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Lookup customer": {
      "main": [
        [
          {
            "node": "Prepare draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare draft": {
      "main": [
        [
          {
            "node": "Create draft request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "tags": []
}
