feat(autorenewal): implement automatic subscription renewal system
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -7,6 +7,24 @@ from db.models.subscriptions import Subscription, SubscriptionStatus
|
||||
|
||||
|
||||
class SubscriptionRepository:
|
||||
async def get_subscriptions_for_autorenewal(
|
||||
self, session: AsyncSession, *, days_before_expiry: int = 1
|
||||
) -> list[Subscription]:
|
||||
from datetime import timedelta
|
||||
|
||||
threshold = datetime.now(timezone.utc) + timedelta(days=days_before_expiry)
|
||||
stmt = (
|
||||
select(Subscription)
|
||||
.where(
|
||||
Subscription.autorenew == True,
|
||||
Subscription.status == SubscriptionStatus.ACTIVE,
|
||||
Subscription.end_date <= threshold,
|
||||
)
|
||||
.with_for_update()
|
||||
)
|
||||
result = await session.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
async def get_subscription_by_user_id(
|
||||
self, session: AsyncSession, user_id: int
|
||||
) -> Subscription | None:
|
||||
|
||||
@@ -112,7 +112,7 @@ class UserRepository:
|
||||
amount=amount,
|
||||
tx_type=tx_type,
|
||||
balance_before=user.balance,
|
||||
balance_after=user.balance + amount,
|
||||
balance_after=user.balance - amount,
|
||||
description=description,
|
||||
created_at=created_at,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user