{
  "name": "fn balance alert",
  "nodes": [
    {
      "parameters": {
        "graphApiVersion": "v23.0",
        "node": "=act_{{ $json.id }}",
        "options": {
          "fields": {
            "field": [
              {
                "name": "name, funding_source_details, spend_cap, amount_spent, balance, account_status"
              }
            ]
          }
        }
      },
      "type": "n8n-nodes-base.facebookGraphApi",
      "typeVersion": 1,
      "position": [
        560,
        96
      ],
      "id": "46ff36d3-6131-43a1-96d7-abfdda69cf1d",
      "name": "Facebook Graph API",
      "credentials": {
        "facebookGraphApi": {
          "id": "N8bIc0umttIBhhsN",
          "name": "Emil Account"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "C0AQD6N001K",
          "mode": "list",
          "cachedResultName": "meta-low-balance"
        },
        "text": "={{ $json.slack_message }}",
        "otherOptions": {}
      },
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.4,
      "position": [
        1088,
        96
      ],
      "id": "7ca42492-ce9b-4324-a768-35b2c80d8144",
      "name": "Send a message",
      "webhookId": "cf9eec5b-fac3-4524-a30c-9bc9973a7dc8",
      "credentials": {
        "slackOAuth2Api": {
          "id": "n2Y45CYxhaBLsOcG",
          "name": "Slack account 2"
        }
      }
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 10
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        -160,
        96
      ],
      "id": "3eb11edf-6fb6-456a-a725-69401ca03366",
      "name": "Schedule Trigger1"
    },
    {
      "parameters": {
        "jsCode": "const ids = [\n  \"1739126056513154\", \n  \"1028797986053084\", \n  \"598274175882800\", \n  \"1586350705721731\", \n  \"252424011228085\", \n  \"1541229220225009\", \n  \"1171899694890833\", \n  \"646050031604920\"\n];\n\n// This maps the array into the format n8n expects: [ { json: { id: value } } ]\nreturn ids.map(id => ({ json: { id: id } }));"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        80,
        96
      ],
      "id": "59a74918-c997-4ca4-ba85-a7c5012bee6c",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "fieldToSplitOut": "id",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        288,
        96
      ],
      "id": "8c452199-b5f7-4970-a535-28fd80eead54",
      "name": "Split Out"
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all();\nconst result = [];\nconst threshold = 10000;\n\nitems.forEach(item => {\n  const data = item.json;\n  let availableBalance = 0;\n  \n  const balanceText = data.funding_source_details?.display_string || \"\";\n  \n  // 1. Check if it's a Prepaid Account (Balance is in the display_string)\n  if (balanceText.includes(\"Balance\")) {\n    const match = balanceText.match(/[\\d,]+\\.?\\d*/);\n    if (match) {\n      availableBalance = parseFloat(match[0].replace(/,/g, ''));\n    }\n  } else {\n    /* 2. For Postpaid/Credit Line accounts:\n      Meta's 'balance' field is usually the amount spent but not yet billed.\n      Note: Meta values are in 'milli-units' or 'cents'. \n      We divide by 100 to get the actual INR value.\n    */\n    availableBalance = parseFloat(data.balance || 0) / 100;\n  }\n\n  // Check against your 10k threshold\n  if (availableBalance < threshold) {\n    result.push({\n      json: {\n        account_name: data.name,\n        account_id: data.id,\n        current_balance: availableBalance,\n        status: data.account_status === 1 ? \"Active\" : \"Disabled/Pending\",\n        slack_message: `🚨 *Low Balance Alert: ${data.name}*\\n` +\n                       `Current Balance: *₹${availableBalance.toLocaleString('en-IN')}*\\n` +\n                       `Account ID: \\`${data.id}\\`\\n` +\n                       `Please top up or check the credit limit soon!`\n      }\n    });\n  }\n});\n\nreturn result;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        816,
        96
      ],
      "id": "6f79ef92-1895-4854-90cf-7d9f559840e7",
      "name": "Code in JavaScript2"
    }
  ],
  "pinData": {},
  "connections": {
    "Facebook Graph API": {
      "main": [
        [
          {
            "node": "Code in JavaScript2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a message": {
      "main": [
        []
      ]
    },
    "Schedule Trigger1": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Facebook Graph API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript2": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "44ec67c6-9f78-4148-8043-831e2e830b14",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "ec71a95432ae1169816e368ff566c55fc9a5562016ed19405e0147feef576edc"
  },
  "id": "KymIurayYzAwnRk2",
  "tags": []
}