File size: 3,604 Bytes
d05a9d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datetime import datetime
import json
import os

# Simple file-based storage for subscriptions
SUBSCRIPTIONS_FILE = 'data/subscriptions.json'

# Create data directory if it doesn't exist
os.makedirs(os.path.dirname(SUBSCRIPTIONS_FILE), exist_ok=True)

# Create empty subscriptions file if it doesn't exist
if not os.path.exists(SUBSCRIPTIONS_FILE):
    with open(SUBSCRIPTIONS_FILE, 'w') as f:
        json.dump({}, f)


class Subscription:
    def __init__(self, user_id, plan_id, start_date, end_date, is_active=True):
        self.user_id = user_id
        self.plan_id = plan_id
        self.start_date = start_date
        self.end_date = end_date
        self.is_active = is_active
        self.created_at = datetime.now().isoformat()
    
    @staticmethod
    def get_by_user_id(user_id):
        """Get subscription by user ID"""
        try:
            with open(SUBSCRIPTIONS_FILE, 'r') as f:
                subscriptions = json.load(f)
            
            user_subscription = subscriptions.get(str(user_id))
            if user_subscription:
                return Subscription(
                    user_id=user_subscription['user_id'],
                    plan_id=user_subscription['plan_id'],
                    start_date=user_subscription['start_date'],
                    end_date=user_subscription['end_date'],
                    is_active=user_subscription['is_active']
                )
            return None
        except Exception:
            return None
    
    @staticmethod
    def create_or_update(user_id, plan_id, start_date, end_date, is_active=True):
        """Create or update subscription"""
        try:
            # Load existing subscriptions
            if os.path.exists(SUBSCRIPTIONS_FILE):
                with open(SUBSCRIPTIONS_FILE, 'r') as f:
                    subscriptions = json.load(f)
            else:
                subscriptions = {}
            
            # Update or create subscription
            subscriptions[str(user_id)] = {
                'user_id': user_id,
                'plan_id': plan_id,
                'start_date': start_date,
                'end_date': end_date,
                'is_active': is_active,
                'created_at': datetime.now().isoformat()
            }
            
            # Save subscriptions
            with open(SUBSCRIPTIONS_FILE, 'w') as f:
                json.dump(subscriptions, f, indent=2)
            
            return Subscription(user_id, plan_id, start_date, end_date, is_active)
        except Exception as e:
            print(f"Error creating/updating subscription: {e}")
            return None
    
    @staticmethod
    def cancel(user_id):
        """Cancel subscription for user"""
        try:
            # Load existing subscriptions
            if os.path.exists(SUBSCRIPTIONS_FILE):
                with open(SUBSCRIPTIONS_FILE, 'r') as f:
                    subscriptions = json.load(f)
            else:
                return False
            
            # Update subscription status
            if str(user_id) in subscriptions:
                subscriptions[str(user_id)]['is_active'] = False
                
                # Save subscriptions
                with open(SUBSCRIPTIONS_FILE, 'w') as f:
                    json.dump(subscriptions, f, indent=2)
                
                return True
            return False
        except Exception as e:
            print(f"Error canceling subscription: {e}")
            return False