{
  "name": "claims-notifier",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "00099889-3dd2-49c4-98fe-b1022e9a0f48",
      "name": "When clicking 'Execute workflow'"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1kWt1Sz2omMoXB31-riPLwe6RYT-dB4cmC-Yhq5oSPoU",
          "mode": "list",
          "cachedResultName": "3_Claims Data",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1kWt1Sz2omMoXB31-riPLwe6RYT-dB4cmC-Yhq5oSPoU/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1kWt1Sz2omMoXB31-riPLwe6RYT-dB4cmC-Yhq5oSPoU/edit#gid=0"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        208,
        0
      ],
      "id": "f66e965f-811d-4251-bab1-020fe86fc6fc",
      "name": "Get row(s) in sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "81c767a7-d327-4797-8149-a809da7654b6",
              "leftValue": "={{ $json.Status }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [
        416,
        0
      ],
      "id": "da80f1e3-25a4-40ea-9a7d-77c1ab5d3cc5",
      "name": "Filter"
    },
    {
      "parameters": {
        "jsCode": "// Group filtered rows by Merchant and prepare a Slack message for each one.\n\nconst groups = {};\n\nfor (const item of $input.all()) {\n  const row = item.json;\n  const merchant = (row.Merchant || \"\").trim();\n  if (!merchant) continue;\n\n  const amount = Number(row[\"Amount (\u20ac)\"]) || 0;\n  const month = String(row[\"Invoice Month\"]).slice(0, 7);\n  const line = `\u2022 ${row.Carrier} \u2014 ${row[\"Surcharge Type\"]} \u2014 \u20ac${amount.toFixed(2)}`;\n\n  if (!groups[merchant]) {\n    groups[merchant] = {\n      merchant,\n      total: 0,\n      months: new Set(),\n      lines: [],\n      claim_ids: [],\n      row_numbers: [],\n    };\n  }\n\n  const g = groups[merchant];\n  g.total += amount;\n  g.months.add(month);\n  g.lines.push(line);\n  g.claim_ids.push(row[\"Claim ID\"]);\n  g.row_numbers.push(row.row_number);\n}\n\nreturn Object.values(groups).map(g => ({\n  json: {\n    merchant: g.merchant,\n    charge_count: g.lines.length,\n    total_amount: Number(g.total.toFixed(2)),\n    invoice_months: [...g.months].sort().join(\", \"),\n    claim_ids: g.claim_ids,\n    row_numbers: g.row_numbers,\n    slack_text:\n      `:warning: *${g.merchant}* \u2014 ${g.lines.length} flagged charge(s)\\n` +\n      `*Total claimed:* \u20ac${g.total.toFixed(2)}\\n` +\n      `*Invoice month(s):* ${[...g.months].sort().join(\", \")}\\n\\n` +\n      `*Breakdown:*\\n${g.lines.join(\"\\n\")}`,\n  },\n}));"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        624,
        0
      ],
      "id": "bc447d93-edb0-4c31-a431-46cca34175ba",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        832,
        0
      ],
      "id": "d512aa84-39d0-413a-8735-f6915d088848",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"text\": {{ JSON.stringify($json.slack_text) }}\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1120,
        16
      ],
      "id": "0de4b657-ee0d-452f-a85a-80547f87f58c",
      "name": "HTTP Request",
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "fieldToSplitOut": "claim_ids",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        1472,
        32
      ],
      "id": "ebffce1c-8b12-4e96-914c-bde24c2b8fca",
      "name": "Split Out"
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "value": "1kWt1Sz2omMoXB31-riPLwe6RYT-dB4cmC-Yhq5oSPoU",
          "mode": "list",
          "cachedResultName": "3_Claims Data",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1kWt1Sz2omMoXB31-riPLwe6RYT-dB4cmC-Yhq5oSPoU/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1kWt1Sz2omMoXB31-riPLwe6RYT-dB4cmC-Yhq5oSPoU/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Claim ID": "={{ $json.claim_ids }}",
            "Status": "NOTIFIED",
            "Notified At": "={{ $now.toISO() }}"
          },
          "matchingColumns": [
            "Claim ID"
          ],
          "schema": [
            {
              "id": "Claim ID",
              "displayName": "Claim ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Merchant",
              "displayName": "Merchant",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Carrier",
              "displayName": "Carrier",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Surcharge Type",
              "displayName": "Surcharge Type",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Amount (\u20ac)",
              "displayName": "Amount (\u20ac)",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Invoice Month",
              "displayName": "Invoice Month",
              "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": "Notified At",
              "displayName": "Notified At",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1200,
        272
      ],
      "id": "0733115a-f459-4eee-899a-c105c0917ebf",
      "name": "Update row in sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "When clicking 'Execute workflow'": {
      "main": [
        [
          {
            "node": "Get row(s) in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get row(s) in sheet": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [],
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Update row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update row in sheet": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "da024e15-0663-4471-b91f-ccc1a464f960",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "Ae13JjYm6eOsdlLc",
  "tags": []
}