The marketing surface (PricingPage, Stripe products) was wired for "Starter / Pro / Enterprise" while the backend was on "free / pro / team", leaving plan_billing unseeded and BillingPlan accepting a literal that violated the FK to plan_limits. This change: - Migration 4ce3e594cb87: defensive UPDATE of any subscriptions on plan='team' to 'enterprise' (dev has zero), renames the plan_limits row team -> enterprise, inserts a starter row with caps interpolated between free and pro (max_trees=10, sessions=75, ai=15/mo). - Renames the plan tier across schemas (invite_code, billing, admin, subscription comment), is_paid/has_pro_entitlement checks in the Subscription model, admin/admin_dashboard plan validators, and the frontend useSubscription isPaidPlan check. Resource visibility uses the same string 'team' in a separate domain (Tree/StepLibrary visibility) and is intentionally untouched. - New backend/scripts/sync_stripe_plan_ids.py: idempotent upsert of plan_billing rows from Stripe products by exact name match. Picks the active monthly recurring price for tiers that have one; leaves annual fields NULL by design. Works against test or live keys. - Test fixture updates: conftest seeds the new taxonomy, the public plans helper is a true upsert so tests can override max_users, and team -> enterprise across test_admin_plan_limits and test_invite_plan. Verified: 86/86 passing across the subscription/billing/plan/invite/ admin sweep; sync script run against test mode populates plan_billing correctly for all three tiers. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
83 lines
2.6 KiB
Python
83 lines
2.6 KiB
Python
from typing import Annotated
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, func
|
|
|
|
from app.core.admin_database import get_admin_db
|
|
from app.models.user import User
|
|
from app.models.subscription import Subscription
|
|
from app.models.tree import Tree
|
|
from app.models.audit_log import AuditLog
|
|
from app.schemas.admin import DashboardMetrics, ActivityEntry
|
|
from app.api.deps import require_admin
|
|
|
|
router = APIRouter(prefix="/admin/dashboard", tags=["admin-dashboard"])
|
|
|
|
|
|
@router.get("/metrics", response_model=DashboardMetrics)
|
|
async def get_dashboard_metrics(
|
|
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
|
current_user: Annotated[User, Depends(require_admin)],
|
|
):
|
|
"""Get platform overview metrics."""
|
|
total_users = await db.scalar(select(func.count()).select_from(User)) or 0
|
|
active_subs = await db.scalar(
|
|
select(func.count()).select_from(Subscription).where(
|
|
Subscription.status.in_(["active", "trialing"])
|
|
)
|
|
) or 0
|
|
paid_accounts = await db.scalar(
|
|
select(func.count()).select_from(Subscription).where(
|
|
Subscription.plan.in_(["pro", "starter", "enterprise"])
|
|
)
|
|
) or 0
|
|
total_trees = await db.scalar(
|
|
select(func.count()).select_from(Tree).where(Tree.deleted_at.is_(None))
|
|
) or 0
|
|
|
|
return DashboardMetrics(
|
|
total_users=total_users,
|
|
active_subscriptions=active_subs,
|
|
paid_accounts=paid_accounts,
|
|
total_trees=total_trees,
|
|
)
|
|
|
|
|
|
@router.get("/activity", response_model=list[ActivityEntry])
|
|
async def get_dashboard_activity(
|
|
db: Annotated[AsyncSession, Depends(get_admin_db)],
|
|
current_user: Annotated[User, Depends(require_admin)],
|
|
):
|
|
"""Get recent audit log entries for activity feed."""
|
|
query = (
|
|
select(
|
|
AuditLog.id,
|
|
AuditLog.action,
|
|
AuditLog.resource_type,
|
|
AuditLog.resource_id,
|
|
AuditLog.details,
|
|
AuditLog.ip_address,
|
|
AuditLog.created_at,
|
|
User.email.label("user_email"),
|
|
)
|
|
.outerjoin(User, AuditLog.user_id == User.id)
|
|
.order_by(AuditLog.created_at.desc())
|
|
.limit(10)
|
|
)
|
|
result = await db.execute(query)
|
|
rows = result.all()
|
|
|
|
return [
|
|
ActivityEntry(
|
|
id=row.id,
|
|
user_email=row.user_email,
|
|
action=row.action,
|
|
resource_type=row.resource_type,
|
|
resource_id=row.resource_id,
|
|
details=row.details,
|
|
ip_address=row.ip_address,
|
|
created_at=row.created_at,
|
|
)
|
|
for row in rows
|
|
]
|