Skip to content

Commit

Permalink
Merge pull request #8
Browse files Browse the repository at this point in the history
Check if user already exists in contacts list
  • Loading branch information
usbtypec1 authored Sep 30, 2023
2 parents 5bc5643 + 996bec3 commit e6ac3bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "duck-duck-bot"
version = "1.9.4"
version = "1.9.5"
description = ""
authors = ["Eldos <[email protected]>"]
readme = "README.md"
Expand Down
21 changes: 20 additions & 1 deletion src/handlers/contacts/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from models import User
from repositories import ContactRepository, UserRepository, BalanceRepository
from repositories import HTTPClientFactory
from services import can_create_new_contact, compute_new_contact_price
from services import (
can_create_new_contact,
compute_new_contact_price,
BalanceNotifier,
is_user_already_contact,
)
from states import ContactCreateWaitForForwardedMessage

__all__ = ('register_handlers',)
Expand Down Expand Up @@ -71,6 +76,7 @@ async def on_add_contact(
user_repository: UserRepository,
contact_repository: ContactRepository,
balance_repository: BalanceRepository,
balance_notifier: BalanceNotifier,
) -> None:
reply = message.reply_to_message
if reply.from_user.is_bot:
Expand Down Expand Up @@ -98,6 +104,13 @@ async def on_add_contact(
contacts = await contact_repository.get_by_user_id(message.from_user.id)
contacts_count = len(contacts)

if is_user_already_contact(
user_id=to_user.id,
contacts=contacts,
):
await message.reply('🤭 Этот пользователь уже в ваших контактах')
return

user_balance = await balance_repository.get_user_balance(user.id)

new_contact_price = compute_new_contact_price(contacts_count)
Expand All @@ -112,12 +125,18 @@ async def on_add_contact(
)
return

withdrawal = await balance_repository.create_withdrawal(
user_id=user.id,
amount=new_contact_price,
description='Добавление нового контакта',
)
await contact_repository.create(
of_user_id=user.id,
to_user_id=to_user.id,
private_name=name,
public_name=name,
)
await balance_notifier.send_withdrawal_notification(withdrawal)
await message.reply('✅ Контакт успешно добавлен')


Expand Down
20 changes: 18 additions & 2 deletions src/services/contacts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
__all__ = ('can_create_new_contact', 'compute_new_contact_price')
__all__ = (
'can_create_new_contact',
'compute_new_contact_price',
'is_user_already_contact',
)

from collections.abc import Iterable

from models import Contact


def can_create_new_contact(*, contact_price: int, balance: int) -> bool:
return balance >= contact_price


def compute_new_contact_price(contacts_count: int) -> int:
return 100 * 2 ** contacts_count
return 100 * (2 + contacts_count) // 2 * (contacts_count + 1)


def is_user_already_contact(
*,
user_id: int,
contacts: Iterable[Contact],
) -> bool:
return any(contact.to_user.id == user_id for contact in contacts)

0 comments on commit e6ac3bd

Please sign in to comment.