diff options
| author | Ben Sima <ben@bsima.me> | 2025-11-19 17:42:31 -0500 |
|---|---|---|
| committer | Ben Sima <ben@bsima.me> | 2025-11-19 17:42:31 -0500 |
| commit | d15f9fbae7ea4cac1e7c06a7d911a3ccfd6ef5ba (patch) | |
| tree | ef415f0be87b8fd1a561bc97e2a088e30264cdd8 /Biz/PodcastItLater/Core.py | |
| parent | d1613e8ed216135924067fe70ece215379203b73 (diff) | |
Implement usage tracking and limits enforcement
- Fix get_usage() to count from user_episodes table instead of
episodes.user_id
- Now correctly tracks when episodes are added to user's feed -
Handles shared/existing episodes properly (count against the user
who added them)
- Add comprehensive test suite for usage limits (TestUsageLimits):
- test_usage_counts_episodes_added_to_feed -
test_usage_counts_existing_episodes_correctly -
test_free_tier_limit_enforcement (10 articles) -
test_can_submit_blocks_at_limit - test_paid_tier_unlimited
- Billing.can_submit() now properly enforces 10 article limit
for free tier - Usage tracking via user_episodes.added_at ensures
accurate billing
Completes t-144eKR1
Diffstat (limited to 'Biz/PodcastItLater/Core.py')
| -rw-r--r-- | Biz/PodcastItLater/Core.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/Biz/PodcastItLater/Core.py b/Biz/PodcastItLater/Core.py index f1f89e9..8d31956 100644 --- a/Biz/PodcastItLater/Core.py +++ b/Biz/PodcastItLater/Core.py @@ -1382,18 +1382,24 @@ class Database: # noqa: PLR0904 ) -> dict[str, int]: """Get usage stats for user in period. + Counts episodes added to user's feed (via user_episodes table) + during the billing period, regardless of who created them. + Returns: dict with keys: articles (int), minutes (int) """ with Database.get_connection() as conn: cursor = conn.cursor() - # Count articles created in period + # Count articles added to user's feed in period + # Uses user_episodes junction table to track when episodes + # were added, which correctly handles shared/existing episodes cursor.execute( """ - SELECT COUNT(*) as count, SUM(duration) as total_seconds - FROM episodes - WHERE user_id = ? AND created_at >= ? AND created_at < ? + SELECT COUNT(*) as count, SUM(e.duration) as total_seconds + FROM user_episodes ue + JOIN episodes e ON e.id = ue.episode_id + WHERE ue.user_id = ? AND ue.added_at >= ? AND ue.added_at < ? """, (user_id, period_start.isoformat(), period_end.isoformat()), ) |
