feat(order): Add finalize order functionality
This commit is contained in:
@@ -58,7 +58,7 @@ class User(Base):
|
||||
@property
|
||||
def shopping_cart(self):
|
||||
for order in self.orders:
|
||||
if order.account_id is None:
|
||||
if order.transaction is None:
|
||||
cart = order
|
||||
break
|
||||
else:
|
||||
@@ -109,10 +109,7 @@ class Order(Base):
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey(TABLE_PREFIX + "user.id"))
|
||||
user: Mapped[User] = relationship("User", back_populates="orders")
|
||||
|
||||
account_id: Mapped[int] = mapped_column(
|
||||
ForeignKey(TABLE_PREFIX + "account.id"), nullable=True
|
||||
)
|
||||
account: Mapped[Account | None] = relationship("Account")
|
||||
transaction: Mapped["Transaction | None"] = relationship("Transaction")
|
||||
|
||||
items: Mapped[list["OrderItem"]] = relationship(
|
||||
"OrderItem", cascade="all, delete-orphan", back_populates="order"
|
||||
@@ -120,12 +117,37 @@ class Order(Base):
|
||||
|
||||
@property
|
||||
def is_in_shopping_cart(self):
|
||||
return self.account is None
|
||||
return self.transaction is None
|
||||
|
||||
@property
|
||||
def total_amount(self):
|
||||
return sum(item.total_amount for item in self.items)
|
||||
|
||||
def finalize(self, account: Account):
|
||||
"""
|
||||
Moves the order from the shopping cart to a given account
|
||||
and adds a transaction to the account.
|
||||
|
||||
:param account: The account to which the order should be finalized
|
||||
:raises ValueError: If the order is already finalized or empty"""
|
||||
if not self.is_in_shopping_cart:
|
||||
raise ValueError("Order is already finalized.")
|
||||
|
||||
if not self.items:
|
||||
raise ValueError("Cannot finalize an empty order.")
|
||||
|
||||
assert account in self.user.accounts, "Account does not belong to user."
|
||||
|
||||
# create a transaction for the order
|
||||
transaction = Transaction(
|
||||
type="order",
|
||||
total_amount=-self.total_amount,
|
||||
order=self,
|
||||
account=account,
|
||||
)
|
||||
session = object_session(self)
|
||||
session.add(transaction)
|
||||
|
||||
|
||||
class OrderItem(Base):
|
||||
__tablename__ = TABLE_PREFIX + "order_item"
|
||||
@@ -142,7 +164,7 @@ class OrderItem(Base):
|
||||
|
||||
|
||||
TransactionTypes = typing.Literal[
|
||||
"product",
|
||||
"order",
|
||||
"deposit",
|
||||
"withdrawal",
|
||||
"expense",
|
||||
@@ -161,10 +183,10 @@ class Transaction(Base):
|
||||
Numeric(10, 2), nullable=False
|
||||
)
|
||||
|
||||
product_id: Mapped[int] = mapped_column(
|
||||
ForeignKey(TABLE_PREFIX + "product.id"), nullable=True
|
||||
order_id: Mapped[int] = mapped_column(
|
||||
ForeignKey(TABLE_PREFIX + "order.id"), nullable=True
|
||||
)
|
||||
product: Mapped["Product"] = relationship("Product")
|
||||
order: Mapped["Order"] = relationship("Order", back_populates="transaction")
|
||||
|
||||
account_id: Mapped[int] = mapped_column(ForeignKey(TABLE_PREFIX + "account.id"))
|
||||
account: Mapped["Account"] = relationship("Account", back_populates="transactions")
|
||||
|
||||
Reference in New Issue
Block a user