{
  "name": "Sales Data Import & Validation",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "calculate-commissions",
        "responseMode": "responseNode",
        "options": {
          "rawBody": false
        }
      },
      "id": "Webhook-Node-1",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        100,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $node[\"Webhook\"].json[\"headers\"][\"x-forwarded-uri\"] || \"\" }}",
              "operation": "contains",
              "value2": "webhook-test"
            }
          ]
        }
      },
      "id": "If-Node-1",
      "name": "Is Test/Dev Mode?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        300,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "appUrl",
              "value": "https://special-hotel-dev.gaboggamer.online"
            },
            {
              "name": "signature",
              "value": "qUHuPqPjA65psdtwQU7zgp/DVkvd1xXk2WP/vzbnEdc="
            }
          ]
        },
        "options": {}
      },
      "id": "Set-Dev-Env",
      "name": "Set Dev Environment",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        200
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "appUrl",
              "value": "https://special-hotel.gaboggamer.online"
            },
            {
              "name": "signature",
              "value": "Ecjb2s33tHJppNBDJ/DxXEjHWKow8bNWmsQrk1sQKyQ="
            }
          ]
        },
        "options": {}
      },
      "id": "Set-Prod-Env",
      "name": "Set Prod Environment",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        500,
        400
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Set Dev Environment').isExecuted ? $('Set Dev Environment').item.json.appUrl : $('Set Prod Environment').item.json.appUrl }}/api/n8n/validate-sales",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "x-n8n-signature",
              "value": "={{ $('Set Dev Environment').isExecuted ? $('Set Dev Environment').item.json.signature : $('Set Prod Environment').item.json.signature }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"sales\": {{ JSON.stringify($node[\"Webhook\"].json[\"body\"][\"sales\"]) }}\n}",
        "options": {}
      },
      "id": "Http-Request-Validate",
      "name": "HTTP Request: validate-sales",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Verify the following sales records for anomalies. Return a JSON object containing a \"sales\" array, matching the input structure, but adding the fields \"isAnomaly\" (boolean) and \"flaggedReason\" (string or null) for each record.\nIf a record has isValid: false, set isAnomaly to true and flaggedReason to invalidReason.\nIf the amount is greater than the threshold, set isAnomaly to true and flaggedReason to \"Sales amount deviates abnormally from collaborator monthly average\".\nIf the amount is extremely high (e.g. >1000000), set isAnomaly to true and flaggedReason to \"Sales amount exceeds normal threshold limits (>1M)\".\nOtherwise, set isAnomaly to false and flaggedReason to null.\n\nInput sales data:\n{{ JSON.stringify($node[\"HTTP Request: validate-sales\"].json.validationResults) }}",
        "hasOutputParser": true,
        "needsFallback": true,
        "options": {}
      },
      "id": "LLM-Chain-Anomaly",
      "name": "LLM Chain: Anomaly Detection",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.9,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "DeepSeek-Model-Node",
      "name": "Primary Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatDeepSeek",
      "typeVersion": 1,
      "position": [
        750,
        480
      ],
      "credentials": {
        "deepSeekApi": {
          "id": "YbGKBhQ9rlUebE4F",
          "name": "Semillero2_Primary_deepSeekApi"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "Google-Gemini-Model-Node",
      "name": "Fallback Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        900,
        480
      ],
      "credentials": {
        "googlePalmApi": {
          "id": "1Vz05qteUH9kLf7Z",
          "name": "Google Gemini(PaLM) Api account"
        }
      }
    },
    {
      "parameters": {
        "jsonSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"sales\": {\n      \"type\": \"array\",\n      \"items\": {\n        \"type\": \"object\",\n        \"properties\": {\n          \"username\": { \"type\": \"string\" },\n          \"hotelCode\": { \"type\": \"string\" },\n          \"period\": { \"type\": \"string\" },\n          \"amount\": { \"type\": \"number\" },\n          \"salesCount\": { \"type\": \"number\" },\n          \"transactionId\": { \"type\": \"string\" },\n          \"isAnomaly\": { \"type\": \"boolean\" },\n          \"flaggedReason\": { \"type\": \"string\" }\n        },\n        \"required\": [\"username\", \"hotelCode\", \"period\", \"amount\", \"salesCount\", \"isAnomaly\"]\n      }\n    }\n  },\n  \"required\": [\"sales\"]\n}"
      },
      "id": "Structured-Output-Parser-Node",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1,
      "position": [
        1050,
        480
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Set Dev Environment').isExecuted ? $('Set Dev Environment').item.json.appUrl : $('Set Prod Environment').item.json.appUrl }}/api/sales/batch-save",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "x-n8n-signature",
              "value": "={{ $('Set Dev Environment').isExecuted ? $('Set Dev Environment').item.json.signature : $('Set Prod Environment').item.json.signature }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"idempotencyKey\": \"{{ $node[\"Webhook\"].json[\"body\"][\"idempotencyKey\"] }}\",\n  \"uploaderId\": {{ $node[\"Webhook\"].json[\"body\"][\"uploaderId\"] }},\n  \"sales\": {{ JSON.stringify($node[\"LLM Chain: Anomaly Detection\"].json[\"sales\"]) }}\n}",
        "options": {}
      },
      "id": "Http-Request-1",
      "name": "Callback batch-save",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1200,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": true,\n  \"code\": \"IMPORT_ACCEPTED\",\n  \"metadata\": {\n    \"idempotencyKey\": \"{{ $node[\"Webhook\"].json[\"body\"][\"idempotencyKey\"] }}\",\n    \"status\": \"PROCESSING\"\n  }\n}"
      },
      "id": "Respond-To-Webhook",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        450,
        550
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Is Test/Dev Mode?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Test/Dev Mode?": {
      "main": [
        [
          {
            "node": "Set Dev Environment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set Prod Environment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Dev Environment": {
      "main": [
        [
          {
            "node": "HTTP Request: validate-sales",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Prod Environment": {
      "main": [
        [
          {
            "node": "HTTP Request: validate-sales",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request: validate-sales": {
      "main": [
        [
          {
            "node": "LLM Chain: Anomaly Detection",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Primary Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "LLM Chain: Anomaly Detection",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Fallback Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "LLM Chain: Anomaly Detection",
            "type": "ai_languageModel",
            "index": 1
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "outputParser": [
        [
          {
            "node": "LLM Chain: Anomaly Detection",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "LLM Chain: Anomaly Detection": {
      "main": [
        [
          {
            "node": "Callback batch-save",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {},
  "id": "sales-import-workflow"
}
