Skip to content

Commit

Permalink
Fix new replenishment
Browse files Browse the repository at this point in the history
Issue #7
  • Loading branch information
HeySlava committed Oct 7, 2023
1 parent 6e6c6f4 commit 8a267e9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions bot/handlers/_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
'custom_date': 'Введи дату в формате YYYY-MM-DD для получения отчета',
'current_balance': 'Твой баланс: {balance} {currency}',
'new_replanishment': 'Новое пополнение на {value} {currency}',
'new_expence': 'Новый расход на сумму {value} {currency}',
}
65 changes: 48 additions & 17 deletions bot/handlers/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
)
Expand Down

0 comments on commit 8a267e9

Please sign in to comment.