Skip to content

Commit

Permalink
Add commerce service models, Related to #15
Browse files Browse the repository at this point in the history
  • Loading branch information
eloravpn committed Oct 14, 2023
1 parent 10ed1a8 commit 14a0c13
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Empty file added src/commerce/__init__.py
Empty file.
91 changes: 91 additions & 0 deletions src/commerce/models.py
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions src/commerce/schemas.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 14a0c13

Please sign in to comment.