digital-identity-platform-docs
digital-identity-platform-docs / supabase/functions/billing-usage-check
supabase/functions/billing-usage-check
Fileoverview
Billing Usage Check Edge Function
Scheduled cron job that checks container usage against plan limits and takes appropriate action (auto-upgrade or pause) when limits are exceeded.
Cloudflare Architecture Context
| Attribute | Value |
|---|---|
| Classification | 🔵 BACKEND-ONLY (Cron) |
| Position | After Cloudflare (reads CF-aggregated usage data) |
| Receives Public Traffic | No (pg_cron only) |
| Latency Sensitive | No |
| Safe to Retry | Yes (idempotent status updates) |
| Failure Behavior | Logs errors, continues with other containers |
Data Source: Reads from sgtm_usage_daily_cf table which is populated by Cloudflare Analytics Engine via scheduled sync.
Endpoint
POST /billing-usage-check
Auth
Required - x-cron-secret header with CRON_SECRET
Remarks
This function runs on a schedule and:
- Queries all active subscriptions with current month usage
- For each subscription at or over limit:
- If auto_upgrade=true: Attempt to upgrade to next higher plan via Stripe
- If no higher plan available or auto_upgrade=false: Pause subscription
- Logs warnings for subscriptions approaching 80% of limit
Usage thresholds:
- < 80%: No action
- 80-99%: Warning logged
= 100%: Auto-upgrade or pause
Tables touched:
- sgtm_container_subscriptions (read/write via RPC and direct)
- billing_plans (read)
External services:
- Stripe API for subscription upgrades