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:
@@ -5,6 +5,8 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from app.core.database import get_db
|
||||
from app.core.rate_limit import limiter
|
||||
from app.core.audit import log_audit
|
||||
from app.core.email import EmailService
|
||||
from app.models.user import User
|
||||
from app.models.invite_code import InviteCode
|
||||
from app.schemas.invite_code import InviteCodeCreate, InviteCodeResponse, InviteCodeValidation
|
||||
@@ -23,9 +25,35 @@ async def create_invite_code(
|
||||
invite_code = InviteCode(
|
||||
created_by_id=current_user.id,
|
||||
expires_at=invite_data.expires_at,
|
||||
note=invite_data.note
|
||||
note=invite_data.note,
|
||||
email=invite_data.email,
|
||||
assigned_plan=invite_data.assigned_plan,
|
||||
trial_duration_days=invite_data.trial_duration_days,
|
||||
)
|
||||
db.add(invite_code)
|
||||
await db.flush()
|
||||
|
||||
# Send invite email if email provided
|
||||
email_sent = False
|
||||
if invite_data.email:
|
||||
email_sent = await EmailService.send_invite_email(
|
||||
to_email=invite_data.email,
|
||||
code=invite_code.code,
|
||||
plan=invite_data.assigned_plan,
|
||||
trial_days=invite_data.trial_duration_days,
|
||||
)
|
||||
if email_sent:
|
||||
invite_code.email_sent_at = datetime.now(timezone.utc)
|
||||
|
||||
await log_audit(
|
||||
db, current_user.id, "invite.create", "invite_code", invite_code.id,
|
||||
{
|
||||
"code": invite_code.code,
|
||||
"plan": invite_data.assigned_plan,
|
||||
"email": invite_data.email,
|
||||
"email_sent": email_sent,
|
||||
},
|
||||
)
|
||||
await db.commit()
|
||||
await db.refresh(invite_code)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user