From d15f9fbae7ea4cac1e7c06a7d911a3ccfd6ef5ba Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Wed, 19 Nov 2025 17:42:31 -0500 Subject: 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 --- Biz/PodcastItLater/Core.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'Biz/PodcastItLater/Core.py') 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()), ) -- cgit v1.2.3