44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from datetime import UTC, datetime
|
|
from enum import StrEnum
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import BIGINT, INTEGER, TEXT, DateTime, Enum, ForeignKey
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from db.base import Base
|
|
|
|
if TYPE_CHECKING:
|
|
pass
|
|
|
|
|
|
class BalanceTxType(StrEnum):
|
|
DEPOSIT = "deposit"
|
|
PURCHASE = "purchase"
|
|
REFUND = "refund"
|
|
REFERRAL_BONUS = "referral"
|
|
MANUAL = "manual"
|
|
AUTORENEW = "autorenew"
|
|
MANUAL_RENEW = "manual_renew"
|
|
|
|
|
|
class BalanceTransaction(Base):
|
|
__tablename__ = "balance_transactions"
|
|
|
|
id: Mapped[int] = mapped_column(BIGINT, autoincrement=True, primary_key=True)
|
|
user_id: Mapped[int] = mapped_column(BIGINT, ForeignKey("users.id"), nullable=False, index=True)
|
|
amount: Mapped[int] = mapped_column(INTEGER, nullable=False)
|
|
|
|
tx_type: Mapped[BalanceTxType] = mapped_column(
|
|
Enum(BalanceTxType, name="balancetxtype", values_callable=lambda x: [e.value for e in x]),
|
|
nullable=False,
|
|
)
|
|
balance_before: Mapped[int] = mapped_column(INTEGER, nullable=False)
|
|
balance_after: Mapped[int] = mapped_column(INTEGER, nullable=False)
|
|
description: Mapped[str] = mapped_column(TEXT, nullable=True)
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
nullable=False,
|
|
default=lambda: datetime.now(UTC),
|
|
)
|