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:
Michael Chihlas
2026-02-11 21:42:58 -05:00
parent a466400c5b
commit 50cb0fc7f0
24 changed files with 2522 additions and 1121 deletions

View File

@@ -1,33 +1,45 @@
import { useState, useEffect, useCallback } from 'react'
import { Plus, Copy, Trash2, Ticket } from 'lucide-react'
import { Plus, Copy, Trash2, Ticket, Mail, MailCheck } from 'lucide-react'
import { DataTable, PageHeader, StatusBadge, ActionMenu, EmptyState } from '@/components/admin'
import type { Column } 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 { InviteCodeResponse, InviteCodeCreateRequest } from '@/types/admin'
interface InviteCode {
id: string
code: string
created_by_id: string
used_by_id: string | null
is_active: boolean
expires_at: string | null
created_at: string
const PLAN_OPTIONS = [
{ value: 'free', label: 'Free' },
{ value: 'pro', label: 'Pro' },
{ value: 'team', label: 'Team' },
] as const
const planBadgeVariant = (plan: string): 'success' | 'destructive' | 'warning' | 'default' => {
switch (plan) {
case 'pro': return 'success'
case 'team': return 'warning'
default: return 'default'
}
}
export function InviteCodesPage() {
const [codes, setCodes] = useState<InviteCode[]>([])
const [codes, setCodes] = useState<InviteCodeResponse[]>([])
const [loading, setLoading] = useState(true)
const [createOpen, setCreateOpen] = useState(false)
const [creating, setCreating] = useState(false)
// Form state
const [email, setEmail] = useState('')
const [expiresInDays, setExpiresInDays] = useState('')
const [assignedPlan, setAssignedPlan] = useState<'free' | 'pro' | 'team'>('free')
const [trialDays, setTrialDays] = useState('')
const [note, setNote] = useState('')
const fetchCodes = useCallback(async () => {
setLoading(true)
try {
const data = await adminApi.listInviteCodes()
setCodes(Array.isArray(data) ? data : data.items || [])
setCodes(Array.isArray(data) ? data : [])
} catch {
toast.error('Failed to load invite codes')
} finally {
@@ -37,18 +49,36 @@ export function InviteCodesPage() {
useEffect(() => { fetchCodes() }, [fetchCodes])
const resetForm = () => {
setEmail('')
setExpiresInDays('')
setAssignedPlan('free')
setTrialDays('')
setNote('')
}
const handleCreate = async () => {
setCreating(true)
try {
const expiresAt = expiresInDays
? new Date(Date.now() + parseInt(expiresInDays) * 86400000).toISOString()
: undefined
await adminApi.createInviteCode(expiresAt ? { expires_at: expiresAt } : undefined)
const data: InviteCodeCreateRequest = {}
if (expiresInDays) {
data.expires_at = new Date(Date.now() + parseInt(expiresInDays) * 86400000).toISOString()
}
if (email.trim()) data.email = email.trim()
if (note.trim()) data.note = note.trim()
data.assigned_plan = assignedPlan
if (assignedPlan !== 'free' && trialDays) {
data.trial_duration_days = parseInt(trialDays)
}
await adminApi.createInviteCode(data)
toast.success('Invite code created')
setCreateOpen(false)
setExpiresInDays('')
resetForm()
fetchCodes()
} catch {
toast.error('Failed to create invite code')
} finally {
setCreating(false)
}
}
@@ -57,9 +87,9 @@ export function InviteCodesPage() {
toast.success('Code copied to clipboard')
}
const handleDelete = async (id: string) => {
const handleDelete = async (code: string) => {
try {
await adminApi.deleteInviteCode(id)
await adminApi.deleteInviteCode(code)
toast.success('Invite code deleted')
fetchCodes()
} catch {
@@ -67,7 +97,12 @@ export function InviteCodesPage() {
}
}
const columns: Column<InviteCode>[] = [
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'
)
const columns: Column<InviteCodeResponse>[] = [
{
key: 'code',
header: 'Code',
@@ -75,14 +110,48 @@ export function InviteCodesPage() {
<code className="rounded bg-white/10 px-2 py-1 text-sm font-mono text-white/70">{c.code}</code>
),
},
{
key: 'email',
header: 'Recipient',
render: (c) => c.email ? (
<div className="flex items-center gap-1.5">
{c.email_sent ? (
<MailCheck className="h-3.5 w-3.5 text-emerald-400" />
) : (
<Mail className="h-3.5 w-3.5 text-white/30" />
)}
<span className="text-sm text-white/60">{c.email}</span>
</div>
) : (
<span className="text-sm text-white/30">&mdash;</span>
),
},
{
key: 'plan',
header: 'Plan',
render: (c) => (
<StatusBadge variant={planBadgeVariant(c.assigned_plan)}>
{c.assigned_plan.charAt(0).toUpperCase() + c.assigned_plan.slice(1)}
</StatusBadge>
),
},
{
key: 'trial',
header: 'Trial',
render: (c) => c.has_trial ? (
<span className="text-sm text-white/60">{c.trial_duration_days}d</span>
) : (
<span className="text-sm text-white/30">&mdash;</span>
),
},
{
key: 'status',
header: 'Status',
render: (c) => {
if (c.used_by_id) return <StatusBadge variant="default">Used</StatusBadge>
if (!c.is_active) return <StatusBadge variant="destructive">Inactive</StatusBadge>
if (c.expires_at && new Date(c.expires_at) < new Date()) return <StatusBadge variant="warning">Expired</StatusBadge>
return <StatusBadge variant="success">Active</StatusBadge>
if (c.is_used) return <StatusBadge variant="default">Used</StatusBadge>
if (c.is_expired) return <StatusBadge variant="warning">Expired</StatusBadge>
if (c.is_valid) return <StatusBadge variant="success">Active</StatusBadge>
return <StatusBadge variant="destructive">Inactive</StatusBadge>
},
},
{
@@ -114,12 +183,12 @@ export function InviteCodesPage() {
icon: <Copy className="h-4 w-4" />,
onClick: () => handleCopy(c.code),
},
{
...(!c.is_used ? [{
label: 'Delete',
icon: <Trash2 className="h-4 w-4" />,
onClick: () => handleDelete(c.id),
onClick: () => handleDelete(c.code),
destructive: true,
},
}] : []),
]} />
),
},
@@ -129,7 +198,7 @@ export function InviteCodesPage() {
<div className="space-y-6">
<PageHeader
title="Invite Codes"
description="Manage registration invite codes"
description="Create and manage registration invite codes with plan assignment"
action={
<button
onClick={() => setCreateOpen(true)}
@@ -160,27 +229,73 @@ export function InviteCodesPage() {
<Modal
isOpen={createOpen}
onClose={() => setCreateOpen(false)}
onClose={() => { setCreateOpen(false); resetForm() }}
title="Create Invite Code"
size="sm"
footer={
<div className="flex justify-end gap-3">
<button
onClick={() => setCreateOpen(false)}
onClick={() => { setCreateOpen(false); resetForm() }}
className="rounded-md border border-white/10 px-4 py-2 text-sm font-medium text-white/60 hover:bg-white/10 hover:text-white"
>
Cancel
</button>
<button
onClick={handleCreate}
className="rounded-md bg-white px-4 py-2 text-sm font-medium text-black hover:bg-white/90"
disabled={creating}
className="rounded-md bg-white px-4 py-2 text-sm font-medium text-black hover:bg-white/90 disabled:opacity-50"
>
Create
{creating ? 'Creating...' : 'Create'}
</button>
</div>
}
>
<div className="space-y-4">
<div>
<label className="mb-1 block text-sm font-medium text-white">Recipient Email</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Optional — will send invite email"
className={inputClass}
/>
</div>
<div>
<label className="mb-1 block text-sm font-medium text-white">Plan</label>
<select
aria-label="Plan"
value={assignedPlan}
onChange={(e) => {
const plan = e.target.value as 'free' | 'pro' | 'team'
setAssignedPlan(plan)
if (plan === 'free') setTrialDays('')
}}
className={inputClass}
>
{PLAN_OPTIONS.map(o => (
<option key={o.value} value={o.value}>{o.label}</option>
))}
</select>
</div>
{assignedPlan !== 'free' && (
<div>
<label className="mb-1 block text-sm font-medium text-white">Trial Duration (days)</label>
<input
type="number"
value={trialDays}
onChange={(e) => setTrialDays(e.target.value)}
placeholder="e.g. 14 (1-90)"
min={1}
max={90}
className={inputClass}
/>
<p className="mt-1 text-xs text-white/40">Leave empty for no trial account gets full plan immediately.</p>
</div>
)}
<div>
<label className="mb-1 block text-sm font-medium text-white">Expires in (days)</label>
<input
@@ -188,10 +303,18 @@ export function InviteCodesPage() {
value={expiresInDays}
onChange={(e) => setExpiresInDays(e.target.value)}
placeholder="Leave empty for no expiry"
className={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'
)}
className={inputClass}
/>
</div>
<div>
<label className="mb-1 block text-sm font-medium text-white">Note</label>
<input
type="text"
value={note}
onChange={(e) => setNote(e.target.value)}
placeholder="Optional note (e.g. who this is for)"
className={inputClass}
/>
</div>
</div>