From 8a267e93b9c4bbe67b5642b20c1436a1f35c91eb Mon Sep 17 00:00:00 2001 From: Vyacheslav Kapitonov Date: Sun, 8 Oct 2023 02:05:18 +0300 Subject: [PATCH] Fix new replenishment Issue #7 --- bot/handlers/_responses.py | 1 + bot/handlers/balance.py | 65 ++++++++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/bot/handlers/_responses.py b/bot/handlers/_responses.py index 549a66f..b3c894d 100644 --- a/bot/handlers/_responses.py +++ b/bot/handlers/_responses.py @@ -17,4 +17,5 @@ 'custom_date': 'Введи дату в формате YYYY-MM-DD для получения отчета', 'current_balance': 'Твой баланс: {balance} {currency}', 'new_replanishment': 'Новое пополнение на {value} {currency}', + 'new_expence': 'Новый расход на сумму {value} {currency}', } diff --git a/bot/handlers/balance.py b/bot/handlers/balance.py index 7862f59..ab56405 100644 --- a/bot/handlers/balance.py +++ b/bot/handlers/balance.py @@ -36,28 +36,59 @@ def _prepare_balance_history(replenishments: Sequence[Expense]) -> List[str]: return chunkineze(report_lines, chunk_size=50) +async def _just_balance( + message: Message, + session: Session, +) -> None: + user = user_service.get_user_by_id(message.chat.id, session) + balance = balance_service.get_balance(user.id, session) + text = 'Изменить баланс на ЧИСЛО - /balance ЧИСЛО\n\n' + + text += RESPONSES['current_balance'].format( + balance=balance, + currency=user.currency, + ) + return await message.answer(text) + + +async def _balance_history( + message: Message, + session: Session, +) -> None: + + replenishments = balance_service.get_balance_history( + user_id=message.chat.id, + session=session, + ) + for chunk in _prepare_balance_history(replenishments): + await message.answer(chunk) + + +def _replanishment_msg( + value: int, + currency: str, +) -> str: + if value >= 0: + text = RESPONSES['new_replanishment'].format( + value=value, + currency=currency, + ) + else: + text = RESPONSES['new_expence'].format( + value=value, + currency=currency, + ) + return text + + @router.message(Command('b')) @router.message(Command('balance')) async def balance(message: Message, session: Session, command: CommandObject): if not command.args: - user = user_service.get_user_by_id(message.chat.id, session) - balance = balance_service.get_balance(user.id, session) - text = 'Изменить баланс на ЧИСЛО - /balance ЧИСЛО\n\n' - - text += RESPONSES['current_balance'].format( - balance=balance, - currency=user.currency, - ) - return await message.answer(text) + await _just_balance(message=message, session=session) if command.args.strip() == 'history': - replenishments = balance_service.get_balance_history( - user_id=message.chat.id, - session=session, - ) - for chunk in _prepare_balance_history(replenishments): - await message.answer(chunk) - return + return await _balance_history(message=message, session=session) value, _, comment = command.args.partition('\n') try: @@ -79,7 +110,7 @@ async def balance(message: Message, session: Session, command: CommandObject): unit=user.currency, session=session, ) - text = RESPONSES['new_replanishment'].format( + text = _replanishment_msg( value=value, currency=user.currency, )