fix: allow repeated auto-assignment of promo groups on each purchase
Remove the threshold barrier that prevented re-assignment to the same promo group tier. Previously, _get_best_group_for_spending was called with min_threshold_kopeks=previous_threshold, which meant once a user was auto-assigned to a tier (e.g. 100 kopeks), the check 100 > 100 would fail and the function would skip cleanup of promocode groups. Now the function always finds the best group for the user's spending without threshold filtering. The threshold ratchet is preserved only for the watermark update (auto_promo_group_threshold_kopeks only increases, never decreases). Also elevate promo group assignment failure logging from DEBUG to WARNING across all 3 call sites in transaction.py.
This commit is contained in:
@@ -108,7 +108,7 @@ async def create_transaction(
|
||||
|
||||
await maybe_assign_promo_group_by_total_spent(db, user_id)
|
||||
except Exception as exc:
|
||||
logger.debug('Не удалось проверить автовыдачу промогруппы для пользователя', user_id=user_id, exc=exc)
|
||||
logger.warning('Не удалось проверить автовыдачу промогруппы для пользователя', user_id=user_id, exc=exc)
|
||||
if type == TransactionType.SUBSCRIPTION_PAYMENT and is_completed:
|
||||
try:
|
||||
from app.services.referral_contest_service import referral_contest_service
|
||||
@@ -168,7 +168,7 @@ async def emit_transaction_side_effects(
|
||||
|
||||
await maybe_assign_promo_group_by_total_spent(db, user_id)
|
||||
except Exception as exc:
|
||||
logger.debug('Не удалось проверить автовыдачу промогруппы для пользователя', user_id=user_id, exc=exc)
|
||||
logger.warning('Не удалось проверить автовыдачу промогруппы для пользователя', user_id=user_id, exc=exc)
|
||||
|
||||
if type == TransactionType.SUBSCRIPTION_PAYMENT and is_completed:
|
||||
try:
|
||||
@@ -253,7 +253,7 @@ async def complete_transaction(db: AsyncSession, transaction: Transaction) -> Tr
|
||||
|
||||
await maybe_assign_promo_group_by_total_spent(db, transaction.user_id)
|
||||
except Exception as exc:
|
||||
logger.debug(
|
||||
logger.warning(
|
||||
'Не удалось проверить автовыдачу промогруппы для пользователя', user_id=transaction.user_id, exc=exc
|
||||
)
|
||||
|
||||
|
||||
@@ -114,28 +114,16 @@ async def maybe_assign_promo_group_by_total_spent(
|
||||
|
||||
previous_threshold = user.auto_promo_group_threshold_kopeks or 0
|
||||
|
||||
target_group = await _get_best_group_for_spending(
|
||||
db,
|
||||
total_spent,
|
||||
min_threshold_kopeks=previous_threshold,
|
||||
)
|
||||
# Находим группу, соответствующую текущим тратам (без порогового фильтра,
|
||||
# чтобы промокод-группы всегда очищались при покупке)
|
||||
target_group = await _get_best_group_for_spending(db, total_spent)
|
||||
if not target_group:
|
||||
return None
|
||||
|
||||
try:
|
||||
target_threshold = target_group.auto_assign_total_spent_kopeks or 0
|
||||
|
||||
if target_threshold <= previous_threshold:
|
||||
logger.debug(
|
||||
"Порог промогруппы '' не превышает ранее назначенный для пользователя",
|
||||
target_group_name=target_group.name,
|
||||
target_threshold=target_threshold,
|
||||
previous_threshold=previous_threshold,
|
||||
telegram_id=user.telegram_id,
|
||||
)
|
||||
return None
|
||||
|
||||
# Удаляем старые auto/promocode группы перед назначением новой
|
||||
# Фаза 1: Удаляем старые auto/promocode группы, отличные от целевой
|
||||
current_groups = await get_user_promo_groups(db, user_id)
|
||||
removed_any = False
|
||||
for upg in current_groups:
|
||||
@@ -170,7 +158,8 @@ async def maybe_assign_promo_group_by_total_spent(
|
||||
return target_group
|
||||
|
||||
user.auto_promo_group_assigned = True
|
||||
user.auto_promo_group_threshold_kopeks = target_threshold
|
||||
if target_threshold > previous_threshold:
|
||||
user.auto_promo_group_threshold_kopeks = target_threshold
|
||||
user.updated_at = datetime.now(UTC)
|
||||
|
||||
newly_added = False
|
||||
|
||||
Reference in New Issue
Block a user