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:
|
||||
|
||||
Reference in New Issue
Block a user