Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new replenishment #14

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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