feat: admin invite codes with plan assignment + user detail page
- Migration 030: add email, assigned_plan, trial_duration_days, email_sent_at
to invite_codes with CHECK constraints
- Resend email integration (graceful degradation when API key not set)
- Invite codes now support plan assignment (free/pro/team) and trial duration (1-90 days)
- Registration applies invite code plan/trial to new subscription
- Auto-downgrade expired trials on authenticated access
- Enriched GET /admin/users/{id} with account, subscription, sessions, audit logs
- New endpoints: PUT /admin/users/{id}/subscription/plan and extend-trial
- Frontend: enhanced invite codes page with email, plan, trial fields
- Frontend: new user detail page at /admin/users/:userId
- Fixed API path drift: /invite-codes -> /invites
- 11 new backend tests, 416 total passing
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,22 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from typing import Literal, Optional
|
||||
from uuid import UUID
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, EmailStr, Field, model_validator
|
||||
|
||||
|
||||
class InviteCodeCreate(BaseModel):
|
||||
"""Schema for creating a new invite code."""
|
||||
expires_at: Optional[datetime] = Field(None, description="Optional expiration time")
|
||||
note: Optional[str] = Field(None, max_length=255, description="Note about who this code is for")
|
||||
email: Optional[EmailStr] = Field(None, description="Recipient email for invite delivery")
|
||||
assigned_plan: Literal["free", "pro", "team"] = Field("free", description="Plan to assign on registration")
|
||||
trial_duration_days: Optional[int] = Field(None, ge=1, le=90, description="Trial duration in days (1-90)")
|
||||
|
||||
@model_validator(mode="after")
|
||||
def free_plan_no_trial(self):
|
||||
if self.assigned_plan == "free" and self.trial_duration_days is not None:
|
||||
raise ValueError("Free plan cannot have a trial duration")
|
||||
return self
|
||||
|
||||
|
||||
class InviteCodeResponse(BaseModel):
|
||||
@@ -23,6 +32,12 @@ class InviteCodeResponse(BaseModel):
|
||||
is_used: bool
|
||||
is_expired: bool
|
||||
is_valid: bool
|
||||
email: Optional[str] = None
|
||||
assigned_plan: str = "free"
|
||||
trial_duration_days: Optional[int] = None
|
||||
email_sent_at: Optional[datetime] = None
|
||||
has_trial: bool = False
|
||||
email_sent: bool = False
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
@@ -38,3 +38,15 @@ class SubscriptionDetails(BaseModel):
|
||||
subscription: SubscriptionResponse
|
||||
limits: PlanLimitsResponse
|
||||
usage: UsageResponse
|
||||
|
||||
|
||||
class SubscriptionPlanUpdate(BaseModel):
|
||||
plan: str # free, pro, team
|
||||
|
||||
model_config = {"json_schema_extra": {"examples": [{"plan": "pro"}]}}
|
||||
|
||||
|
||||
class ExtendTrialRequest(BaseModel):
|
||||
days: int # 1-90
|
||||
|
||||
model_config = {"json_schema_extra": {"examples": [{"days": 14}]}}
|
||||
|
||||
72
backend/app/schemas/user_detail.py
Normal file
72
backend/app/schemas/user_detail.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class AccountSummary(BaseModel):
|
||||
id: UUID
|
||||
name: str
|
||||
display_code: Optional[str] = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class SubscriptionSummary(BaseModel):
|
||||
id: UUID
|
||||
plan: str
|
||||
status: str
|
||||
current_period_start: Optional[datetime] = None
|
||||
current_period_end: Optional[datetime] = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class SessionSummary(BaseModel):
|
||||
id: UUID
|
||||
tree_name: Optional[str] = None
|
||||
started_at: datetime
|
||||
completed_at: Optional[datetime] = None
|
||||
outcome: Optional[str] = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class AuditLogSummary(BaseModel):
|
||||
id: UUID
|
||||
action: str
|
||||
resource_type: Optional[str] = None
|
||||
resource_id: Optional[str] = None
|
||||
created_at: datetime
|
||||
details: Optional[dict] = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class InviteCodeUsedSummary(BaseModel):
|
||||
code: str
|
||||
assigned_plan: str
|
||||
trial_duration_days: Optional[int] = None
|
||||
created_by_email: Optional[str] = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class UserDetailResponse(BaseModel):
|
||||
id: UUID
|
||||
email: str
|
||||
full_name: Optional[str] = None
|
||||
role: str
|
||||
is_active: bool
|
||||
is_super_admin: bool
|
||||
is_team_admin: bool
|
||||
created_at: datetime
|
||||
|
||||
account: Optional[AccountSummary] = None
|
||||
subscription: Optional[SubscriptionSummary] = None
|
||||
invite_code_used: Optional[InviteCodeUsedSummary] = None
|
||||
|
||||
recent_sessions: list[SessionSummary] = []
|
||||
total_sessions: int = 0
|
||||
recent_audit_logs: list[AuditLogSummary] = []
|
||||
total_audit_logs: int = 0
|
||||
Reference in New Issue
Block a user