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:
422
frontend/src/pages/admin/UserDetailPage.tsx
Normal file
422
frontend/src/pages/admin/UserDetailPage.tsx
Normal file
@@ -0,0 +1,422 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useParams, useNavigate } from 'react-router-dom'
|
||||
import { ArrowLeft, Shield, Crown, UserCheck, UserX, Clock, Ticket } from 'lucide-react'
|
||||
import { StatusBadge } from '@/components/admin'
|
||||
import { Modal } from '@/components/common/Modal'
|
||||
import { adminApi } from '@/api/admin'
|
||||
import { toast } from '@/lib/toast'
|
||||
import { cn } from '@/lib/utils'
|
||||
import type { UserDetailResponse } from '@/types/admin'
|
||||
|
||||
const PLAN_OPTIONS = ['free', 'pro', 'team'] as const
|
||||
|
||||
export function UserDetailPage() {
|
||||
const { userId } = useParams<{ userId: string }>()
|
||||
const navigate = useNavigate()
|
||||
const [user, setUser] = useState<UserDetailResponse | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
// Modal state
|
||||
const [planModalOpen, setPlanModalOpen] = useState(false)
|
||||
const [selectedPlan, setSelectedPlan] = useState('')
|
||||
const [trialModalOpen, setTrialModalOpen] = useState(false)
|
||||
const [trialDays, setTrialDays] = useState('14')
|
||||
const [activeTab, setActiveTab] = useState<'sessions' | 'audit'>('sessions')
|
||||
|
||||
const fetchUser = useCallback(async () => {
|
||||
if (!userId) return
|
||||
setLoading(true)
|
||||
try {
|
||||
const data = await adminApi.getUserDetail(userId)
|
||||
setUser(data)
|
||||
} catch {
|
||||
toast.error('Failed to load user details')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [userId])
|
||||
|
||||
useEffect(() => { fetchUser() }, [fetchUser])
|
||||
|
||||
const handleChangePlan = async () => {
|
||||
if (!userId || !selectedPlan) return
|
||||
try {
|
||||
await adminApi.updateUserSubscriptionPlan(userId, selectedPlan)
|
||||
toast.success(`Plan changed to ${selectedPlan}`)
|
||||
setPlanModalOpen(false)
|
||||
fetchUser()
|
||||
} catch {
|
||||
toast.error('Failed to change plan')
|
||||
}
|
||||
}
|
||||
|
||||
const handleExtendTrial = async () => {
|
||||
if (!userId || !trialDays) return
|
||||
try {
|
||||
await adminApi.extendUserTrial(userId, parseInt(trialDays))
|
||||
toast.success(`Trial extended by ${trialDays} days`)
|
||||
setTrialModalOpen(false)
|
||||
fetchUser()
|
||||
} catch {
|
||||
toast.error('Failed to extend trial')
|
||||
}
|
||||
}
|
||||
|
||||
const handleToggleActive = async () => {
|
||||
if (!userId || !user) return
|
||||
try {
|
||||
if (user.is_active) {
|
||||
await adminApi.deactivateUser(userId)
|
||||
toast.success('User deactivated')
|
||||
} else {
|
||||
await adminApi.activateUser(userId)
|
||||
toast.success('User activated')
|
||||
}
|
||||
fetchUser()
|
||||
} catch {
|
||||
toast.error('Failed to update user status')
|
||||
}
|
||||
}
|
||||
|
||||
const inputClass = cn(
|
||||
'w-full rounded-md border border-white/10 bg-black/50 px-3 py-2 text-sm text-white',
|
||||
'placeholder:text-white/40 focus:outline-none focus:border-white/30 focus:ring-2 focus:ring-white/20'
|
||||
)
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex items-center justify-center py-20">
|
||||
<div className="h-8 w-8 animate-spin rounded-full border-2 border-white/20 border-t-white" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return (
|
||||
<div className="py-20 text-center text-white/40">User not found</div>
|
||||
)
|
||||
}
|
||||
|
||||
const fmt = (d: string | null) => d ? new Date(d).toLocaleDateString() : '—'
|
||||
const fmtFull = (d: string | null) => d ? new Date(d).toLocaleString() : '—'
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<button
|
||||
onClick={() => navigate('/admin/users')}
|
||||
className="rounded-md border border-white/10 p-2 text-white/60 hover:bg-white/10 hover:text-white"
|
||||
>
|
||||
<ArrowLeft className="h-4 w-4" />
|
||||
</button>
|
||||
<div className="flex-1">
|
||||
<h1 className="text-xl font-semibold text-white">
|
||||
{user.full_name || user.email}
|
||||
</h1>
|
||||
<p className="text-sm text-white/40">{user.email}</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{user.is_super_admin && (
|
||||
<StatusBadge variant="warning">
|
||||
<Crown className="mr-1 h-3 w-3" /> Super Admin
|
||||
</StatusBadge>
|
||||
)}
|
||||
<StatusBadge variant={user.is_active ? 'success' : 'destructive'}>
|
||||
{user.is_active ? 'Active' : 'Inactive'}
|
||||
</StatusBadge>
|
||||
<StatusBadge variant="default">{user.role}</StatusBadge>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Account & Subscription */}
|
||||
<div className="grid grid-cols-1 gap-6 md:grid-cols-2">
|
||||
<div className="glass-card rounded-2xl p-6">
|
||||
<h2 className="mb-4 text-sm font-semibold uppercase tracking-wider text-white/40">
|
||||
Account & Subscription
|
||||
</h2>
|
||||
<dl className="space-y-3">
|
||||
{user.account && (
|
||||
<>
|
||||
<div className="flex justify-between">
|
||||
<dt className="text-sm text-white/60">Account</dt>
|
||||
<dd className="text-sm text-white">{user.account.name}</dd>
|
||||
</div>
|
||||
{user.account.display_code && (
|
||||
<div className="flex justify-between">
|
||||
<dt className="text-sm text-white/60">Display Code</dt>
|
||||
<dd className="text-sm font-mono text-white/70">{user.account.display_code}</dd>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{user.subscription ? (
|
||||
<>
|
||||
<div className="flex justify-between">
|
||||
<dt className="text-sm text-white/60">Plan</dt>
|
||||
<dd className="text-sm font-semibold text-white">
|
||||
{user.subscription.plan.charAt(0).toUpperCase() + user.subscription.plan.slice(1)}
|
||||
</dd>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<dt className="text-sm text-white/60">Status</dt>
|
||||
<dd>
|
||||
<StatusBadge variant={user.subscription.status === 'trialing' ? 'warning' : 'success'}>
|
||||
{user.subscription.status}
|
||||
</StatusBadge>
|
||||
</dd>
|
||||
</div>
|
||||
{user.subscription.current_period_end && (
|
||||
<div className="flex justify-between">
|
||||
<dt className="text-sm text-white/60">Period End</dt>
|
||||
<dd className="text-sm text-white/70">{fmt(user.subscription.current_period_end)}</dd>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div className="text-sm text-white/40">No subscription</div>
|
||||
)}
|
||||
<div className="flex justify-between">
|
||||
<dt className="text-sm text-white/60">Joined</dt>
|
||||
<dd className="text-sm text-white/70">{fmt(user.created_at)}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
{/* Admin Actions */}
|
||||
<div className="glass-card rounded-2xl p-6">
|
||||
<h2 className="mb-4 text-sm font-semibold uppercase tracking-wider text-white/40">
|
||||
Admin Actions
|
||||
</h2>
|
||||
<div className="space-y-3">
|
||||
<button
|
||||
onClick={() => {
|
||||
setSelectedPlan(user.subscription?.plan || 'free')
|
||||
setPlanModalOpen(true)
|
||||
}}
|
||||
className="flex w-full items-center gap-3 rounded-lg border border-white/10 px-4 py-3 text-left text-sm text-white/70 hover:bg-white/5 hover:text-white"
|
||||
>
|
||||
<Shield className="h-4 w-4 text-white/40" />
|
||||
Change Plan
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setTrialModalOpen(true)}
|
||||
className="flex w-full items-center gap-3 rounded-lg border border-white/10 px-4 py-3 text-left text-sm text-white/70 hover:bg-white/5 hover:text-white"
|
||||
>
|
||||
<Clock className="h-4 w-4 text-white/40" />
|
||||
{user.subscription?.status === 'trialing' ? 'Extend Trial' : 'Start Trial'}
|
||||
</button>
|
||||
<button
|
||||
onClick={handleToggleActive}
|
||||
className={cn(
|
||||
'flex w-full items-center gap-3 rounded-lg border px-4 py-3 text-left text-sm',
|
||||
user.is_active
|
||||
? 'border-red-500/20 text-red-400 hover:bg-red-500/5'
|
||||
: 'border-emerald-500/20 text-emerald-400 hover:bg-emerald-500/5'
|
||||
)}
|
||||
>
|
||||
{user.is_active ? (
|
||||
<><UserX className="h-4 w-4" /> Deactivate User</>
|
||||
) : (
|
||||
<><UserCheck className="h-4 w-4" /> Activate User</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Invite Code Used */}
|
||||
{user.invite_code_used && (
|
||||
<div className="glass-card rounded-2xl p-6">
|
||||
<h2 className="mb-4 text-sm font-semibold uppercase tracking-wider text-white/40">
|
||||
<Ticket className="mr-2 inline h-4 w-4" />
|
||||
Invite Code Used
|
||||
</h2>
|
||||
<dl className="grid grid-cols-2 gap-4 md:grid-cols-4">
|
||||
<div>
|
||||
<dt className="text-xs text-white/40">Code</dt>
|
||||
<dd className="mt-1 font-mono text-sm text-white/70">{user.invite_code_used.code}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-xs text-white/40">Plan Assigned</dt>
|
||||
<dd className="mt-1 text-sm text-white/70">
|
||||
{user.invite_code_used.assigned_plan.charAt(0).toUpperCase() + user.invite_code_used.assigned_plan.slice(1)}
|
||||
</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-xs text-white/40">Trial Days</dt>
|
||||
<dd className="mt-1 text-sm text-white/70">{user.invite_code_used.trial_duration_days ?? '—'}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt className="text-xs text-white/40">Created By</dt>
|
||||
<dd className="mt-1 text-sm text-white/70">{user.invite_code_used.created_by_email ?? '—'}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Tabs: Sessions / Audit Logs */}
|
||||
<div className="glass-card rounded-2xl">
|
||||
<div className="flex border-b border-white/[0.06]">
|
||||
<button
|
||||
onClick={() => setActiveTab('sessions')}
|
||||
className={cn(
|
||||
'px-6 py-3 text-sm font-medium',
|
||||
activeTab === 'sessions' ? 'border-b-2 border-white text-white' : 'text-white/40 hover:text-white/60'
|
||||
)}
|
||||
>
|
||||
Sessions ({user.total_sessions})
|
||||
</button>
|
||||
<button
|
||||
onClick={() => setActiveTab('audit')}
|
||||
className={cn(
|
||||
'px-6 py-3 text-sm font-medium',
|
||||
activeTab === 'audit' ? 'border-b-2 border-white text-white' : 'text-white/40 hover:text-white/60'
|
||||
)}
|
||||
>
|
||||
Audit Logs ({user.total_audit_logs})
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="p-6">
|
||||
{activeTab === 'sessions' && (
|
||||
user.recent_sessions.length > 0 ? (
|
||||
<table className="w-full">
|
||||
<thead>
|
||||
<tr className="border-b border-white/[0.06] text-left text-xs text-white/40">
|
||||
<th className="pb-2 font-medium">Tree</th>
|
||||
<th className="pb-2 font-medium">Started</th>
|
||||
<th className="pb-2 font-medium">Completed</th>
|
||||
<th className="pb-2 font-medium">Outcome</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{user.recent_sessions.map(s => (
|
||||
<tr key={s.id} className="border-b border-white/[0.03]">
|
||||
<td className="py-3 text-sm text-white/70">{s.tree_name ?? '—'}</td>
|
||||
<td className="py-3 text-sm text-white/40">{fmtFull(s.started_at)}</td>
|
||||
<td className="py-3 text-sm text-white/40">{fmtFull(s.completed_at)}</td>
|
||||
<td className="py-3">
|
||||
{s.outcome ? (
|
||||
<StatusBadge variant={s.outcome === 'resolved' ? 'success' : 'default'}>
|
||||
{s.outcome}
|
||||
</StatusBadge>
|
||||
) : (
|
||||
<span className="text-sm text-white/30">—</span>
|
||||
)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
) : (
|
||||
<div className="py-8 text-center text-sm text-white/40">No sessions yet</div>
|
||||
)
|
||||
)}
|
||||
|
||||
{activeTab === 'audit' && (
|
||||
user.recent_audit_logs.length > 0 ? (
|
||||
<table className="w-full">
|
||||
<thead>
|
||||
<tr className="border-b border-white/[0.06] text-left text-xs text-white/40">
|
||||
<th className="pb-2 font-medium">Action</th>
|
||||
<th className="pb-2 font-medium">Resource</th>
|
||||
<th className="pb-2 font-medium">Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{user.recent_audit_logs.map(a => (
|
||||
<tr key={a.id} className="border-b border-white/[0.03]">
|
||||
<td className="py-3 text-sm text-white/70">{a.action}</td>
|
||||
<td className="py-3 text-sm text-white/40">{a.resource_type ?? '—'}</td>
|
||||
<td className="py-3 text-sm text-white/40">{fmtFull(a.created_at)}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
) : (
|
||||
<div className="py-8 text-center text-sm text-white/40">No audit logs yet</div>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Change Plan Modal */}
|
||||
<Modal
|
||||
isOpen={planModalOpen}
|
||||
onClose={() => setPlanModalOpen(false)}
|
||||
title="Change Subscription Plan"
|
||||
size="sm"
|
||||
footer={
|
||||
<div className="flex justify-end gap-3">
|
||||
<button
|
||||
onClick={() => setPlanModalOpen(false)}
|
||||
className="rounded-md border border-white/10 px-4 py-2 text-sm font-medium text-white/60 hover:bg-white/10"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={handleChangePlan}
|
||||
className="rounded-md bg-white px-4 py-2 text-sm font-medium text-black hover:bg-white/90"
|
||||
>
|
||||
Update Plan
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-white">Plan</label>
|
||||
<select
|
||||
aria-label="Subscription plan"
|
||||
value={selectedPlan}
|
||||
onChange={(e) => setSelectedPlan(e.target.value)}
|
||||
className={inputClass}
|
||||
>
|
||||
{PLAN_OPTIONS.map(p => (
|
||||
<option key={p} value={p}>{p.charAt(0).toUpperCase() + p.slice(1)}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* Extend Trial Modal */}
|
||||
<Modal
|
||||
isOpen={trialModalOpen}
|
||||
onClose={() => setTrialModalOpen(false)}
|
||||
title={user.subscription?.status === 'trialing' ? 'Extend Trial' : 'Start Trial'}
|
||||
size="sm"
|
||||
footer={
|
||||
<div className="flex justify-end gap-3">
|
||||
<button
|
||||
onClick={() => setTrialModalOpen(false)}
|
||||
className="rounded-md border border-white/10 px-4 py-2 text-sm font-medium text-white/60 hover:bg-white/10"
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={handleExtendTrial}
|
||||
className="rounded-md bg-white px-4 py-2 text-sm font-medium text-black hover:bg-white/90"
|
||||
>
|
||||
{user.subscription?.status === 'trialing' ? 'Extend' : 'Start Trial'}
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<div>
|
||||
<label className="mb-1 block text-sm font-medium text-white">Days to add</label>
|
||||
<input
|
||||
type="number"
|
||||
value={trialDays}
|
||||
onChange={(e) => setTrialDays(e.target.value)}
|
||||
min={1}
|
||||
max={90}
|
||||
className={inputClass}
|
||||
/>
|
||||
<p className="mt-1 text-xs text-white/40">1-90 days. Will convert to trialing status if not already.</p>
|
||||
</div>
|
||||
</Modal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default UserDetailPage
|
||||
Reference in New Issue
Block a user