From 14a0c13068df74d82856ba8cc96ca428e0d0ce6f Mon Sep 17 00:00:00 2001 From: Elora VPN Date: Fri, 15 Sep 2023 22:52:22 +0330 Subject: [PATCH] Add commerce service models, Related to #15 --- src/accounts/models.py | 1 + src/commerce/__init__.py | 0 src/commerce/models.py | 91 ++++++++++++++++++++++++++++++++++++++++ src/commerce/schemas.py | 32 ++++++++++++++ src/users/models.py | 3 ++ 5 files changed, 127 insertions(+) create mode 100644 src/commerce/__init__.py create mode 100644 src/commerce/models.py create mode 100644 src/commerce/schemas.py diff --git a/src/accounts/models.py b/src/accounts/models.py index 88cdd61..4eb786c 100644 --- a/src/accounts/models.py +++ b/src/accounts/models.py @@ -29,6 +29,7 @@ class Account(Base): notification = relationship( "Notification", back_populates="account", cascade="all, delete-orphan" ) + orders = relationship("Order", back_populates="account") uuid = Column(String(128), index=True, unique=True, nullable=False) email = Column(String(128), index=True, unique=True, nullable=False) enable = Column(Boolean, default=True) diff --git a/src/commerce/__init__.py b/src/commerce/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/commerce/models.py b/src/commerce/models.py new file mode 100644 index 0000000..1af1ecb --- /dev/null +++ b/src/commerce/models.py @@ -0,0 +1,91 @@ +from datetime import datetime + +from sqlalchemy import ( + Column, + DateTime, + Enum, + Integer, + String, + Boolean, BigInteger, ForeignKey, +) + +from sqlalchemy.orm import relationship + +from src.commerce.schemas import PaymentMethod, TransactionType, PaymentStatus, OrderStatus +from src.database import Base + + +class Transaction(Base): + __tablename__ = "transaction" + id = Column(Integer, primary_key=True, index=True) + user_id = Column(Integer, ForeignKey("user.id")) + user = relationship("User", back_populates="transactions") + # payment_id = Column(Integer, ForeignKey("payment.id"), nullable=True) + # order_id = Column(Integer, ForeignKey("order.id"), nullable=True) + description = Column(String(4000), nullable=True) + + amount = Column(BigInteger, default=0) + + type = Column(Enum(TransactionType), nullable=False, + default=TransactionType.payment) + + created_at = Column(DateTime, default=datetime.utcnow) + modified_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + + +class Payment(Base): + __tablename__ = "payment" + + id = Column(Integer, primary_key=True, index=True) + user_id = Column(Integer, ForeignKey("user.id")) + user = relationship("User", back_populates="payments") + + order_id = Column(Integer, ForeignKey("order.id"), nullable=True) + order = relationship("Order", back_populates="payments") + + method = Column(Enum(PaymentMethod), nullable=False, + default=PaymentMethod.money_order) + + total = Column(BigInteger, default=0) + + status = Column(Enum(PaymentStatus), nullable=False, + default=PaymentStatus.pending) + + created_at = Column(DateTime, default=datetime.utcnow) + modified_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + + +class Order(Base): + __tablename__ = "order" + id = Column(Integer, primary_key=True, index=True) + user_id = Column(Integer, ForeignKey("user.id")) + user = relationship("User", back_populates="orders") + + account_id = Column(Integer, ForeignKey("account.id"), nullable=True) + account = relationship("Account", back_populates="orders") + + payments = relationship("Payment", back_populates="order") + + status = Column(Enum(OrderStatus), nullable=False, + default=OrderStatus.open) + + duration = Column(Integer, default=1) + data_limit = Column(BigInteger, nullable=True) + + total = Column(BigInteger, default=0) + total_discount_amount = Column(BigInteger, default=0) + + created_at = Column(DateTime, default=datetime.utcnow) + modified_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) + + +class Service(Base): + __tablename__ = "service" + + id = Column(Integer, primary_key=True, index=True) + name = Column(String(128), nullable=True) + + duration = Column(Integer, default=1) + data_limit = Column(BigInteger, nullable=True) + price = Column(BigInteger, default=0) + discount = Column(Integer, default=0) diff --git a/src/commerce/schemas.py b/src/commerce/schemas.py new file mode 100644 index 0000000..128d410 --- /dev/null +++ b/src/commerce/schemas.py @@ -0,0 +1,32 @@ +from enum import Enum +from typing import List + +from pydantic import BaseModel, root_validator, validator + + +class TransactionType(str, Enum): + payment = "PAYMENT" + order = "ORDER" + bonus = "BONUS" + + +class PaymentMethod(str, Enum): + money_order = "MONEY_ORDER" + online = "ONLINE" + cryptocurrencies = "CRYPTOCURRENCIES" + + +class PaymentStatus(str, Enum): + pending = "PENDING" + paid = "PAID" + + +class OrderStatus(str, Enum): + open = "OPEN" + pending = "PENDING" + paid = "PAID" + completed = "COMPLETED" + + +class Transaction(BaseModel): + description: str diff --git a/src/users/models.py b/src/users/models.py index 755a1dd..569dd5e 100644 --- a/src/users/models.py +++ b/src/users/models.py @@ -23,6 +23,9 @@ class User(Base): accounts = relationship( "Account", back_populates="user", cascade="all, delete-orphan" ) + orders = relationship("Order", back_populates="user") + payments = relationship("Payment", back_populates="user") + transaction = relationship("Transaction", back_populates="user") username = Column(String(128), unique=True, index=True, nullable=False) hashed_password = Column(String(128)) first_name = Column(String(128), index=True, nullable=True)