{
  "flow": [
    {
      "id": 1,
      "mapper": {
        "from": "{{formatDate(addDays(now; -7); \"YYYY-MM-DD\")}}",
        "limit": 1000
      },
      "module": "convertkit:listAllSubscribers",
      "version": 1,
      "metadata": {
        "designer": {
          "x": 0,
          "y": 0,
          "name": "Kit \u2014 New Subscribers, Last 7 Days"
        }
      },
      "parameters": {
        "__IMTCONN__": null
      }
    },
    {
      "id": 2,
      "mapper": {
        "id": "{{1.id}}"
      },
      "module": "builtin:BasicAggregator",
      "version": 1,
      "metadata": {
        "designer": {
          "x": 300,
          "y": 0,
          "name": "Aggregate \u2014 Subscriber Count"
        }
      },
      "parameters": {
        "feeder": 1
      }
    },
    {
      "id": 3,
      "mapper": {
        "limit": 500
      },
      "module": "stripe:listPaymentIntents",
      "version": 1,
      "metadata": {
        "designer": {
          "x": 600,
          "y": 0,
          "name": "Stripe \u2014 Payment Intents"
        }
      },
      "parameters": {
        "__IMTCONN__": null
      }
    },
    {
      "id": 4,
      "filter": {
        "name": "Succeeded, last 7 days",
        "conditions": [
          [
            {
              "a": "{{3.status}}",
              "b": "succeeded",
              "o": "text:equal"
            },
            {
              "a": "{{formatDate(3.created; \"X\")}}",
              "b": "{{formatDate(addDays(now; -7); \"X\")}}",
              "o": "number:greater"
            }
          ]
        ]
      },
      "mapper": {
        "amount": "{{3.amount}}"
      },
      "module": "builtin:BasicAggregator",
      "version": 1,
      "metadata": {
        "designer": {
          "x": 900,
          "y": 0,
          "name": "Aggregate \u2014 Sales Count + Revenue"
        }
      },
      "parameters": {
        "feeder": 3
      }
    },
    {
      "id": 5,
      "mapper": {
        "base": "YOUR_AIRTABLE_BASE_ID",
        "table": "YOUR_AIRTABLE_TABLE_ID",
        "formula": "IS_AFTER({Published Date}, DATEADD(TODAY(), -7, 'days'))",
        "maxRecords": 1000,
        "useColumnId": false
      },
      "module": "airtable:ActionSearchRecords",
      "version": 3,
      "metadata": {
        "designer": {
          "x": 1200,
          "y": 0,
          "name": "Airtable \u2014 Content Published, Last 7 Days"
        }
      },
      "parameters": {
        "__IMTCONN__": null
      }
    },
    {
      "id": 6,
      "mapper": {
        "id": "{{5.id}}"
      },
      "module": "builtin:BasicAggregator",
      "version": 1,
      "metadata": {
        "designer": {
          "x": 1500,
          "y": 0,
          "name": "Aggregate \u2014 Content Count"
        }
      },
      "parameters": {
        "feeder": 5
      }
    },
    {
      "id": 7,
      "mapper": {
        "base": "YOUR_AIRTABLE_BASE_ID",
        "table": "YOUR_AIRTABLE_TABLE_ID",
        "formula": "IS_AFTER({Date}, DATEADD(TODAY(), -7, 'days'))",
        "maxRecords": 1000,
        "useColumnId": false
      },
      "module": "airtable:ActionSearchRecords",
      "version": 3,
      "metadata": {
        "designer": {
          "x": 1800,
          "y": 0,
          "name": "Airtable \u2014 Automation Errors, Last 7 Days"
        }
      },
      "parameters": {
        "__IMTCONN__": null
      }
    },
    {
      "id": 8,
      "mapper": {
        "id": "{{7.id}}"
      },
      "module": "builtin:BasicAggregator",
      "version": 1,
      "metadata": {
        "designer": {
          "x": 2100,
          "y": 0,
          "name": "Aggregate \u2014 Error Count"
        }
      },
      "parameters": {
        "feeder": 7
      }
    },
    {
      "id": 9,
      "mapper": {
        "base": "YOUR_AIRTABLE_BASE_ID",
        "table": "YOUR_AIRTABLE_TABLE_ID",
        "formula": "IS_AFTER({Week Of}, DATEADD(TODAY(), -6, 'days'))",
        "maxRecords": 1,
        "useColumnId": false
      },
      "module": "airtable:ActionSearchRecords",
      "version": 3,
      "metadata": {
        "designer": {
          "x": 2400,
          "y": 0,
          "name": "Airtable \u2014 Find This Week's Metrics Row"
        }
      },
      "parameters": {
        "__IMTCONN__": null
      }
    },
    {
      "id": 10,
      "mapper": {
        "id": "{{9.id}}"
      },
      "module": "builtin:BasicAggregator",
      "version": 1,
      "metadata": {
        "designer": {
          "x": 2700,
          "y": 0,
          "name": "Aggregate \u2014 Existing Row Check"
        }
      },
      "parameters": {
        "feeder": 9
      }
    },
    {
      "id": 11,
      "mapper": null,
      "module": "builtin:BasicRouter",
      "routes": [
        {
          "flow": [
            {
              "id": 12,
              "filter": {
                "name": "Row exists \u2014 update",
                "conditions": [
                  [
                    {
                      "a": "{{length(10.array)}}",
                      "b": "0",
                      "o": "number:greater"
                    }
                  ]
                ]
              },
              "mapper": {
                "id": "{{10.array[1].id}}",
                "base": "YOUR_AIRTABLE_BASE_ID",
                "table": "YOUR_AIRTABLE_TABLE_ID",
                "record": {
                  "YOUR_FIELD_1": "{{length(2.array)}}",
                  "YOUR_FIELD_2": "{{sum(map(4.array; \"amount\")) / 100}}",
                  "YOUR_FIELD_3": "{{length(6.array)}}",
                  "YOUR_FIELD_4": "{{length(8.array)}}",
                  "YOUR_FIELD_5": "{{length(4.array)}}"
                },
                "typecast": true,
                "useColumnId": true
              },
              "module": "airtable:ActionUpdateRecords",
              "onerror": [
                {
                  "id": 20,
                  "mapper": {
                    "base": "YOUR_AIRTABLE_BASE_ID",
                    "table": "YOUR_AIRTABLE_TABLE_ID",
                    "record": {
                      "YOUR_FIELD_1": "{{now}}",
                      "YOUR_FIELD_2": "Todo",
                      "YOUR_FIELD_3": "Nightly Dashboard Aggregation failed to update Metrics row: {{12.error.message}}",
                      "YOUR_FIELD_4": "Nightly Dashboard Aggregation"
                    },
                    "typecast": false,
                    "useColumnId": true
                  },
                  "module": "airtable:ActionCreateRecord",
                  "version": 3,
                  "metadata": {
                    "designer": {
                      "x": 3000,
                      "y": 150,
                      "name": "Log Error"
                    }
                  },
                  "parameters": {
                    "__IMTCONN__": null
                  }
                },
                {
                  "id": 21,
                  "mapper": {
                    "text": "\u26a0\ufe0f Nightly Dashboard Aggregation failed to update this week's Metrics row. Error: {{12.error.message}}.",
                    "chatId": "YOUR_TELEGRAM_CHAT_ID"
                  },
                  "module": "telegram:SendReplyMessage",
                  "version": 1,
                  "metadata": {
                    "designer": {
                      "x": 3300,
                      "y": 150,
                      "name": "Alert"
                    }
                  },
                  "parameters": {
                    "__IMTCONN__": null
                  }
                }
              ],
              "version": 3,
              "metadata": {
                "designer": {
                  "x": 3000,
                  "y": -150,
                  "name": "Update This Week's Row"
                }
              },
              "parameters": {
                "__IMTCONN__": null
              }
            }
          ]
        },
        {
          "flow": [
            {
              "id": 13,
              "filter": {
                "name": "No row yet \u2014 create",
                "conditions": [
                  [
                    {
                      "a": "{{length(10.array)}}",
                      "b": "0",
                      "o": "number:equal"
                    }
                  ]
                ]
              },
              "mapper": {
                "base": "YOUR_AIRTABLE_BASE_ID",
                "table": "YOUR_AIRTABLE_TABLE_ID",
                "record": {
                  "YOUR_FIELD_1": "{{formatDate(now; \"YYYY-MM-DD\")}}",
                  "YOUR_FIELD_2": "{{length(2.array)}}",
                  "YOUR_FIELD_3": "{{sum(map(4.array; \"amount\")) / 100}}",
                  "YOUR_FIELD_4": "{{length(6.array)}}",
                  "YOUR_FIELD_5": "{{length(8.array)}}",
                  "YOUR_FIELD_6": "{{length(4.array)}}"
                },
                "typecast": true,
                "useColumnId": true
              },
              "module": "airtable:ActionCreateRecord",
              "onerror": [
                {
                  "id": 30,
                  "mapper": {
                    "base": "YOUR_AIRTABLE_BASE_ID",
                    "table": "YOUR_AIRTABLE_TABLE_ID",
                    "record": {
                      "YOUR_FIELD_1": "{{now}}",
                      "YOUR_FIELD_2": "Todo",
                      "YOUR_FIELD_3": "Nightly Dashboard Aggregation failed to create Metrics row: {{13.error.message}}",
                      "YOUR_FIELD_4": "Nightly Dashboard Aggregation"
                    },
                    "typecast": false,
                    "useColumnId": true
                  },
                  "module": "airtable:ActionCreateRecord",
                  "version": 3,
                  "metadata": {
                    "designer": {
                      "x": 3000,
                      "y": 450,
                      "name": "Log Error"
                    }
                  },
                  "parameters": {
                    "__IMTCONN__": null
                  }
                },
                {
                  "id": 31,
                  "mapper": {
                    "text": "\u26a0\ufe0f Nightly Dashboard Aggregation failed to create this week's Metrics row. Error: {{13.error.message}}.",
                    "chatId": "YOUR_TELEGRAM_CHAT_ID"
                  },
                  "module": "telegram:SendReplyMessage",
                  "version": 1,
                  "metadata": {
                    "designer": {
                      "x": 3300,
                      "y": 450,
                      "name": "Alert"
                    }
                  },
                  "parameters": {
                    "__IMTCONN__": null
                  }
                }
              ],
              "version": 3,
              "metadata": {
                "designer": {
                  "x": 3000,
                  "y": 300,
                  "name": "Create This Week's Row"
                }
              },
              "parameters": {
                "__IMTCONN__": null
              }
            }
          ]
        }
      ],
      "version": 1,
      "metadata": {
        "designer": {
          "x": 2900,
          "y": 0,
          "name": "Route \u2014 Update or Create"
        }
      }
    }
  ],
  "name": "Nightly Dashboard Aggregation",
  "metadata": {
    "instant": false,
    "version": 1,
    "designer": {
      "orphans": []
    },
    "scenario": {
      "dlq": false,
      "maxErrors": 3,
      "autoCommit": true,
      "roundtrips": 1,
      "sequential": false,
      "confidential": false,
      "freshVariables": false,
      "autoCommitTriggerLast": true
    }
  },
  "scheduling": {
    "time": "02:00",
    "type": "daily"
  }
}