2025-01-09 20:52:46 +00:00
|
|
|
from sqlalchemy import (
|
|
|
|
create_engine, Column, Integer, String, DateTime,
|
|
|
|
ForeignKey, Enum, Text, JSON, Boolean, Float
|
|
|
|
)
|
2024-12-30 06:03:07 +00:00
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
|
from sqlalchemy.orm import relationship
|
|
|
|
import enum
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
class SubscriptionStatus(enum.Enum):
|
|
|
|
ACTIVE = "active"
|
|
|
|
EXPIRED = "expired"
|
|
|
|
PENDING = "pending"
|
|
|
|
CANCELLED = "cancelled"
|
2025-01-09 20:52:46 +00:00
|
|
|
FAILED = "failed" # New status for failed provisions
|
|
|
|
SUSPENDED = "suspended" # New status for temp suspension
|
|
|
|
|
|
|
|
class LogLevel(enum.Enum):
|
|
|
|
DEBUG = "debug"
|
|
|
|
INFO = "info"
|
|
|
|
WARNING = "warning"
|
|
|
|
ERROR = "error"
|
2024-12-30 06:03:07 +00:00
|
|
|
|
|
|
|
class User(Base):
|
|
|
|
__tablename__ = 'users'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(String, unique=True, nullable=False) # UUID generated in frontend
|
|
|
|
created_at = Column(DateTime, default=datetime.datetime.utcnow)
|
2025-01-09 20:52:46 +00:00
|
|
|
last_login = Column(DateTime, nullable=True) # New: Track last login time
|
|
|
|
is_active = Column(Boolean, default=True, nullable=True) # New: User account status
|
|
|
|
user_data = Column(JSON, nullable=True) # New: Optional user metadata
|
2024-12-30 06:03:07 +00:00
|
|
|
|
|
|
|
subscriptions = relationship("Subscription", back_populates="user")
|
|
|
|
payments = relationship("Payment", back_populates="user")
|
2025-01-09 20:52:46 +00:00
|
|
|
provision_logs = relationship("ProvisionLog", back_populates="user") # New relationship
|
2024-12-30 06:03:07 +00:00
|
|
|
|
|
|
|
class Subscription(Base):
|
|
|
|
__tablename__ = 'subscriptions'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
invoice_id = Column(String, unique=True)
|
|
|
|
public_key = Column(Text, nullable=False) # WireGuard public key
|
|
|
|
start_time = Column(DateTime, nullable=False)
|
|
|
|
expiry_time = Column(DateTime, nullable=False)
|
|
|
|
status = Column(Enum(SubscriptionStatus), default=SubscriptionStatus.PENDING)
|
|
|
|
warning_sent = Column(Integer, default=0)
|
|
|
|
assigned_ip = Column(String) # WireGuard IP address assigned to this subscription
|
|
|
|
|
2025-01-09 20:52:46 +00:00
|
|
|
# New fields for monitoring
|
|
|
|
last_connection = Column(DateTime, nullable=True) # Track last connection time
|
|
|
|
data_usage = Column(Float, default=0.0) # Track data usage in MB
|
|
|
|
is_test = Column(Boolean, default=False) # Flag for test subscriptions
|
|
|
|
provision_attempts = Column(Integer, default=0) # Count provision attempts
|
|
|
|
cleanup_attempts = Column(Integer, default=0) # Count cleanup attempts
|
|
|
|
config_data = Column(JSON, nullable=True) # Store additional config data
|
|
|
|
|
2024-12-30 06:03:07 +00:00
|
|
|
user = relationship("User", back_populates="subscriptions")
|
|
|
|
payments = relationship("Payment", back_populates="subscription")
|
2025-01-09 20:52:46 +00:00
|
|
|
provision_logs = relationship("ProvisionLog", back_populates="subscription")
|
2024-12-30 06:03:07 +00:00
|
|
|
|
|
|
|
class Payment(Base):
|
|
|
|
__tablename__ = 'payments'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
subscription_id = Column(Integer, ForeignKey('subscriptions.id'))
|
|
|
|
invoice_id = Column(String, unique=True)
|
|
|
|
amount = Column(Integer, nullable=False) # Amount in sats
|
|
|
|
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
|
2025-01-09 20:52:46 +00:00
|
|
|
# New payment tracking fields
|
|
|
|
payment_method = Column(String, nullable=True) # Payment method used
|
|
|
|
payment_status = Column(String, nullable=True) # Payment status
|
|
|
|
confirmations = Column(Integer, default=0) # Number of confirmations
|
|
|
|
payment_data = Column(JSON, nullable=True) # Additional payment data
|
|
|
|
|
2024-12-30 06:03:07 +00:00
|
|
|
user = relationship("User", back_populates="payments")
|
2025-01-09 20:52:46 +00:00
|
|
|
subscription = relationship("Subscription", back_populates="payments")
|
|
|
|
|
|
|
|
class ProvisionLog(Base):
|
|
|
|
__tablename__ = 'provision_logs'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
subscription_id = Column(Integer, ForeignKey('subscriptions.id'))
|
|
|
|
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
|
|
|
|
action = Column(String, nullable=False) # 'provision' or 'cleanup'
|
|
|
|
status = Column(String, nullable=False) # 'success' or 'failure'
|
|
|
|
ansible_output = Column(Text, nullable=True) # Store Ansible output
|
|
|
|
error_message = Column(Text, nullable=True) # Store error messages
|
|
|
|
|
|
|
|
user = relationship("User", back_populates="provision_logs")
|
|
|
|
subscription = relationship("Subscription", back_populates="provision_logs")
|