Improve Tribute auto-renew
This commit is contained in:
@@ -96,6 +96,19 @@ async def update_payment_status_by_db_id(
|
||||
return payment
|
||||
|
||||
|
||||
async def user_has_successful_payment_for_provider(
|
||||
session: AsyncSession, user_id: int, provider: str) -> bool:
|
||||
"""Check if a user has at least one successful payment for the provider."""
|
||||
|
||||
stmt = (select(Payment.payment_id)
|
||||
.where(Payment.user_id == user_id,
|
||||
Payment.provider == provider,
|
||||
Payment.status == 'succeeded')
|
||||
.limit(1))
|
||||
result = await session.execute(stmt)
|
||||
return result.scalar_one_or_none() is not None
|
||||
|
||||
|
||||
async def update_payment_status_by_yk_id(session: AsyncSession,
|
||||
yookassa_payment_id: str,
|
||||
new_status: str) -> Optional[Payment]:
|
||||
|
||||
@@ -231,3 +231,25 @@ async def set_skip_notifications_for_provider(
|
||||
Subscription.provider == provider).values(skip_notifications=skip))
|
||||
result = await session.execute(stmt)
|
||||
return result.rowcount
|
||||
|
||||
|
||||
async def get_active_subscriptions_for_autorenew(
|
||||
session: AsyncSession, provider: str,
|
||||
days_threshold: int = 1,
|
||||
require_skip_flag: bool = True) -> List[Subscription]:
|
||||
"""Fetch active subscriptions nearing expiration for auto-renew logic."""
|
||||
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
threshold_date = now_utc + timedelta(days=days_threshold)
|
||||
|
||||
conditions = [
|
||||
Subscription.provider == provider,
|
||||
Subscription.is_active == True,
|
||||
Subscription.end_date <= threshold_date,
|
||||
]
|
||||
if require_skip_flag:
|
||||
conditions.append(Subscription.skip_notifications == True)
|
||||
|
||||
stmt = select(Subscription).where(*conditions)
|
||||
result = await session.execute(stmt)
|
||||
return result.scalars().all()
|
||||
|
||||
Reference in New Issue
Block a user