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.

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

Returns

200 - Summary with checked, auto_upgraded, paused, warnings counts

Returns

401 - Invalid or missing cron secret

Returns

500 - Configuration or database error