diff --git a/.bot.pid b/.bot.pid index 36e4afd..c93c25b 100644 --- a/.bot.pid +++ b/.bot.pid @@ -1 +1 @@ -851623 +859161 diff --git a/src/core/services.py b/src/core/services.py index cb59336..b627055 100644 --- a/src/core/services.py +++ b/src/core/services.py @@ -583,19 +583,47 @@ class ParticipationService: results["errors"].append(f"Пользователь с счётом {formatted_account}{card_info} не найден") continue - # Пробуем добавить в розыгрыш - if await ParticipationService.add_participant(session, lottery_id, user.id): - results["added"] += 1 - detail = f"{user.first_name} ({formatted_account})" - if card_number: - detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})" - results["details"].append(detail) - else: + # Получаем запись Account для этого счета + account_record = await session.execute( + select(Account).where(Account.account_number == formatted_account) + ) + account_record = account_record.scalar_one_or_none() + + if not account_record: + card_info = f" (карта: {card_number})" if card_number else "" + results["errors"].append(f"Запись счета {formatted_account}{card_info} не найдена в базе") + continue + + # Проверяем, не участвует ли уже этот счет + existing = await session.execute( + select(Participation).where( + Participation.lottery_id == lottery_id, + Participation.account_number == formatted_account + ) + ) + if existing.scalar_one_or_none(): results["skipped"] += 1 detail = f"{user.first_name} ({formatted_account})" if card_number: detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})" results["details"].append(f"Уже участвует: {detail}") + continue + + # Добавляем участие по счету + participation = Participation( + lottery_id=lottery_id, + user_id=user.id, + account_id=account_record.id, + account_number=formatted_account + ) + session.add(participation) + await session.commit() + + results["added"] += 1 + detail = f"{user.first_name} ({formatted_account})" + if card_number: + detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})" + results["details"].append(detail) except Exception as e: results["errors"].append(f"Ошибка с {account_input}: {str(e)}") @@ -650,8 +678,19 @@ class ParticipationService: results["details"].append(f"Не найден: {formatted_account}{card_info}") continue - # Пробуем удалить из розыгрыша - if await ParticipationService.remove_participant(session, lottery_id, user.id): + # Ищем участие по номеру счета (не по user_id!) + participation = await session.execute( + select(Participation).where( + Participation.lottery_id == lottery_id, + Participation.account_number == formatted_account + ) + ) + participation = participation.scalar_one_or_none() + + if participation: + await session.delete(participation) + await session.commit() + results["removed"] += 1 detail = f"{user.first_name} ({formatted_account})" if card_number: