{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "117245d0-1397-45f6-adee-6f766ac76bfe",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1728,
        416
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 4,
              "triggerAtMinute": 0
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "b90a223a-8d0d-416f-98aa-d8872bde439f",
      "name": "Load EE Subaccounts",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueErrorOutput",
      "position": [
        -1216,
        416
      ],
      "parameters": {
        "url": "https://api.elasticemail.com/v4/subaccounts",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpCustomAuth"
      },
      "credentials": {
        "httpCustomAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "86f5d0f4-bb22-4c33-816e-f9613645eff2",
      "name": "Config",
      "type": "n8n-nodes-base.set",
      "position": [
        -1456,
        416
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "Minimum amount of Email Credits",
              "type": "numberValue",
              "numberValue": "100"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "bf03cc6c-6057-42d2-86ae-58f77fc44a8b",
      "name": "Send a Error message",
      "type": "n8n-nodes-base.slack",
      "position": [
        -912,
        608
      ],
      "parameters": {
        "text": "=Error obtaining Elastic Email subaccounts data  {{ $json.error }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C0B165RD8JU",
          "cachedResultName": "elastic-email"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.4
    },
    {
      "id": "77137d2a-3633-41ae-ab47-edf9a62c224f",
      "name": "Send notification about low Email Credits",
      "type": "n8n-nodes-base.slack",
      "position": [
        -448,
        400
      ],
      "parameters": {
        "text": "=Elastic Email account {{ $json.Email }} has low Email credits amount ({{ $json.EmailCredits }})",
        "select": "channel",
        "blocksUi": "={{$json.messageContent}}",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C0B165RD8JU",
          "cachedResultName": "elastic-email"
        },
        "messageType": "block",
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.4
    },
    {
      "id": "44761006-41d0-4a9f-9282-1b7a17470ab9",
      "name": "Check if account has enough email credits",
      "type": "n8n-nodes-base.filter",
      "position": [
        -912,
        400
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "11fbf9e0-89e1-4ec6-b121-7807f508fd0a",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{ $json.EmailCredits }}",
              "rightValue": "={{ $('Config').first().json['Minimum amount of Email Credits'] }}"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.3
    },
    {
      "id": "dd4b6cb4-f527-42d0-81be-845aef10c9c1",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1792,
        288
      ],
      "parameters": {
        "color": 3,
        "width": 496,
        "height": 352,
        "content": "## Fill out Config\nSet number of credits to trigger warning"
      },
      "typeVersion": 1
    },
    {
      "id": "9f22a4e3-d672-402a-9efa-54bd060f56bc",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1280,
        288
      ],
      "parameters": {
        "color": 7,
        "width": 1120,
        "height": 496,
        "content": "## Load Elastic Email Subaccount, check if they have at least provided amount of Email Credits and notify user if they don't\n"
      },
      "typeVersion": 1
    },
    {
      "id": "05b7ed1a-012f-4d68-a7d2-6087b358c11d",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2384,
        -96
      ],
      "parameters": {
        "width": 576,
        "height": 736,
        "content": "## Elasticmail Email insufficent amount of email credits\n### How it works\nThis workflow uses custom HTTP Request to Elastic Email official API to get information about your subaccounts, filters out those which has more credits than you provided in config, and notifies you on Slack if subaccount like this exists\n\n### Setup\n1. Provide minimum allowed amount of Email Credits in \"Config\" node\n2. Configure Slack OAuth2 connection\n3. Create credentials for Elastic Email:\n    1. Create generic credential type in HTTP Request node\n    2. Select Custom Auth\n    3. In JSON field type:\n```\n{\n\t\"headers\": {\n\t\t\"X-ElasticEmail-ApiKey\": \"<Your EE Auth token>\"\n\t}\n}\n```\n\nTo obtain Elastic Email Auth token refer to official Rest API [docs](https://elasticemail.com/developers/api-documentation/rest-api) or you can get one [here](https://app.elasticemail.com/marketing/settings/new/manage-api) directly\n\n\n\n\n\n\n\n\n\nNeed help? Contact us at [developers@sailingbyte.com](mailto:developers@sailingbyte.com) or visit [sailingbyte.com](https://sailingbyte.com)!\n\nHappy hacking!"
      },
      "typeVersion": 1
    },
    {
      "id": "be87eaec-9f0d-41ea-9bc1-ef7bbcab5d15",
      "name": "Prepare message content",
      "type": "n8n-nodes-base.code",
      "position": [
        -672,
        400
      ],
      "parameters": {
        "jsCode": "const affectedAccounts = $input.all().map(item=>item.json)\nconst accountMessages = affectedAccounts.map(a=>`\n  ### ${a.Email}\\n\\n**Remaining credits:** ${a.EmailCredits}\\n\\n**Monthly refill credits:** ${a.Settings.Email.MonthlyRefillCredits}\\n\\n---\n`).join('')\nconst messageContent = \n{\n\t\"blocks\": [\n\t\t{\n\t\t\t\"type\": \"markdown\",\n\t\t\t\"text\": `## Warning \\n\\n${affectedAccounts.length} of your ElasticEmail subaccounts are running low on credits.\\n\\n---\\n\\n${accountMessages}`\n\t\t}\n\t]\n}\n\n\nreturn {messageContent}"
      },
      "typeVersion": 2
    }
  ],
  "connections": {
    "Config": {
      "main": [
        [
          {
            "node": "Load EE Subaccounts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load EE Subaccounts": {
      "main": [
        [
          {
            "node": "Check if account has enough email credits",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a Error message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare message content": {
      "main": [
        [
          {
            "node": "Send notification about low Email Credits",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if account has enough email credits": {
      "main": [
        [
          {
            "node": "Prepare message content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}