import logging from typing import TYPE_CHECKING from tortoise import timezone from src import domain if TYPE_CHECKING: from .. import Usecase log = logging.getLogger(__name__) async def handle_unsubscription(self: 'Usecase', user_telegram_id: int, channel_telegram_id: int) -> None: subscriber = await self.database.get_telegram_user(telegram_id=user_telegram_id) if not subscriber: log.warning('Subscriber not found for telegram_id: %s', user_telegram_id) return project = await self.database.get_project_by_channel_telegram(channel_telegram_id) if not project: log.warning('Project not found for channel telegram_id: %s', channel_telegram_id) return subscriptions = await self.database.get_active_subscriptions_by_subscriber_and_project(subscriber.id, project.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 for subscription in subscriptions: subscription.status = domain.SubscriptionStatus.UNSUBSCRIBED subscription.unsubscribed_at = timezone.now() await self.database.update_subscription(subscription) log.info( 'Subscription marked as unsubscribed: subscriber %s (telegram_id: %s) unsubscribed from publication %s', subscriber.id, user_telegram_id, subscription.publication_id, )