fix bugs, implement placing orders

This commit is contained in:
David Rodenkirchen
2024-09-03 17:12:36 +02:00
parent 30b32a4c02
commit 1ca7db6427
4 changed files with 93 additions and 24 deletions
+13 -4
View File
@@ -1,4 +1,5 @@
import logging
from enum import Enum
from typing import Optional
from src.ez_lan_manager.services.AccountingService import AccountingService
@@ -9,9 +10,15 @@ from src.ez_lan_manager.types.CateringMenuItem import CateringMenuItem, Catering
logger = logging.getLogger(__name__.split(".")[-1])
class CateringErrorType(Enum):
INCLUDES_DISABLED_ITEM = 0
INSUFFICIENT_FUNDS = 1
GENERIC = 99
class CateringError(Exception):
def __init__(self, message: str) -> None:
def __init__(self, message: str, error_type: CateringErrorType = CateringErrorType.GENERIC) -> None:
self.message = message
self.error_type = error_type
class CateringService:
@@ -26,7 +33,7 @@ class CateringService:
async def place_order(self, menu_items: CateringMenuItemsWithAmount, user_id: int, is_delivery: bool = True) -> CateringOrder:
for menu_item in menu_items:
if menu_item.is_disabled:
raise CateringError("Order includes disabled items")
raise CateringError("Order includes disabled items", CateringErrorType.INCLUDES_DISABLED_ITEM)
user = await self._user_service.get_user(user_id)
if not user:
@@ -34,12 +41,13 @@ class CateringService:
total_price = sum([item.price * quantity for item, quantity in menu_items.items()])
if await self._accounting_service.get_balance(user_id) < total_price:
raise CateringError("Insufficient funds")
raise CateringError("Insufficient funds", CateringErrorType.INSUFFICIENT_FUNDS)
order = await self._db_service.add_new_order(menu_items, user_id, is_delivery)
if order:
await self._accounting_service.remove_balance(user_id, total_price, f"CATERING - {order.order_id}")
logger.info(f"User '{order.customer.user_name}' (ID:{order.customer.user_id}) ordered from catering for {self._accounting_service.make_euro_string_from_int(total_price)}")
# await self.cancel_order(order) # ToDo: Check if commented out before commit. Un-comment to auto-cancel every placed order
return order
async def update_order_status(self, order_id: int, new_status: CateringOrderStatus) -> bool:
@@ -58,7 +66,8 @@ class CateringService:
return await self._db_service.get_orders(status=status)
async def cancel_order(self, order: CateringOrder) -> bool:
if self._db_service.change_order_status(order.order_id, CateringOrderStatus.CANCELED):
change_result = await self._db_service.change_order_status(order.order_id, CateringOrderStatus.CANCELED)
if change_result:
await self._accounting_service.add_balance(order.customer.user_id, order.price, f"CATERING REFUND - {order.order_id}")
return True
return False