174 lines
5.8 KiB
Python
174 lines
5.8 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,
|
|
Invoice,
|
|
Module,
|
|
PaymentTransaction,
|
|
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,
|
|
UserSubscription.payment_reference,
|
|
)
|
|
.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(),
|
|
)
|
|
)
|
|
|
|
# Lookup payment transaction & invoice for this user's subscriptions
|
|
tx_rows = (await self._session.execute(
|
|
select(
|
|
PaymentTransaction.subscription_id,
|
|
PaymentTransaction.gateway_transaction_id,
|
|
Invoice.invoice_number,
|
|
UserSubscription.payment_reference,
|
|
)
|
|
.join(UserSubscription, UserSubscription.id == PaymentTransaction.subscription_id)
|
|
.outerjoin(Invoice, Invoice.payment_transaction_id == PaymentTransaction.id)
|
|
.where(PaymentTransaction.user_id == user_id)
|
|
)).all()
|
|
|
|
sub_tx_map: dict[UUID, tuple[str, str | None]] = {}
|
|
order_tx_map: dict[str, tuple[str, str | None]] = {}
|
|
for sub_id, gw_tx_id, inv_num, order_ref in tx_rows:
|
|
if sub_id:
|
|
sub_tx_map[sub_id] = (gw_tx_id, inv_num)
|
|
if order_ref:
|
|
order_tx_map[order_ref] = (gw_tx_id, inv_num)
|
|
|
|
records: list[ActiveSubscriptionRecord] = []
|
|
for row in result.all():
|
|
status_value = getattr(row.status, "value", row.status)
|
|
gw_id, inv_num = sub_tx_map.get(row.id) or (order_tx_map.get(row.payment_reference) if row.payment_reference else (None, None))
|
|
records.append(
|
|
ActiveSubscriptionRecord(
|
|
id=row.id,
|
|
plan_code=row.code,
|
|
status=str(status_value),
|
|
started_at=row.started_at,
|
|
expires_at=row.expires_at,
|
|
payment_id=gw_id,
|
|
invoice_number=inv_num,
|
|
)
|
|
)
|
|
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()
|
|
]
|