Billing Cycle Management
Reportr implements a 30-day rolling billing cycle system for fair usage tracking across different subscription tiers. This system automatically resets usage limits every 30 days from the user’s start date.Architecture Overview
Core Module
Location:src/lib/billing-cycle.ts
Purpose: Manages 30-day rolling billing cycles for report generation limits
Key Concepts:
- Rolling Cycle - 30 days from cycle start, not calendar month
- Automatic Reset - Checks and resets on every usage check
- Fair Allocation - Users get full 30 days regardless of signup date
Database Schema
User Model Fields:Core Functions
checkAndResetBillingCycle()
Checks if a user’s billing cycle needs reset and resets if necessary.
Location: src/lib/billing-cycle.ts:20
true if cycle was reset, false if no reset needed
When Called:
- Before checking usage limits
- When displaying usage statistics
- Before creating new reports
getBillingCycleInfo()
Retrieves current billing cycle information for a user.
Location: src/lib/billing-cycle.ts:67
getReportsInCurrentCycle()
Counts reports generated in the current billing cycle.
Location: src/lib/billing-cycle.ts:102
getUsageStats()
Retrieves comprehensive usage statistics for a user.
Location: src/lib/billing-cycle.ts:167
initializeBillingCycle()
Initializes billing cycle for a new user.
Location: src/lib/billing-cycle.ts:134
- During user registration (after signup)
- When upgrading from trial
getDaysUntilReset()
Calculates days remaining until billing cycle reset.
Location: src/lib/billing-cycle.ts:121
Plan Limits
Tier Configuration
Location:src/lib/billing-cycle.ts:172
Plan Comparison
| Plan | Reports/Cycle | Daily Average | Cost/Report* |
|---|---|---|---|
| FREE | 5 | ~0.17 | $0 |
| STARTER | 25 | ~0.83 | $1.20 |
| PROFESSIONAL | 75 | ~2.5 | $0.67 |
| AGENCY | 250 | ~8.3 | $0.40 |
Integration Points
Report Generation Checks
API Route:src/app/api/reports/generate/route.ts (planned)
Dashboard Display
Component:src/components/organisms/UsageCard.tsx (planned)
Middleware Protection
Middleware:src/middleware.ts (planned)
Upgrade/Downgrade Handling
Plan Changes
When user upgrades or downgrades:Edge Cases
First User Creation
Problem: New users have null billing cycle datesSolution: Initialize cycle in signup flow
Clock Skew
Problem: Server time vs database time differencesSolution: Always use database-generated timestamps
Timezone Handling
Problem: Users in different timezonesSolution: Store all dates in UTC, display in user’s timezone
Concurrent Report Generation
Problem: Race condition when multiple reports created simultaneouslySolution: Database-level checks or row locking
Performance Considerations
Caching Strategy
Cache cycle info to reduce database queries:Query Optimization
Index on createdAt for fast counting:Testing
Unit Tests
Monitoring
Metrics to Track
- Cycle Resets - Count of automatic resets per day
- Limit Breaches - Failed report attempts due to limits
- Utilization - Average % of limit used per plan
- Reset Lag - Time between cycle expiry and actual reset