{
  "id": "AutomationNotePortfolio01",
  "name": "AUTOMATION NOTE Inquiry Routing Portfolio",
  "description": null,
  "active": false,
  "isArchived": false,
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "portfolio-inquiry-routing",
        "authentication": "none",
        "responseMode": "responseNode",
        "options": {
          "allowedOrigins": "https://automation-note.com"
        }
      },
      "id": "2d386489-3271-4eb3-a308-0c19fffc2933",
      "name": "Receive Inquiry",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        160
      ],
      "webhookId": "2e7c5f81-e421-49aa-989b-0c39fd68544d"
    },
    {
      "parameters": {
        "jsCode": "const input = $json.body ?? $json;\nconst caseId = typeof input.case_id === 'string' ? input.case_id.trim() : '';\nconst subject = typeof input.subject === 'string' ? input.subject.trim() : '';\nconst body = typeof input.body === 'string' ? input.body.trim() : '';\nconst errors = [];\nif (!/^case-[a-z0-9-]{3,40}$/i.test(caseId)) errors.push('case_id');\nif (!subject || subject.length > 120) errors.push('subject');\nif (!body || body.length > 1000) errors.push('body');\nconst source = `${subject} ${body}`;\nconst category = /請求|金額|支払/.test(source) ? 'billing' : /エラー|連携|障害/.test(source) ? 'technical' : /アカウント|ログイン|権限/.test(source) ? 'account' : 'other';\nconst summary = `${subject}：${body}`.slice(0, 180);\nreturn [{ json: {\n  valid: errors.length === 0,\n  errors,\n  case_id: caseId || null,\n  category,\n  summary,\n  needs_review: /確認が必要|判断が必要|曖昧/.test(body),\n  idempotency_key: caseId || null\n} }];"
      },
      "id": "c4ba59bc-1f11-46a4-a34c-bbaeedb2e3ad",
      "name": "Validate and Normalize",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        260,
        160
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.valid }}",
              "operation": "equal",
              "value2": true
            }
          ]
        },
        "combineOperation": "all"
      },
      "id": "16ef0901-569f-4eae-986a-6df8692f3ff4",
      "name": "Is Valid",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        520,
        160
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $env.PORTFOLIO_MOCK_API_URL + '/route' }}",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ { case_id: $json.case_id, category: $json.category, summary: $json.summary, needs_review: $json.needs_review, idempotency_key: $json.idempotency_key } }}",
        "options": {
          "timeout": 3000,
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "db84337a-c3fa-4b98-9782-cda9e07dccee",
      "name": "Route Through Mock API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        780,
        80
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 250,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "jsCode": "return $input.all().map((item) => ({ json: {\n  ok: true,\n  status: item.json.status,\n  case_id: item.json.case_id,\n  ticket_id: item.json.ticket_id,\n  category: item.json.category,\n  needs_review: item.json.needs_review,\n  duplicate: item.json.status === 'duplicate'\n} }));"
      },
      "id": "e4309ace-d62e-4ce3-8d31-d5ba99595ba7",
      "name": "Build Accepted Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1040,
        80
      ]
    },
    {
      "parameters": {
        "respondWith": "firstIncomingItem",
        "options": {
          "responseCode": 202,
          "responseHeaders": {
            "entries": [
              {
                "name": "Cache-Control",
                "value": "no-store"
              }
            ]
          }
        }
      },
      "id": "aab560ad-5693-4e68-b73e-57490b6a6970",
      "name": "Respond Accepted",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1300,
        80
      ]
    },
    {
      "parameters": {
        "jsCode": "return $input.all().map((item) => ({ json: {\n  ok: false,\n  status: 'rejected',\n  case_id: item.json.case_id,\n  invalid_fields: item.json.errors\n} }));"
      },
      "id": "ca9c8eb9-26ee-4a67-9827-ec30cc8814a8",
      "name": "Build Rejected Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        780,
        280
      ]
    },
    {
      "parameters": {
        "respondWith": "firstIncomingItem",
        "options": {
          "responseCode": 400,
          "responseHeaders": {
            "entries": [
              {
                "name": "Cache-Control",
                "value": "no-store"
              }
            ]
          }
        }
      },
      "id": "23878128-d7b2-44c1-ab57-4015a1a6a775",
      "name": "Respond Rejected",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1040,
        280
      ]
    },
    {
      "parameters": {
        "jsCode": "return [{ json: { ok: false, status: 'upstream_unavailable' } }];"
      },
      "id": "d7985e07-f0da-4dbf-a95f-10c2ecff5189",
      "name": "Build Upstream Failure",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1040,
        -120
      ]
    },
    {
      "parameters": {
        "respondWith": "firstIncomingItem",
        "options": {
          "responseCode": 502,
          "responseHeaders": {
            "entries": [
              {
                "name": "Cache-Control",
                "value": "no-store"
              }
            ]
          }
        }
      },
      "id": "c438f9dc-6141-4334-8ad3-03b9671f333e",
      "name": "Respond Upstream Failure",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        1300,
        -120
      ]
    }
  ],
  "connections": {
    "Receive Inquiry": {
      "main": [
        [
          {
            "node": "Validate and Normalize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate and Normalize": {
      "main": [
        [
          {
            "node": "Is Valid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Valid": {
      "main": [
        [
          {
            "node": "Route Through Mock API",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Rejected Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route Through Mock API": {
      "main": [
        [
          {
            "node": "Build Accepted Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Upstream Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Accepted Response": {
      "main": [
        [
          {
            "node": "Respond Accepted",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Rejected Response": {
      "main": [
        [
          {
            "node": "Respond Rejected",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Upstream Failure": {
      "main": [
        [
          {
            "node": "Respond Upstream Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "pinData": null,
  "versionId": "b881f1e6-ce9a-4bd2-8b42-de7dfa7d628f",
  "activeVersionId": "b881f1e6-ce9a-4bd2-8b42-de7dfa7d628f",
  "sourceWorkflowId": null,
  "tags": [],
  "versionMetadata": {
    "name": null,
    "description": null
  }
}
