feat: hwid limits and topups

This commit is contained in:
3252a8
2026-05-03 22:36:33 +03:00
parent 00f8e5fb20
commit aa5a9496ed
21 changed files with 738 additions and 15 deletions
+19 -1
View File
@@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional
from sqlalchemy import and_, delete, select
from sqlalchemy.ext.asyncio import AsyncSession
from db.models import TariffChange, TrafficTopup, TrafficWarning
from db.models import HwidDevicePurchase, TariffChange, TrafficTopup, TrafficWarning
async def create_traffic_topup(
@@ -26,6 +26,24 @@ async def create_traffic_topup(
return record
async def create_hwid_device_purchase(
session: AsyncSession,
*,
subscription_id: int,
payment_id: Optional[int],
purchased_devices: int,
) -> HwidDevicePurchase:
record = HwidDevicePurchase(
subscription_id=subscription_id,
payment_id=payment_id,
purchased_devices=purchased_devices,
)
session.add(record)
await session.flush()
await session.refresh(record)
return record
async def create_tariff_change(
session: AsyncSession,
change_data: Dict[str, Any],
+21
View File
@@ -320,6 +320,10 @@ def _migration_0011_add_tariffs_schema(connection: Connection) -> None:
sub_statements.append("ALTER TABLE subscriptions ADD COLUMN is_throttled BOOLEAN NOT NULL DEFAULT FALSE")
if "effective_monthly_price_rub" not in sub_columns:
sub_statements.append("ALTER TABLE subscriptions ADD COLUMN effective_monthly_price_rub NUMERIC")
if "hwid_device_limit" not in sub_columns:
sub_statements.append("ALTER TABLE subscriptions ADD COLUMN hwid_device_limit INTEGER")
if "extra_hwid_devices" not in sub_columns:
sub_statements.append("ALTER TABLE subscriptions ADD COLUMN extra_hwid_devices INTEGER NOT NULL DEFAULT 0")
for stmt in sub_statements:
connection.execute(text(stmt))
@@ -331,6 +335,8 @@ def _migration_0011_add_tariffs_schema(connection: Connection) -> None:
payment_statements.append("ALTER TABLE payments ADD COLUMN tariff_key VARCHAR")
if "purchased_gb" not in payment_columns:
payment_statements.append("ALTER TABLE payments ADD COLUMN purchased_gb DOUBLE PRECISION")
if "purchased_hwid_devices" not in payment_columns:
payment_statements.append("ALTER TABLE payments ADD COLUMN purchased_hwid_devices INTEGER")
for stmt in payment_statements:
connection.execute(text(stmt))
@@ -363,6 +369,19 @@ def _migration_0011_add_tariffs_schema(connection: Connection) -> None:
"""
)
)
connection.execute(
text(
"""
CREATE TABLE IF NOT EXISTS hwid_device_purchases (
purchase_id SERIAL PRIMARY KEY,
subscription_id INTEGER NOT NULL REFERENCES subscriptions(subscription_id),
payment_id INTEGER NULL REFERENCES payments(payment_id),
purchased_devices INTEGER NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
"""
)
)
connection.execute(
text(
"""
@@ -393,6 +412,8 @@ def _migration_0011_add_tariffs_schema(connection: Connection) -> None:
"CREATE INDEX IF NOT EXISTS ix_traffic_topups_kind ON traffic_topups (kind)",
"CREATE INDEX IF NOT EXISTS ix_traffic_warnings_subscription_id ON traffic_warnings (subscription_id)",
"CREATE INDEX IF NOT EXISTS ix_tariff_changes_subscription_id ON tariff_changes (subscription_id)",
"CREATE INDEX IF NOT EXISTS ix_hwid_device_purchases_subscription_id ON hwid_device_purchases (subscription_id)",
"CREATE INDEX IF NOT EXISTS ix_hwid_device_purchases_payment_id ON hwid_device_purchases (payment_id)",
]:
connection.execute(text(stmt))
+16
View File
@@ -93,6 +93,8 @@ class Subscription(Base):
period_start_at = Column(DateTime(timezone=True), nullable=True)
is_throttled = Column(Boolean, nullable=False, default=False, index=True)
effective_monthly_price_rub = Column(Numeric, nullable=True)
hwid_device_limit = Column(Integer, nullable=True)
extra_hwid_devices = Column(Integer, nullable=False, default=0)
user = relationship("User", back_populates="subscriptions")
@@ -167,6 +169,7 @@ class Payment(Base):
sale_mode = Column(String, nullable=True, index=True)
tariff_key = Column(String, nullable=True, index=True)
purchased_gb = Column(Float, nullable=True)
purchased_hwid_devices = Column(Integer, nullable=True)
promo_code_id = Column(Integer,
ForeignKey("promo_codes.promo_code_id"),
nullable=True)
@@ -194,6 +197,19 @@ class TrafficTopup(Base):
payment = relationship("Payment")
class HwidDevicePurchase(Base):
__tablename__ = "hwid_device_purchases"
purchase_id = Column(Integer, primary_key=True, autoincrement=True)
subscription_id = Column(Integer, ForeignKey("subscriptions.subscription_id"), nullable=False, index=True)
payment_id = Column(Integer, ForeignKey("payments.payment_id"), nullable=True, index=True)
purchased_devices = Column(Integer, nullable=False)
created_at = Column(DateTime(timezone=True), server_default=func.now())
subscription = relationship("Subscription")
payment = relationship("Payment")
class TrafficWarning(Base):
__tablename__ = "traffic_warnings"
__table_args__ = (