Skip to content

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

AttributeValue
Classification🔵 BACKEND-ONLY (Cron)
PositionAfter Cloudflare (reads CF-aggregated usage data)
Receives Public TrafficNo (pg_cron only)
Latency SensitiveNo
Safe to RetryYes (idempotent status updates)
Failure BehaviorLogs 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:

  1. Queries all active subscriptions with current month usage
  2. 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
  3. 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

Interfaces

Released under proprietary license.