feat(admin): Phase V2-6 Admin CRM 고도화 — CRUD + 차트 + 결제 + 감사

- DB: audit_log 테이블(diff 포함) + subscriptions.admin_note + super_admin role
- Edge Functions 4개: admin-users, admin-subscriptions, admin-payments, admin-audit-log
- 공유 유틸: admin-auth.ts(권한 검증), audit.ts(감사로그 기록)
- Swagger UI: 독립 정적 페이지 + OpenAPI 3.0 spec
- CRUD 페이지: 구독 생성/수정/삭제, role 변경, 감사로그 목록/상세
- recharts: feature별 StackedBar + DAU Line + Top Users HorizontalBar
- 결제 이력: DB + Payple API 병행 조회
- 권한: super_admin만 위험 작업, admin은 조회 전용
- RLS: admin/super_admin IN 정책 + super_admin 쓰기 정책
- SQL RPC: admin_usage_by_feature, admin_top_users, admin_dau
This commit is contained in:
윤찬 2026-04-12 21:32:47 +09:00
parent f7c50eb2ed
commit dca1b90faa
34 changed files with 3142 additions and 45 deletions

View file

@ -0,0 +1,128 @@
'use client'
// apps/admin/src/components/audit-diff-viewer.tsx
// before/after JSON diff 뷰어
import { Box } from '@mui/material'
import { PhosphorText } from '@d3ro/ui/components/ds'
import { d3roPalette, d3roFontMono, d3roTypo } from '@d3ro/ui/theme'
interface AuditDiffViewerProps {
beforeData: Record<string, unknown> | null
afterData: Record<string, unknown> | null
}
interface DiffEntry {
key: string
before: unknown
after: unknown
type: 'added' | 'removed' | 'changed' | 'unchanged'
}
function computeDiff(
before: Record<string, unknown> | null,
after: Record<string, unknown> | null
): DiffEntry[] {
const allKeys = new Set<string>([
...Object.keys(before ?? {}),
...Object.keys(after ?? {}),
])
const entries: DiffEntry[] = []
for (const key of allKeys) {
const bVal = before?.[key]
const aVal = after?.[key]
const bStr = JSON.stringify(bVal)
const aStr = JSON.stringify(aVal)
if (bVal === undefined) {
entries.push({ key, before: undefined, after: aVal, type: 'added' })
} else if (aVal === undefined) {
entries.push({ key, before: bVal, after: undefined, type: 'removed' })
} else if (bStr !== aStr) {
entries.push({ key, before: bVal, after: aVal, type: 'changed' })
} else {
entries.push({ key, before: bVal, after: aVal, type: 'unchanged' })
}
}
// changed/added/removed first
return entries.sort((a, b) => {
const order = { changed: 0, added: 1, removed: 2, unchanged: 3 }
return order[a.type] - order[b.type]
})
}
const typeColors: Record<DiffEntry['type'], string> = {
added: d3roPalette.tag.green,
removed: d3roPalette.tag.red,
changed: d3roPalette.accent.amber,
unchanged: d3roPalette.text.muted,
}
const typeLabels: Record<DiffEntry['type'], string> = {
added: '+',
removed: '-',
changed: '~',
unchanged: ' ',
}
function formatValue(val: unknown): string {
if (val === undefined) return '(none)'
if (val === null) return 'null'
if (typeof val === 'string') return `"${val}"`
return JSON.stringify(val)
}
export function AuditDiffViewer({ beforeData, afterData }: AuditDiffViewerProps): React.ReactElement {
if (!beforeData && !afterData) {
return <PhosphorText variant="dim">No diff data</PhosphorText>
}
const diff = computeDiff(beforeData, afterData)
return (
<Box sx={{
fontFamily: d3roFontMono,
fontSize: d3roTypo.small.size,
bgcolor: d3roPalette.bg.inset,
borderRadius: 1,
p: 1.5,
overflow: 'auto',
}}>
{diff.map((entry) => (
<Box
key={entry.key}
sx={{
display: 'flex',
gap: 1,
py: 0.25,
opacity: entry.type === 'unchanged' ? 0.5 : 1,
}}
>
<Box sx={{ color: typeColors[entry.type], width: 12, flexShrink: 0, textAlign: 'center' }}>
{typeLabels[entry.type]}
</Box>
<Box sx={{ color: d3roPalette.text.label, minWidth: 140, flexShrink: 0 }}>
{entry.key}
</Box>
{entry.type === 'changed' ? (
<Box>
<Box component="span" sx={{ color: d3roPalette.tag.red, textDecoration: 'line-through' }}>
{formatValue(entry.before)}
</Box>
<Box component="span" sx={{ mx: 0.5, color: d3roPalette.text.muted }}>{'->'}</Box>
<Box component="span" sx={{ color: d3roPalette.tag.green }}>
{formatValue(entry.after)}
</Box>
</Box>
) : (
<Box sx={{ color: typeColors[entry.type] }}>
{entry.type === 'removed' ? formatValue(entry.before) : formatValue(entry.after)}
</Box>
)}
</Box>
))}
</Box>
)
}