File size: 5,466 Bytes
22087d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
import asyncio
import sys
from pathlib import Path
from typing import Dict
from datetime import datetime, timezone
from decimal import Decimal
import time

backend_dir = Path(__file__).parent.parent.parent
sys.path.insert(0, str(backend_dir))

import stripe
from core.services.supabase import DBConnection
from core.utils.config import config
from core.utils.logger import logger
from core.billing.shared.config import TIERS
from core.billing.credits.manager import credit_manager

stripe.api_key = config.STRIPE_SECRET_KEY

class GrantMissingCreditsService:
    def __init__(self):
        self.db = DBConnection()
        self.stats = {
            'total_users_checked': 0,
            'credits_granted': 0,
            'already_has_credits': 0,
            'errors': 0,
            'start_time': time.time()
        }
    
    async def initialize(self):
        await self.db.initialize()
        self.client = await self.db.client
    
    async def run(self):
        print("\n" + "="*60)
        print("GRANT MISSING CREDITS FOR PAID TIERS")
        print("="*60)
        print("πŸ” Finding users with paid tiers but no credits...")
        print("="*60)
        
        await self.initialize()
        await self.grant_missing_credits()
        self.print_stats()
    
    async def grant_missing_credits(self):
        print("\nπŸ’° CHECKING USERS WITH PAID TIERS...")
        
        # Get all users with paid tiers (not 'none' or 'free')
        users_result = await self.client.from_('credit_accounts')\
            .select('account_id, tier, balance, stripe_subscription_id')\
            .not_.in_('tier', ['none', 'free'])\
            .execute()
        
        if not users_result.data:
            print("  No users with paid tiers found")
            return
        
        print(f"  Found {len(users_result.data)} users with paid tiers")
        
        for user in users_result.data:
            await self.process_user(user)
        
        print(f"\n  βœ… Processed {self.stats['total_users_checked']} users")
    
    async def process_user(self, user: Dict):
        account_id = user['account_id']
        tier_name = user['tier']
        current_balance = Decimal(str(user.get('balance', 0)))
        stripe_sub_id = user.get('stripe_subscription_id')
        
        self.stats['total_users_checked'] += 1
        
        # Get tier configuration
        tier = TIERS.get(tier_name)
        if not tier:
            logger.warning(f"Unknown tier {tier_name} for account {account_id}")
            return
        
        # Check if user has very low balance (less than 1 credit)
        # This catches users who should have credits but don't
        if current_balance < Decimal('1.0'):
            try:
                # Check if this is a trial subscription
                is_trial = False
                if stripe_sub_id:
                    try:
                        subscription = await stripe.Subscription.retrieve_async(
                            stripe_sub_id,
                            expand=['items.data.price']
                        )
                        is_trial = subscription.status == 'trialing'
                    except Exception as e:
                        logger.debug(f"Could not retrieve subscription {stripe_sub_id}: {e}")
                
                # Grant the appropriate credits
                credits_to_grant = tier.monthly_credits
                description = f"Initial credits for {tier.display_name}"
                
                if is_trial:
                    description = f"Trial credits for {tier.display_name}"
                
                logger.info(f"Granting {credits_to_grant} credits to {account_id} (tier: {tier_name}, current balance: {current_balance})")
                
                await credit_manager.add_credits(
                    account_id=account_id,
                    amount=credits_to_grant,
                    is_expiring=True,
                    description=description
                )
                
                self.stats['credits_granted'] += 1
                print(f"  βœ… Granted {credits_to_grant} credits to user with tier {tier_name}")
                
            except Exception as e:
                logger.error(f"Error granting credits to {account_id}: {e}")
                self.stats['errors'] += 1
        else:
            self.stats['already_has_credits'] += 1
            logger.debug(f"User {account_id} already has {current_balance} credits")
    
    def print_stats(self):
        elapsed = time.time() - self.stats['start_time']
        
        print("\n" + "="*60)
        print("CREDIT GRANT COMPLETE")
        print("="*60)
        print(f"⏱️  Time taken: {elapsed:.2f} seconds")
        print(f"πŸ‘₯ Total users checked: {self.stats['total_users_checked']}")
        print(f"πŸ’° Credits granted to: {self.stats['credits_granted']} users")
        print(f"βœ… Already had credits: {self.stats['already_has_credits']} users")
        print(f"❌ Errors: {self.stats['errors']}")
        print("="*60)

async def main():
    service = GrantMissingCreditsService()
    try:
        await service.run()
    except Exception as e:
        print(f"\n❌ FATAL ERROR: {e}")
        logger.error(f"Credit grant failed: {e}", exc_info=True)
        sys.exit(1)

if __name__ == "__main__":
    print("Starting missing credits grant service...")
    asyncio.run(main())