220 lines
9.9 KiB
TypeScript
220 lines
9.9 KiB
TypeScript
// apps/admin/src/app/(admin)/subscriptions/[id]/page.tsx
|
|
// D3RO Voice — Subscription Detail & Tier Overrides (Midnight Glass v2)
|
|
|
|
import { Box, Typography } from '@mui/material'
|
|
import { C, FONT_SANS, FONT_MONO, panelSx, tableSx, statusBadgeSx } from '@/lib/console-theme'
|
|
import { DoubleBezelCard, TactileBadge } from '@d3ro/ui/components/ds'
|
|
import { getSupabaseAdminClient, isSupabaseAdminConfigured } from '@/lib/supabase-admin'
|
|
import { UnavailableAdminPanel } from '@/components/unavailable-admin-panel'
|
|
import { requireManager, hasMinRole } from '@/lib/admin-guard'
|
|
import { ADMIN_UUID_PATTERN } from '@/lib/request-security'
|
|
import { DetailRow } from '@/components/detail-row'
|
|
import Link from 'next/link'
|
|
import { notFound } from 'next/navigation'
|
|
import { SubscriptionDetailClient } from './client'
|
|
|
|
interface PageProps {
|
|
params: Promise<{ id: string }>
|
|
}
|
|
|
|
export default async function SubscriptionDetailPage({ params }: PageProps): Promise<React.ReactElement> {
|
|
const admin = await requireManager()
|
|
if (!isSupabaseAdminConfigured()) {
|
|
return (
|
|
<UnavailableAdminPanel
|
|
title="Subscription Detail"
|
|
capability="Plans, billing, payment history"
|
|
reason="SUPABASE_URL / SUPABASE_SERVICE_ROLE_KEY 환경변수가 설정되지 않아 Supabase 관리 데이터에 연결할 수 없습니다. 환경변수를 설정하면 실데이터가 표시됩니다."
|
|
/>
|
|
)
|
|
}
|
|
const { id: userId } = await params
|
|
if (!ADMIN_UUID_PATTERN.test(userId)) notFound()
|
|
const supabase = await getSupabaseAdminClient()
|
|
|
|
const [subRes, profileRes, auditRes] = await Promise.all([
|
|
supabase.from('subscriptions').select('id, user_id, tier, status, provider, payment_provider, current_period_start, current_period_end, overage_credits, admin_note, cancel_at, created_at, updated_at').eq('user_id', userId).maybeSingle(),
|
|
supabase.from('profiles').select('id, name, tier, role').eq('id', userId).maybeSingle(),
|
|
supabase.from('audit_log').select('id, action, target_type, target_id, memo, created_at')
|
|
.eq('target_id', userId)
|
|
.eq('target_type', 'subscription')
|
|
.order('created_at', { ascending: false })
|
|
.limit(20),
|
|
])
|
|
|
|
if (subRes.error) throw new Error(`Supabase subscription failed: ${subRes.error.message}`)
|
|
if (profileRes.error) throw new Error(`Supabase profile failed: ${profileRes.error.message}`)
|
|
if (auditRes.error) throw new Error(`Supabase audit log failed: ${auditRes.error.message}`)
|
|
if (!profileRes.data) notFound()
|
|
|
|
const profile = profileRes.data
|
|
const sub = subRes.data
|
|
const auditLogs = auditRes.data ?? []
|
|
|
|
if (sub && (!['free', 'pro', 'pro_plus'].includes(sub.tier) ||
|
|
!['active', 'canceled', 'past_due', 'expired'].includes(sub.status) ||
|
|
!Number.isInteger(sub.overage_credits))) {
|
|
throw new Error('Supabase subscription response is invalid')
|
|
}
|
|
|
|
const tier = (sub?.tier as string | undefined) || (profile.tier as string) || 'free'
|
|
const isProPlus = tier === 'pro_plus'
|
|
const isPro = tier === 'pro'
|
|
|
|
return (
|
|
<>
|
|
{/* Header */}
|
|
<Box
|
|
sx={{
|
|
...panelSx,
|
|
minHeight: 84,
|
|
px: { xs: 2.5, md: 4 },
|
|
py: 2,
|
|
display: 'flex',
|
|
flexWrap: 'wrap',
|
|
alignItems: 'center',
|
|
justifyContent: 'space-between',
|
|
gap: 2,
|
|
}}
|
|
>
|
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 2 }}>
|
|
<Box
|
|
sx={{
|
|
width: 4,
|
|
height: 32,
|
|
borderRadius: '999px',
|
|
background: 'linear-gradient(180deg, var(--d3-tag-purple) 0%, var(--d3-accent-main) 100%)',
|
|
}}
|
|
/>
|
|
<Box>
|
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5 }}>
|
|
<Typography
|
|
component="h1"
|
|
sx={{
|
|
fontFamily: FONT_SANS,
|
|
fontSize: '20px',
|
|
fontWeight: 500,
|
|
color: C.bright,
|
|
letterSpacing: '-0.02em',
|
|
m: 0,
|
|
}}
|
|
>
|
|
Subscription Contract Console
|
|
</Typography>
|
|
<Box component="span" sx={statusBadgeSx(isProPlus ? 'purple' : isPro ? 'green' : 'blue')}>
|
|
{isProPlus ? 'PRO+ VIP' : tier.toUpperCase()}
|
|
</Box>
|
|
</Box>
|
|
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1.5, mt: 0.25 }}>
|
|
<Link href="/subscriptions" style={{ color: C.accentLight, textDecoration: 'none', fontSize: '12px', fontFamily: FONT_SANS, fontWeight: 600 }}>
|
|
← Back to Subscriptions
|
|
</Link>
|
|
<Typography sx={{ fontFamily: FONT_MONO, fontSize: '11px', color: C.dim }}>
|
|
Subscriber: {(profile.name as string) ?? userId}
|
|
</Typography>
|
|
</Box>
|
|
</Box>
|
|
</Box>
|
|
</Box>
|
|
|
|
{/* 2-Column Details Grid */}
|
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
|
|
<Box sx={{ display: 'grid', gridTemplateColumns: { xs: '1fr', lg: '1fr 1fr' }, gap: 3 }}>
|
|
{/* User Info Card */}
|
|
<DoubleBezelCard bezelPadding="6px" innerPadding="24px">
|
|
<Typography sx={{ fontFamily: FONT_SANS, fontSize: '16px', fontWeight: 500, color: C.bright, mb: 2 }}>
|
|
Account Identifiers
|
|
</Typography>
|
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}>
|
|
<DetailRow label="Customer Name" value={(profile.name as string) ?? '-'} />
|
|
<DetailRow label="Account UID" value={userId} isMono />
|
|
<DetailRow label="Assigned Role" value={((profile.role as string) ?? 'user').toUpperCase()} isMono />
|
|
</Box>
|
|
</DoubleBezelCard>
|
|
|
|
{/* Subscription State Card */}
|
|
<DoubleBezelCard bezelPadding="6px" innerPadding="24px">
|
|
<Typography sx={{ fontFamily: FONT_SANS, fontSize: '16px', fontWeight: 500, color: C.bright, mb: 2 }}>
|
|
Contract Status & Pricing
|
|
</Typography>
|
|
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5, fontFamily: FONT_SANS, fontSize: '13px' }}>
|
|
<DetailRow label="Plan Level" value={isProPlus ? 'PRO+ VIP' : isPro ? 'PRO' : 'FREE'} />
|
|
<DetailRow label="Current Status" value={sub?.status ? String(sub.status).toUpperCase() : 'NO SUBSCRIPTION'} />
|
|
<DetailRow label="Payment Gateway" value={sub?.payment_provider ? String(sub.payment_provider).toUpperCase() : 'NOT ASSIGNED'} />
|
|
<DetailRow label="Contract Expires / Renews" value={sub?.current_period_end ? new Date(sub.current_period_end as string).toLocaleDateString() : 'NOT SET'} />
|
|
<DetailRow label="Overage Credits" value={sub ? String(sub.overage_credits) : 'NO SUBSCRIPTION'} isMono />
|
|
</Box>
|
|
</DoubleBezelCard>
|
|
</Box>
|
|
|
|
{/* Client Component for CRUD & Modifications */}
|
|
<SubscriptionDetailClient
|
|
userId={userId}
|
|
hasSub={!!sub}
|
|
canEdit={true}
|
|
canCreateDelete={hasMinRole(admin, 'admin')}
|
|
initialSub={sub ? ({
|
|
tier: sub.tier as 'free' | 'pro' | 'pro_plus',
|
|
status: sub.status as 'active' | 'canceled' | 'past_due' | 'expired',
|
|
currentPeriodEnd: (sub.current_period_end as string | null) ?? null,
|
|
overageCredits: sub.overage_credits as number,
|
|
adminNote: (sub.admin_note as string | null) ?? null,
|
|
}) : undefined}
|
|
/>
|
|
|
|
{/* Audit Trail Table */}
|
|
<DoubleBezelCard bezelPadding="6px" innerPadding="24px">
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
|
|
<Typography sx={{ fontFamily: FONT_SANS, fontSize: '15px', fontWeight: 500, color: C.bright }}>
|
|
Subscription Security Audit Trail
|
|
</Typography>
|
|
<TactileBadge tone="mono" mono>
|
|
LOGGED ACTIONS
|
|
</TactileBadge>
|
|
</Box>
|
|
|
|
<Box sx={{ overflowX: 'auto' }}>
|
|
<Box component="table" sx={tableSx}>
|
|
<Box component="thead">
|
|
<Box component="tr">
|
|
<Box component="th">DATE</Box>
|
|
<Box component="th">ACTION</Box>
|
|
<Box component="th">MEMO / RATIONALE</Box>
|
|
<Box component="th" sx={{ textAlign: 'right' }}>DETAIL</Box>
|
|
</Box>
|
|
</Box>
|
|
<Box component="tbody">
|
|
{auditLogs.length === 0 ? (
|
|
<Box component="tr">
|
|
<Box component="td" colSpan={4} sx={{ textAlign: 'center', color: C.dim, py: 3 }}>
|
|
No subscription audit events have been recorded.
|
|
</Box>
|
|
</Box>
|
|
) : auditLogs.map((log) => (
|
|
<Box component="tr" key={log.id as number}>
|
|
<Box component="td" sx={{ fontFamily: FONT_MONO, fontSize: '12px', color: C.dim }}>
|
|
{new Date(log.created_at as string).toLocaleString()}
|
|
</Box>
|
|
<Box component="td">
|
|
<Box component="span" sx={statusBadgeSx('purple')}>
|
|
{log.action as string}
|
|
</Box>
|
|
</Box>
|
|
<Box component="td" sx={{ color: C.bright }}>
|
|
{log.memo as string}
|
|
</Box>
|
|
<Box component="td" sx={{ textAlign: 'right' }}>
|
|
<Link href={`/audit-log/${log.id as number}`} style={{ color: C.accentLight, textDecoration: 'none', fontSize: '12px', fontFamily: FONT_SANS, fontWeight: 600 }}>
|
|
View Diff →
|
|
</Link>
|
|
</Box>
|
|
</Box>
|
|
))}
|
|
</Box>
|
|
</Box>
|
|
</Box>
|
|
</DoubleBezelCard>
|
|
</Box>
|
|
</>
|
|
)
|
|
}
|