fix account balance not updating in real time
This commit is contained in:
parent
5eca3d25cf
commit
f691851c9e
@ -61,6 +61,12 @@ class UserInfoBox(Component):
|
||||
self.user_balance = await self.session[AccountingService].get_balance(self.user.user_id)
|
||||
self.user_ticket = await self.session[TicketingService].get_user_ticket(self.user.user_id)
|
||||
self.user_seat = await self.session[SeatingService].get_user_seat(self.user.user_id)
|
||||
self.session[AccountingService].add_update_hook(self.update)
|
||||
|
||||
async def update(self) -> None:
|
||||
self.user_balance = await self.session[AccountingService].get_balance(self.user.user_id)
|
||||
self.user_ticket = await self.session[TicketingService].get_user_ticket(self.user.user_id)
|
||||
self.user_seat = await self.session[SeatingService].get_user_seat(self.user.user_id)
|
||||
|
||||
def build(self) -> Component:
|
||||
if not self.user:
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import logging
|
||||
from collections.abc import Callable
|
||||
from datetime import datetime
|
||||
|
||||
from src.ez_lan_manager.services.DatabaseService import DatabaseService
|
||||
@ -12,6 +13,11 @@ class InsufficientFundsError(Exception):
|
||||
class AccountingService:
|
||||
def __init__(self, db_service: DatabaseService) -> None:
|
||||
self._db_service = db_service
|
||||
self._update_hooks: set[Callable] = set()
|
||||
|
||||
def add_update_hook(self, update_hook: Callable) -> None:
|
||||
""" Adds a function to this service, which is called whenever the account balance changes """
|
||||
self._update_hooks.add(update_hook)
|
||||
|
||||
async def add_balance(self, user_id: int, balance_to_add: int, reference: str) -> int:
|
||||
await self._db_service.add_transaction(Transaction(
|
||||
@ -22,6 +28,8 @@ class AccountingService:
|
||||
transaction_date=datetime.now()
|
||||
))
|
||||
logger.debug(f"Added balance of {self.make_euro_string_from_int(balance_to_add)} to user with ID {user_id}")
|
||||
for update_hook in self._update_hooks:
|
||||
await update_hook()
|
||||
return await self.get_balance(user_id)
|
||||
|
||||
async def remove_balance(self, user_id: int, balance_to_remove: int, reference: str) -> int:
|
||||
@ -36,6 +44,8 @@ class AccountingService:
|
||||
transaction_date=datetime.now()
|
||||
))
|
||||
logger.debug(f"Removed balance of {self.make_euro_string_from_int(balance_to_remove)} to user with ID {user_id}")
|
||||
for update_hook in self._update_hooks:
|
||||
await update_hook()
|
||||
return await self.get_balance(user_id)
|
||||
|
||||
async def get_balance(self, user_id: int) -> int:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user