fix: сохранение участников по НОМЕРУ СЧЕТА, а не по user_id
Критическое изменение логики: БЫЛО: - Участие сохранялось только по user_id - Один пользователь мог участвовать только 1 раз - Номер счета игнорировался СТАЛО: - Участие сохраняется по НОМЕРУ СЧЕТА (account_number) - Заполняются поля: user_id, account_id, account_number - Один пользователь может участвовать НЕСКОЛЬКИМИ счетами - Проверка дубликатов по account_number - Удаление также работает по account_number Теперь: 1. Админ отправляет: '2521 11-22-33-44-55-66-77' 2. Парсится: карта=2521, счет=11-22-33-44-55-66-77 3. Находится владелец карты 2521 4. Добавляется участие ПО СЧЕТУ 11-22-33-44-55-66-77 5. Владелец может участвовать другими счетами
This commit is contained in:
@@ -583,19 +583,47 @@ class ParticipationService:
|
|||||||
results["errors"].append(f"Пользователь с счётом {formatted_account}{card_info} не найден")
|
results["errors"].append(f"Пользователь с счётом {formatted_account}{card_info} не найден")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Пробуем добавить в розыгрыш
|
# Получаем запись Account для этого счета
|
||||||
if await ParticipationService.add_participant(session, lottery_id, user.id):
|
account_record = await session.execute(
|
||||||
results["added"] += 1
|
select(Account).where(Account.account_number == formatted_account)
|
||||||
detail = f"{user.first_name} ({formatted_account})"
|
)
|
||||||
if card_number:
|
account_record = account_record.scalar_one_or_none()
|
||||||
detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})"
|
|
||||||
results["details"].append(detail)
|
if not account_record:
|
||||||
else:
|
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
|
results["skipped"] += 1
|
||||||
detail = f"{user.first_name} ({formatted_account})"
|
detail = f"{user.first_name} ({formatted_account})"
|
||||||
if card_number:
|
if card_number:
|
||||||
detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})"
|
detail = f"{user.first_name} (карта: {card_number}, счёт: {formatted_account})"
|
||||||
results["details"].append(f"Уже участвует: {detail}")
|
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:
|
except Exception as e:
|
||||||
results["errors"].append(f"Ошибка с {account_input}: {str(e)}")
|
results["errors"].append(f"Ошибка с {account_input}: {str(e)}")
|
||||||
@@ -650,8 +678,19 @@ class ParticipationService:
|
|||||||
results["details"].append(f"Не найден: {formatted_account}{card_info}")
|
results["details"].append(f"Не найден: {formatted_account}{card_info}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Пробуем удалить из розыгрыша
|
# Ищем участие по номеру счета (не по user_id!)
|
||||||
if await ParticipationService.remove_participant(session, lottery_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
|
results["removed"] += 1
|
||||||
detail = f"{user.first_name} ({formatted_account})"
|
detail = f"{user.first_name} ({formatted_account})"
|
||||||
if card_number:
|
if card_number:
|
||||||
|
|||||||
Reference in New Issue
Block a user