diff options
Diffstat (limited to 'models/payment.py')
| -rw-r--r-- | models/payment.py | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/models/payment.py b/models/payment.py index 2b1cb90..afae642 100644 --- a/models/payment.py +++ b/models/payment.py | |||
| @@ -1,16 +1,64 @@ | |||
| 1 | from datetime import datetime | 1 | from datetime import datetime |
| 2 | from enum import StrEnum, auto | ||
| 2 | 3 | ||
| 3 | from sqlalchemy import ForeignKey | 4 | from aiogram import Bot |
| 5 | from aiogram.types import Message, ReplyMarkupUnion | ||
| 6 | from pydantic import BaseModel | ||
| 7 | from sqlalchemy import JSON, ForeignKey | ||
| 4 | from sqlalchemy.orm import Mapped, mapped_column | 8 | from sqlalchemy.orm import Mapped, mapped_column |
| 5 | 9 | ||
| 6 | from models import BaseTable, Invoice, User | 10 | from models import BaseTable, Invoice, User |
| 7 | 11 | ||
| 8 | 12 | ||
| 13 | class ReceiptFileType(StrEnum): | ||
| 14 | PHOTO = auto() | ||
| 15 | DOCUMENT = auto() | ||
| 16 | |||
| 17 | |||
| 18 | class ReceiptFile(BaseModel): | ||
| 19 | type: ReceiptFileType | ||
| 20 | file_id: str | ||
| 21 | |||
| 22 | async def send( | ||
| 23 | self, | ||
| 24 | bot: Bot, | ||
| 25 | user_id: int, | ||
| 26 | reply_markup: ReplyMarkupUnion | None = None, | ||
| 27 | ) -> Message: | ||
| 28 | if self.type == ReceiptFileType.DOCUMENT: | ||
| 29 | return await bot.send_document( | ||
| 30 | user_id, | ||
| 31 | document=self.file_id, | ||
| 32 | reply_markup=reply_markup, | ||
| 33 | ) | ||
| 34 | else: | ||
| 35 | return await bot.send_photo( | ||
| 36 | user_id, | ||
| 37 | photo=self.file_id, | ||
| 38 | reply_markup=reply_markup, | ||
| 39 | ) | ||
| 40 | |||
| 41 | |||
| 42 | class PaymentStatus(StrEnum): | ||
| 43 | PENDING = auto() | ||
| 44 | ACCEPTED = auto() | ||
| 45 | REJECTED = auto() | ||
| 46 | |||
| 47 | |||
| 9 | class Payment(BaseTable): | 48 | class Payment(BaseTable): |
| 10 | __tablename__ = "payment" | 49 | __tablename__ = "payment" |
| 11 | 50 | ||
| 12 | id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) | 51 | id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True) |
| 13 | user_id: Mapped[int] = mapped_column(ForeignKey(User.id)) | 52 | user_id: Mapped[int] = mapped_column(ForeignKey(User.id)) |
| 14 | invoice_id: Mapped[int] = mapped_column(ForeignKey(Invoice.id)) | 53 | invoice_id: Mapped[int] = mapped_column(ForeignKey(Invoice.id)) |
| 15 | receipt_file_id: Mapped[str] | 54 | __receipt_file: Mapped[str] = mapped_column("receipt_file", JSON()) |
| 55 | status: Mapped[PaymentStatus] = mapped_column(default=PaymentStatus.PENDING) | ||
| 16 | datetime: Mapped[datetime] | 56 | datetime: Mapped[datetime] |
| 57 | |||
| 58 | @property | ||
| 59 | def receipt_file(self) -> ReceiptFile: | ||
| 60 | return ReceiptFile.model_validate_json(self.__receipt_file) | ||
| 61 | |||
| 62 | @receipt_file.setter | ||
| 63 | def receipt_file(self, value: ReceiptFile) -> None: | ||
| 64 | self.__receipt_file = value.model_dump_json() | ||
