ref: рефакторинг usecases
This commit is contained in:
@@ -17,44 +17,43 @@ async def handle_subscription(
|
||||
first_name: str | None = None,
|
||||
last_name: str | None = None,
|
||||
) -> None:
|
||||
async with self.database.transaction():
|
||||
placement = await self.database.get_placement_by_invite_link(invite_link)
|
||||
if not placement:
|
||||
log.warning('Placement not found for invite_link: %s', invite_link)
|
||||
return
|
||||
placement = await self.database.get_placement_by_invite_link(invite_link)
|
||||
if not placement:
|
||||
log.warning('Placement not found for invite_link: %s', invite_link)
|
||||
return
|
||||
|
||||
subscriber = domain.Subscriber(
|
||||
telegram_id=user_telegram_id,
|
||||
username=username,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
subscriber = domain.Subscriber(
|
||||
telegram_id=user_telegram_id,
|
||||
username=username,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
)
|
||||
await self.database.upsert_subscriber(subscriber)
|
||||
|
||||
active_subscription = await self.database.get_active_subscription_by_subscriber_and_channel(
|
||||
subscriber.id, placement.target_channel.telegram_id
|
||||
)
|
||||
|
||||
if active_subscription:
|
||||
# Пользователь уже подписан на канал через другой placement
|
||||
# Это не должно случиться (Telegram не даст подписаться дважды),
|
||||
# но если случилось - логируем и игнорируем
|
||||
log.warning(
|
||||
'User %s (telegram_id: %s) already has active subscription to channel %s via placement %s, '
|
||||
'ignoring new subscription attempt via placement %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.target_channel_id,
|
||||
active_subscription.placement_id,
|
||||
placement.id,
|
||||
)
|
||||
subscriber = await self.database.upsert_subscriber(subscriber)
|
||||
return
|
||||
|
||||
active_subscription = await self.database.get_active_subscription_by_subscriber_and_channel(
|
||||
subscriber.id, placement.target_channel.telegram_id
|
||||
)
|
||||
# Проверяем, была ли раньше подписка через ЭТОТ placement (для реактивации)
|
||||
existing_sub = await self.database.get_subscription_by_subscriber_and_placement(subscriber.id, placement.id)
|
||||
|
||||
if active_subscription:
|
||||
# Пользователь уже подписан на канал через другой placement
|
||||
# Это не должно случиться (Telegram не даст подписаться дважды),
|
||||
# но если случилось - логируем и игнорируем
|
||||
log.warning(
|
||||
'User %s (telegram_id: %s) already has active subscription to channel %s via placement %s, '
|
||||
'ignoring new subscription attempt via placement %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.target_channel_id,
|
||||
active_subscription.placement_id,
|
||||
placement.id,
|
||||
)
|
||||
return
|
||||
|
||||
# Проверяем, была ли раньше подписка через ЭТОТ placement (для реактивации)
|
||||
existing_sub = await self.database.get_subscription_by_subscriber_and_placement(subscriber.id, placement.id)
|
||||
|
||||
if existing_sub and existing_sub.status == domain.SubscriptionStatus.UNSUBSCRIBED:
|
||||
# Реактивируем старую подписку через тот же placement
|
||||
if existing_sub and existing_sub.status == domain.SubscriptionStatus.UNSUBSCRIBED:
|
||||
async with self.database.transaction():
|
||||
existing_sub.status = domain.SubscriptionStatus.ACTIVE
|
||||
existing_sub.unsubscribed_at = None
|
||||
await self.database.update_subscription(existing_sub)
|
||||
@@ -62,29 +61,30 @@ async def handle_subscription(
|
||||
placement.subscriptions_count += 1
|
||||
await self.database.update_placement(placement)
|
||||
|
||||
log.info(
|
||||
'Subscription reactivated: subscriber %s (telegram_id: %s) resubscribed via same placement %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.id,
|
||||
)
|
||||
return
|
||||
|
||||
subscription = domain.Subscription(
|
||||
placement_id=placement.id,
|
||||
subscriber_id=subscriber.id,
|
||||
target_channel_id=placement.target_channel_id,
|
||||
invite_link=invite_link,
|
||||
log.info(
|
||||
'Subscription reactivated: subscriber %s (telegram_id: %s) resubscribed via same placement %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.id,
|
||||
)
|
||||
return
|
||||
|
||||
subscription = domain.Subscription(
|
||||
placement_id=placement.id,
|
||||
subscriber_id=subscriber.id,
|
||||
target_channel_id=placement.target_channel_id,
|
||||
invite_link=invite_link,
|
||||
)
|
||||
async with self.database.transaction():
|
||||
await self.database.create_subscription(subscription)
|
||||
|
||||
placement.subscriptions_count += 1
|
||||
await self.database.update_placement(placement)
|
||||
|
||||
log.info(
|
||||
'Subscription created: subscriber %s (telegram_id: %s) subscribed via placement %s (invite_link: %s)',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.id,
|
||||
invite_link,
|
||||
)
|
||||
log.info(
|
||||
'Subscription created: subscriber %s (telegram_id: %s) subscribed via placement %s (invite_link: %s)',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.id,
|
||||
invite_link,
|
||||
)
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import datetime
|
||||
import logging
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from tortoise import timezone
|
||||
|
||||
from src import domain
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -11,39 +12,38 @@ log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def handle_unsubscription(self: 'Usecase', user_telegram_id: int, channel_telegram_id: int) -> None:
|
||||
async with self.database.transaction():
|
||||
subscriber = await self.database.get_subscriber(user_telegram_id)
|
||||
if not subscriber:
|
||||
log.warning('Subscriber not found for telegram_id: %s', user_telegram_id)
|
||||
return
|
||||
subscriber = await self.database.get_subscriber(user_telegram_id)
|
||||
if not subscriber:
|
||||
log.warning('Subscriber not found for telegram_id: %s', user_telegram_id)
|
||||
return
|
||||
|
||||
subscriptions = await self.database.get_active_subscriptions_by_subscriber_and_channel(
|
||||
subscriber.id, channel_telegram_id
|
||||
subscriptions = await self.database.get_active_subscriptions_by_subscriber_and_channel(
|
||||
subscriber.id, channel_telegram_id
|
||||
)
|
||||
|
||||
if not subscriptions:
|
||||
log.info(
|
||||
'No active subscriptions found for subscriber %s (telegram_id: %s) in channel %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
channel_telegram_id,
|
||||
)
|
||||
return
|
||||
|
||||
if not subscriptions:
|
||||
log.info(
|
||||
'No active subscriptions found for subscriber %s (telegram_id: %s) in channel %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
channel_telegram_id,
|
||||
)
|
||||
return
|
||||
|
||||
for subscription in subscriptions:
|
||||
for subscription in subscriptions:
|
||||
async with self.database.transaction():
|
||||
subscription.status = domain.SubscriptionStatus.UNSUBSCRIBED
|
||||
subscription.unsubscribed_at = datetime.datetime.now(datetime.UTC)
|
||||
subscription.unsubscribed_at = timezone.now()
|
||||
await self.database.update_subscription(subscription)
|
||||
|
||||
# Декрементируем счётчик подписок у закупа
|
||||
placement = subscription.placement
|
||||
if placement.subscriptions_count > 0:
|
||||
placement.subscriptions_count -= 1
|
||||
await self.database.update_placement(placement)
|
||||
|
||||
log.info(
|
||||
'Subscription marked as unsubscribed: subscriber %s (telegram_id: %s) unsubscribed from placement %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.id,
|
||||
)
|
||||
log.info(
|
||||
'Subscription marked as unsubscribed: subscriber %s (telegram_id: %s) unsubscribed from placement %s',
|
||||
subscriber.id,
|
||||
user_telegram_id,
|
||||
placement.id,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user