Skip to content

Commit

Permalink
Reverted Add cost button (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
parfeniukink authored Dec 26, 2022
1 parent 17eaee7 commit 1ca691a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/apps/costs/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def get_sum(costs: list[Cost]) -> Decimal:


class CostsGeneralMenu(str, Enum):
DELETE_COST = "Delete costs 💵"
ADD_COST = "Add cost 💵"
DELETE_COST = "Delete cost 💵"


class ExtraCallbackData(Enum):
Expand Down
38 changes: 24 additions & 14 deletions src/apps/costs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

from telebot import types

from apps.categories import categories_keyboard
from apps.categories.domain import Category
from apps.categories.services import CategoriesCRUD
from apps.configurations.domain import Configuration
from apps.configurations.services import ConfigurationsCRUD
from apps.categories import CategoriesCRUD, Category, categories_keyboard
from apps.configurations import Configuration, ConfigurationsCRUD
from apps.costs.db.schemas import CostCreateSchema
from apps.costs.domain import (
Cost,
Expand All @@ -17,9 +14,8 @@
)
from apps.costs.keyboards import cost_sources_keyboard, ids_keyboard
from apps.costs.services import CostsCRUD, CostsService
from apps.dates import dates_keyboard, get_exist_dates_keyboard
from apps.dates.domain import DatesFormats
from apps.finances import PRICE_REGEX, Currency
from apps.dates import DatesFormats, dates_keyboard, get_exist_dates_keyboard
from apps.finances import Currency
from apps.shared.domain import ConfirmationOptions, base_error_handler
from apps.shared.keyboards import confirmation_keyboard, default_keyboard
from apps.shared.validators import money_validator
Expand Down Expand Up @@ -163,30 +159,44 @@ async def description_entered_callback(m: types.Message):
storage.trash_messages.add(m.id)


@bot.message_handler(regexp=PRICE_REGEX)
@base_error_handler
@UsersService.only_for_members
async def add_costs(m: types.Message):
async def value_entered_callback(m: types.Message):
if not m.text:
raise CostsError("Value could not be empty")

user: User = await UsersCRUD().get_by_account_id(m.from_user.id)
configuration: Configuration = await ConfigurationsCRUD().get(user.id)
storage: CostsStorage = CostsStorage(user)

storage.value = money_validator(m.text)
State(user.id).set(callback=description_entered_callback)

sent_message = await Messages.send(
text="Enter the description and press Enter",
chat_id=m.chat.id,
reply_markup=cost_sources_keyboard(configuration),
)

storage.trash_messages.add(sent_message.id)
storage.trash_messages.add(m.id)


@bot.message_handler(regexp=rf"^{CostsGeneralMenu.ADD_COST}")
@base_error_handler
@UsersService.only_for_members
async def add_costs(m: types.Message):
user: User = await UsersCRUD().get_by_account_id(m.from_user.id)
storage: CostsStorage = CostsStorage(user)

State(user.id).set(callback=value_entered_callback)

sent_message = await Messages.send(
text="Enter the value and press Enter",
chat_id=m.chat.id,
reply_markup=types.ReplyKeyboardRemove(),
)

storage.trash_messages.add(m.id)
storage.trash_messages.add(sent_message.id)


# #############################
# Delete costs block
# #############################
Expand Down
19 changes: 19 additions & 0 deletions src/apps/costs/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
description_entered_callback,
id_to_delete_selected_callback_query,
month_selected_callback_query,
value_entered_callback,
)
from apps.costs.services import CostsCRUD
from apps.dates import DatesFormats
Expand All @@ -38,6 +39,24 @@ async def test_add_costs(mocker, telegram_message, prepopulated_users):
assert len(storage.trash_messages) == 2


async def test_value_entered_callback(mocker, telegram_message, prepopulated_users):
send_message = mocker.patch("bot.Messages.send")
telegram_message.text = "200.12"
storage = CostsStorage(prepopulated_users["john"])

await value_entered_callback.__wrapped__(telegram_message)

send_message.assert_called_once()
assert len(storage.trash_messages) == 2


async def test_value_entered_callback_FAIL(telegram_message):
telegram_message.text = ""

with pytest.raises(CostsError):
await value_entered_callback.__wrapped__(telegram_message)


async def test_description_entered_callback(
mocker, telegram_message, prepopulated_users
):
Expand Down

0 comments on commit 1ca691a

Please sign in to comment.