147 lines
4.6 KiB
Python
147 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
from uuid import UUID
|
|
|
|
from sqlalchemy import func, or_, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.db.enums import EntitlementStatus, SubscriptionStatus
|
|
from app.db.models import (
|
|
AccessPlan,
|
|
Module,
|
|
UserEntitlement,
|
|
UserSubscription,
|
|
)
|
|
|
|
from .domain import (
|
|
ActiveEntitlementRecord,
|
|
ActiveSubscriptionRecord,
|
|
)
|
|
|
|
|
|
class AccessRepository:
|
|
"""
|
|
Commerce/content read repository for effective user access.
|
|
|
|
No auth.app_users query is required: authenticated user_id comes from
|
|
the validated JWT subject.
|
|
"""
|
|
|
|
def __init__(self, session: AsyncSession) -> None:
|
|
self._session = session
|
|
|
|
async def get_active_subscriptions(
|
|
self,
|
|
user_id: UUID,
|
|
) -> list[ActiveSubscriptionRecord]:
|
|
result = await self._session.execute(
|
|
select(
|
|
UserSubscription.id,
|
|
AccessPlan.code,
|
|
UserSubscription.status,
|
|
UserSubscription.started_at,
|
|
UserSubscription.expires_at,
|
|
)
|
|
.join(
|
|
AccessPlan,
|
|
AccessPlan.id == UserSubscription.plan_id,
|
|
)
|
|
.where(
|
|
UserSubscription.user_id == user_id,
|
|
UserSubscription.status == SubscriptionStatus.ACTIVE,
|
|
or_(
|
|
UserSubscription.expires_at.is_(None),
|
|
UserSubscription.expires_at > func.now(),
|
|
),
|
|
)
|
|
.order_by(
|
|
UserSubscription.started_at.asc(),
|
|
UserSubscription.id.asc(),
|
|
)
|
|
)
|
|
|
|
records: list[ActiveSubscriptionRecord] = []
|
|
for row in result.all():
|
|
status_value = getattr(row.status, "value", row.status)
|
|
records.append(
|
|
ActiveSubscriptionRecord(
|
|
id=row.id,
|
|
plan_code=row.code,
|
|
status=str(status_value),
|
|
started_at=row.started_at,
|
|
expires_at=row.expires_at,
|
|
)
|
|
)
|
|
return records
|
|
|
|
async def get_active_entitlements(
|
|
self,
|
|
user_id: UUID,
|
|
) -> list[ActiveEntitlementRecord]:
|
|
result = await self._session.execute(
|
|
select(
|
|
UserEntitlement.id,
|
|
UserEntitlement.module_id,
|
|
Module.code,
|
|
Module.slug,
|
|
Module.name,
|
|
UserEntitlement.status,
|
|
UserEntitlement.granted_at,
|
|
UserEntitlement.expires_at,
|
|
UserEntitlement.source_subscription_id,
|
|
UserEntitlement.source_plan_id,
|
|
)
|
|
.join(
|
|
Module,
|
|
Module.id == UserEntitlement.module_id,
|
|
)
|
|
.where(
|
|
UserEntitlement.user_id == user_id,
|
|
UserEntitlement.status == EntitlementStatus.ACTIVE,
|
|
or_(
|
|
UserEntitlement.expires_at.is_(None),
|
|
UserEntitlement.expires_at > func.now(),
|
|
),
|
|
)
|
|
.order_by(
|
|
Module.name.asc(),
|
|
Module.id.asc(),
|
|
UserEntitlement.granted_at.asc(),
|
|
UserEntitlement.id.asc(),
|
|
)
|
|
)
|
|
|
|
records: list[ActiveEntitlementRecord] = []
|
|
for row in result.all():
|
|
status_value = getattr(row.status, "value", row.status)
|
|
records.append(
|
|
ActiveEntitlementRecord(
|
|
id=row.id,
|
|
module_id=row.module_id,
|
|
module_code=row.code,
|
|
module_slug=row.slug,
|
|
module_name=row.name,
|
|
status=str(status_value),
|
|
granted_at=row.granted_at,
|
|
expires_at=row.expires_at,
|
|
source_subscription_id=row.source_subscription_id,
|
|
source_plan_id=row.source_plan_id,
|
|
)
|
|
)
|
|
return records
|
|
|
|
async def get_all_active_modules(
|
|
self,
|
|
) -> list[tuple[str | None, str]]:
|
|
# Preserves the previous lifetime-access semantics: is_active is the
|
|
# canonical gate; no new module-status rule is introduced here.
|
|
result = await self._session.execute(
|
|
select(Module.code, Module.slug)
|
|
.where(Module.is_active.is_(True))
|
|
.order_by(Module.name.asc(), Module.id.asc())
|
|
)
|
|
return [
|
|
(row.code, row.slug)
|
|
for row in result.all()
|
|
]
|