{
  "name": "Citizen Complaint Routing & SLA Escalation",
  "nodes": [
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "gpt-4o-mini"
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        352,
        256
      ],
      "id": "29487253-1ef4-4b21-9ce2-91c442fdfc2f",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "SsnwT4koLkkkT1th",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Citizen Request Webhook').item.json.body.From }}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        512,
        256
      ],
      "id": "2f62a067-a896-455e-b47c-82c37eaf2458",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "citizen-request",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ],
      "id": "260d2365-6a20-4b64-ac38-0668d863b081",
      "name": "Citizen Request Webhook",
      "webhookId": "12eef532-fe62-47dd-afb3-c59f1417b49e"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "53c9bc94-0be1-4502-9596-3bff8fd5059f",
              "name": "Body",
              "value": "={{ $json.body.Body }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        208,
        0
      ],
      "id": "31617133-adc8-4dc9-9776-0f8a559c5134",
      "name": "Extract WhatsApp Message"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.Body }}",
        "options": {
          "systemMessage": "Classify this complaint into:\n1. Category (water / electricity / road / garbage)\n2. Priority (low / medium / high)\n\nComplaint:\n{{ $json.description }}\n\nReturn JSON:\n{\n \"category\": \"\",\n \"priority\": \"\"\n}"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        416,
        0
      ],
      "id": "46707422-66c8-458d-ae6b-ce31993bbe6f",
      "name": "AI Complaint Classifier"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "leftValue": "={{ JSON.parse($json.output).category }}",
                    "rightValue": "false",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "70932fbc-1a18-4c27-bf99-8ddeb92b0043"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Water Dept"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "867f6c41-d976-4158-98fa-83c6335123bd",
                    "leftValue": "={{ JSON.parse($json.output).category }}",
                    "rightValue": "electricity",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Electricity Dept"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "9ade3cac-5393-498b-b6c5-5d5dc5fbcdf7",
                    "leftValue": "={{ JSON.parse($json.output).category }}",
                    "rightValue": "garbage",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Sanitation Dept"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        768,
        -16
      ],
      "id": "8d7d7cfc-c451-4cf7-892d-8ae8be572649",
      "name": "Route to Department",
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3087101d-5e63-4418-8849-446ec7b5ba61",
              "name": "=department",
              "value": "Water Department",
              "type": "string"
            },
            {
              "id": "31f8e36c-0cdc-465e-95d2-5b27190abace",
              "name": "assigned_to",
              "value": "Water Officer A",
              "type": "string"
            },
            {
              "id": "68120776-0a73-4309-bcf1-fad0e0bb5f97",
              "name": "status",
              "value": "assigned",
              "type": "string"
            },
            {
              "id": "a25ae993-4f88-42d4-a5b3-550a69e21659",
              "name": "complaint_id",
              "value": "={{$now}}-{{ JSON.parse($json.output).category }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        -176
      ],
      "id": "f89f8859-c416-4847-87a1-160bb7062b71",
      "name": "Assign Water Department"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3087101d-5e63-4418-8849-446ec7b5ba61",
              "name": "=department",
              "value": "Electricity Department",
              "type": "string"
            },
            {
              "id": "31f8e36c-0cdc-465e-95d2-5b27190abace",
              "name": "assigned_to",
              "value": "Electricity Officer A",
              "type": "string"
            },
            {
              "id": "68120776-0a73-4309-bcf1-fad0e0bb5f97",
              "name": "status",
              "value": "assigned",
              "type": "string"
            },
            {
              "id": "e819fc0b-3a30-412b-b0f6-58773252cba3",
              "name": "complaint_id",
              "value": "={{$now}}-{{ JSON.parse($json.output).category }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        0
      ],
      "id": "5a8d61db-fcb9-492f-9d17-34566940e40e",
      "name": "Assign Electricity Department"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3087101d-5e63-4418-8849-446ec7b5ba61",
              "name": "=department",
              "value": "Sanitation Department",
              "type": "string"
            },
            {
              "id": "31f8e36c-0cdc-465e-95d2-5b27190abace",
              "name": "assigned_to",
              "value": "Sanitation Officer A",
              "type": "string"
            },
            {
              "id": "68120776-0a73-4309-bcf1-fad0e0bb5f97",
              "name": "status",
              "value": "assigned",
              "type": "string"
            },
            {
              "id": "ce93af1b-a7fb-4a71-8ede-b0e97f22bdf0",
              "name": "complaint_id",
              "value": "={{$now}}-{{ JSON.parse($json.output).category }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        176
      ],
      "id": "1ed4e3ed-cbf5-411e-81a4-938f030d7b2d",
      "name": "Assign Sanitation Department"
    },
    {
      "parameters": {
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "complaints",
          "mode": "list",
          "cachedResultName": "complaints"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "complaint_id": "={{ $json.complaint_id }}",
            "department": "={{ $json.department }}",
            "category": "={{ JSON.parse($('AI Complaint Classifier').item.json.output).category }}",
            "priority": "={{ JSON.parse($('AI Complaint Classifier').item.json.output).priority }}",
            "status": "={{ $json.status }}",
            "description": "={{ $('Citizen Request Webhook').item.json.body.Body }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "complaint_id",
              "displayName": "complaint_id",
              "required": true,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "category",
              "displayName": "category",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "department",
              "displayName": "department",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "displayName": "status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "priority",
              "displayName": "priority",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "description",
              "displayName": "description",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "created_at",
              "displayName": "created_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "phone",
              "displayName": "phone",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        1520,
        0
      ],
      "id": "1ba8ad97-1795-4517-b689-e9fb1e929306",
      "name": "Save Complaint to Database",
      "credentials": {
        "postgres": {
          "id": "FNDrUt6j6515EoU7",
          "name": "Postgres account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const message = `Hello 👋\n\nYour complaint has been registered.\n\nComplaint ID: ${$input.first().json.complaint_id}\nCategory: ${$input.first().json.category}\nDepartment: ${$input.first().json.department}\n\nWe will update you soon.`;\n\nreturn {\n  output: {\n    payload: {\n      message\n    }\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1728,
        0
      ],
      "id": "98a8f686-1d6f-455c-8f3d-40765f18803f",
      "name": "Generate Confirmation Message"
    },
    {
      "parameters": {
        "from": "={{ $('Citizen Request Webhook').item.json.body.To }}",
        "to": "={{ $('Citizen Request Webhook').item.json.body.From }}",
        "message": "={{ $json.output.payload.message }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1936,
        0
      ],
      "id": "80a9ded2-967c-4162-a378-5b9ffc0bd5dd",
      "name": "Send WhatsApp Notification",
      "credentials": {
        "twilioApi": {
          "id": "x3JY36aoL50A71ad",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        0,
        640
      ],
      "id": "80dfb688-c2bf-4fac-a28c-f030a4ae095f",
      "name": "SLA Scheduler (Every 5 Minutes)"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT *\nFROM complaints\nWHERE status != 'resolved'",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        208,
        640
      ],
      "id": "db815329-62dc-46ba-9409-aef800fd12f5",
      "name": "Fetch Pending Complaints",
      "credentials": {
        "postgres": {
          "id": "FNDrUt6j6515EoU7",
          "name": "Postgres account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "0585c5ce-7dec-4447-9ce7-c60431fcdd4c",
              "leftValue": "={{ new Date() - new Date($json.created_at) }}",
              "rightValue": 3600000,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        416,
        640
      ],
      "id": "983e0e0b-0f74-4710-8a20-bdb168ba5889",
      "name": "Check SLA Breach"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "UPDATE complaints\nSET status = 'escalated'\nWHERE complaint_id = '{{ $json.complaint_id }}'",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        720,
        496
      ],
      "id": "a314df5d-6ad4-4940-b335-2340463c63b3",
      "name": "Mark Complaint as Escalated",
      "credentials": {
        "postgres": {
          "id": "FNDrUt6j6515EoU7",
          "name": "Postgres account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c51c54e3-2e5c-4eb2-bbb4-ea3eff860530",
              "name": "assigned_to",
              "value": "Senior Officer",
              "type": "string"
            },
            {
              "id": "6879f56f-21d2-4a01-ae72-c2acd1c8ddad",
              "name": "status",
              "value": "escalated",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        928,
        496
      ],
      "id": "cf9ae965-ab88-4a4c-aa3c-26f4678dd5de",
      "name": "Assign Senior Officer"
    },
    {
      "parameters": {
        "jsCode": "const message = `⚠️ Alert!\n\nYour complaint ${$('Fetch Pending Complaints').first().json.complaint_id} has been escalated due to delay.\n\nWe are prioritizing it now.`;\n\nreturn {\n  output: {\n    payload: {\n      message\n    }\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1136,
        496
      ],
      "id": "e5713393-8beb-4ec4-bd4e-20d0725e27c7",
      "name": "Generate Escalation Alert Message"
    },
    {
      "parameters": {
        "from": "=whatsapp:+14155238886",
        "to": "=whatsapp:+{{ $('Fetch Pending Complaints').first().json.phone }}",
        "message": "={{ $json.output.payload.message }}",
        "options": {}
      },
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1344,
        496
      ],
      "id": "6ac9193e-f56c-4e60-96c6-4e3ee2545444",
      "name": "Send Escalation WhatsApp Alert",
      "credentials": {
        "twilioApi": {
          "id": "x3JY36aoL50A71ad",
          "name": "Twilio account"
        }
      }
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        720,
        784
      ],
      "id": "42df0ced-5925-44aa-85c3-648248aeda4c",
      "name": "No SLA Breach (Skip)"
    },
    {
      "parameters": {
        "content": "## Automated SLA Monitoring, Escalation & Notification System",
        "width": 304
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -352,
        624
      ],
      "id": "b1ca85ed-cbd2-43ce-bc69-8e77037bc21e",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## Citizen Complaint Intake, Classification & Department Routing System",
        "width": 320
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -352,
        -16
      ],
      "id": "4f4522b1-f84c-48b3-a955-1c697bf2c59c",
      "name": "Sticky Note"
    }
  ],
  "pinData": {},
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Complaint Classifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Complaint Classifier",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Citizen Request Webhook": {
      "main": [
        [
          {
            "node": "Extract WhatsApp Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract WhatsApp Message": {
      "main": [
        [
          {
            "node": "AI Complaint Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Complaint Classifier": {
      "main": [
        [
          {
            "node": "Route to Department",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route to Department": {
      "main": [
        [
          {
            "node": "Assign Water Department",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Assign Electricity Department",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Assign Sanitation Department",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Water Department": {
      "main": [
        [
          {
            "node": "Save Complaint to Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Electricity Department": {
      "main": [
        [
          {
            "node": "Save Complaint to Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Sanitation Department": {
      "main": [
        [
          {
            "node": "Save Complaint to Database",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Complaint to Database": {
      "main": [
        [
          {
            "node": "Generate Confirmation Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Confirmation Message": {
      "main": [
        [
          {
            "node": "Send WhatsApp Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SLA Scheduler (Every 5 Minutes)": {
      "main": [
        [
          {
            "node": "Fetch Pending Complaints",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Pending Complaints": {
      "main": [
        [
          {
            "node": "Check SLA Breach",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check SLA Breach": {
      "main": [
        [
          {
            "node": "Mark Complaint as Escalated",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No SLA Breach (Skip)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark Complaint as Escalated": {
      "main": [
        [
          {
            "node": "Assign Senior Officer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Senior Officer": {
      "main": [
        [
          {
            "node": "Generate Escalation Alert Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Escalation Alert Message": {
      "main": [
        [
          {
            "node": "Send Escalation WhatsApp Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "a786f43f-16ac-4dd8-ad80-85be2718d9ef",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "YPkjdIYA1rk8xxya",
  "tags": []
}