Implement ops operational closure
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
function assert(condition: unknown, message: string) {
|
||||
if (!condition) throw new Error(message);
|
||||
}
|
||||
|
||||
export function runTests() {
|
||||
const projectRoot = process.cwd();
|
||||
const opsApi = fs.readFileSync(path.join(projectRoot, "lib", "ops-api.ts"), "utf8");
|
||||
const opsTypes = fs.readFileSync(path.join(projectRoot, "types", "ops.ts"), "utf8");
|
||||
const sidebar = fs.readFileSync(
|
||||
path.join(projectRoot, "components", "ops", "layout", "AdminSidebar.tsx"),
|
||||
"utf8",
|
||||
);
|
||||
const paymentsPage = fs.readFileSync(
|
||||
path.join(projectRoot, "components", "ops", "payments", "PaymentsPageClient.tsx"),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
assert(
|
||||
opsApi.includes("auditLog(") &&
|
||||
opsApi.includes("/api/ops/audit-log") &&
|
||||
opsApi.includes("refunds(") &&
|
||||
opsApi.includes("/api/ops/refunds") &&
|
||||
opsApi.includes("updateRefund("),
|
||||
"ops API client must expose audit log and refund case endpoints",
|
||||
);
|
||||
assert(
|
||||
opsTypes.includes("OpsAuditEvent") &&
|
||||
opsTypes.includes("RefundCase") &&
|
||||
opsTypes.includes("refund_case_id") &&
|
||||
opsTypes.includes("refund_status"),
|
||||
"ops types must model audit events, refund cases, and incident refund metadata",
|
||||
);
|
||||
assert(
|
||||
sidebar.includes("/ops/audit-log") && sidebar.includes("审计日志"),
|
||||
"ops sidebar must expose the unified audit log page",
|
||||
);
|
||||
assert(
|
||||
paymentsPage.includes("退款工单") &&
|
||||
paymentsPage.includes("refund_case_id") &&
|
||||
paymentsPage.includes("refund_status") &&
|
||||
paymentsPage.includes("opsApi.refunds"),
|
||||
"ops payment page must surface refund cases next to payment incidents",
|
||||
);
|
||||
|
||||
const auditRoute = path.join(projectRoot, "app", "api", "ops", "audit-log", "route.ts");
|
||||
const refundsRoute = path.join(projectRoot, "app", "api", "ops", "refunds", "route.ts");
|
||||
const refundUpdateRoute = path.join(projectRoot, "app", "api", "ops", "refunds", "[caseId]", "route.ts");
|
||||
for (const route of [auditRoute, refundsRoute, refundUpdateRoute]) {
|
||||
const source = fs.readFileSync(route, "utf8");
|
||||
assert(
|
||||
source.includes("requireOpsProxyAuth") && source.includes("no-store"),
|
||||
`${path.basename(path.dirname(route))} ops proxy route must be admin-protected and uncached`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { RefreshCcw } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { opsApi } from "@/lib/ops-api";
|
||||
import type { OpsAuditEvent } from "@/types/ops";
|
||||
|
||||
function compactDate(value?: string) {
|
||||
if (!value) return "--";
|
||||
return value.slice(0, 19).replace("T", " ");
|
||||
}
|
||||
|
||||
function actionLabel(action?: string) {
|
||||
const key = String(action || "").trim().toLowerCase();
|
||||
if (key === "manual_points_grant") return "手动补分";
|
||||
if (key === "feedback_reward_grant") return "反馈奖励";
|
||||
if (key === "subscription_manual_grant") return "手动开通";
|
||||
if (key === "subscription_manual_extend") return "会员延期";
|
||||
if (key === "refund_case_create") return "创建退款工单";
|
||||
if (key === "refund_case_update") return "更新退款工单";
|
||||
return key || "未知操作";
|
||||
}
|
||||
|
||||
export function AuditLogPageClient() {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [events, setEvents] = useState<OpsAuditEvent[]>([]);
|
||||
|
||||
const load = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const payload = (await opsApi.auditLog(150)) as { events?: OpsAuditEvent[] };
|
||||
setEvents(payload.events ?? []);
|
||||
} catch {
|
||||
setEvents([]);
|
||||
}
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-bold text-white">审计日志</h1>
|
||||
<Button variant="outline" size="sm" onClick={load} className="gap-1.5">
|
||||
<RefreshCcw className="h-3.5 w-3.5" /> 刷新
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>管理员操作 ({events.length})</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{loading ? (
|
||||
<div className="text-sm text-slate-500">加载中...</div>
|
||||
) : events.length === 0 ? (
|
||||
<div className="text-sm text-slate-500">暂无审计事件</div>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-slate-200 text-left text-slate-500">
|
||||
<th className="py-2 pr-4 font-bold">时间</th>
|
||||
<th className="py-2 pr-4 font-bold">操作</th>
|
||||
<th className="py-2 pr-4 font-bold">管理员</th>
|
||||
<th className="py-2 pr-4 font-bold">对象</th>
|
||||
<th className="py-2 pr-4 font-bold">详情</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{events.map((event) => (
|
||||
<tr key={event.id} className="border-b border-slate-100">
|
||||
<td className="whitespace-nowrap py-2 pr-4 font-mono text-xs text-slate-500">
|
||||
{compactDate(event.created_at)}
|
||||
</td>
|
||||
<td className="py-2 pr-4 font-bold text-slate-900">
|
||||
{actionLabel(event.action)}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-xs text-slate-600">
|
||||
{event.actor_email || "--"}
|
||||
</td>
|
||||
<td className="py-2 pr-4">
|
||||
<div className="font-mono text-xs text-blue-700">
|
||||
{event.target_email || event.target_user_id || event.target_id || "--"}
|
||||
</div>
|
||||
<div className="mt-0.5 text-[11px] text-slate-500">
|
||||
{event.target_type || "--"}
|
||||
</div>
|
||||
</td>
|
||||
<td className="max-w-md py-2 pr-4">
|
||||
<code className="block truncate rounded bg-slate-100 px-2 py-1 text-[11px] text-slate-600">
|
||||
{JSON.stringify(event.payload ?? {})}
|
||||
</code>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -44,6 +44,7 @@ const navGroups = [
|
||||
items: [
|
||||
{ href: "/ops/config", icon: Settings, label: "系统配置" },
|
||||
{ href: "/ops/subscriptions", icon: ScrollText, label: "订阅操作" },
|
||||
{ href: "/ops/audit-log", icon: FileText, label: "审计日志" },
|
||||
{ href: "/ops/view-logs", icon: FileText, label: "日志查看" },
|
||||
],
|
||||
},
|
||||
|
||||
@@ -12,6 +12,7 @@ import type {
|
||||
PaymentRuntimePayload,
|
||||
PaymentIncident,
|
||||
PaymentRecord,
|
||||
RefundCase,
|
||||
} from "@/types/ops";
|
||||
|
||||
const PaymentIncidentPieChart = dynamic(
|
||||
@@ -60,6 +61,8 @@ function paymentReasonLabel(reason?: string) {
|
||||
if (key === "expired") return "订单已过期";
|
||||
if (key === "event_mismatch") return "支付事件不匹配";
|
||||
if (key === "direct_transfer_mismatch") return "直接转账不匹配";
|
||||
if (key === "refund_required") return "需要退款处理";
|
||||
if (key === "duplicate_payment") return "重复付款";
|
||||
if (key === "unknown") return "未知原因";
|
||||
return key || "未知原因";
|
||||
}
|
||||
@@ -96,21 +99,24 @@ export function PaymentsPageClient() {
|
||||
const [runtime, setRuntime] = useState<PaymentRuntimePayload | null>(null);
|
||||
const [incidents, setIncidents] = useState<PaymentIncident[]>([]);
|
||||
const [payments, setPayments] = useState<PaymentRecord[]>([]);
|
||||
const [refunds, setRefunds] = useState<RefundCase[]>([]);
|
||||
const [risk, setRisk] = useState<BillingRiskPayload | null>(null);
|
||||
const [resolving, setResolving] = useState<Set<number>>(new Set());
|
||||
|
||||
const load = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [rt, inc, pay, riskPayload] = await Promise.all([
|
||||
const [rt, inc, pay, refundPayload, riskPayload] = await Promise.all([
|
||||
opsApi.paymentRuntime() as Promise<PaymentRuntimePayload>,
|
||||
opsApi.incidents(50),
|
||||
opsApi.listPayments(50),
|
||||
opsApi.refunds(50),
|
||||
opsApi.billingRisk(30, 80) as Promise<BillingRiskPayload>,
|
||||
]);
|
||||
setRuntime(rt);
|
||||
setIncidents((inc as unknown as { incidents?: PaymentIncident[] }).incidents ?? []);
|
||||
setPayments((pay as unknown as { payments?: PaymentRecord[] }).payments ?? []);
|
||||
setRefunds((refundPayload as unknown as { refunds?: RefundCase[] }).refunds ?? []);
|
||||
setRisk(riskPayload);
|
||||
} catch { /* */ }
|
||||
setLoading(false);
|
||||
@@ -328,6 +334,11 @@ export function PaymentsPageClient() {
|
||||
<div className="mt-0.5 max-w-xl truncate text-xs text-slate-500" title={inc.detail || inc.reason || ""}>
|
||||
{inc.detail || inc.reason || "—"}
|
||||
</div>
|
||||
{inc.refund_case_id ? (
|
||||
<div className="mt-1 text-[11px] font-semibold text-blue-700">
|
||||
退款工单 #{inc.refund_case_id} · {inc.refund_status || "open"}
|
||||
</div>
|
||||
) : null}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-xs text-slate-500">
|
||||
<div className="font-mono" title={inc.user_id || ""}>{compactMono(inc.user_id)}</div>
|
||||
@@ -355,6 +366,54 @@ export function PaymentsPageClient() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader><CardTitle>退款工单 ({refunds.length})</CardTitle></CardHeader>
|
||||
<CardContent>
|
||||
{refunds.length === 0 ? (
|
||||
<span className="text-sm text-slate-500">暂无退款或售后工单</span>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<thead>
|
||||
<tr className="border-b border-white/10 text-left text-slate-400">
|
||||
<th className="py-2 pr-4 font-medium">ID</th>
|
||||
<th className="py-2 pr-4 font-medium">状态 / 原因</th>
|
||||
<th className="py-2 pr-4 font-medium">用户 / Intent</th>
|
||||
<th className="py-2 pr-4 font-medium">Tx Hash</th>
|
||||
<th className="py-2 pr-4 font-medium">处理人</th>
|
||||
<th className="py-2 pr-4 font-medium">更新时间</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{refunds.map((refund) => (
|
||||
<tr key={refund.id} className="border-b border-white/5">
|
||||
<td className="py-2 pr-4 font-mono text-xs text-slate-500">{refund.id}</td>
|
||||
<td className="py-2 pr-4">
|
||||
<div className="font-bold text-slate-900">{refund.status || "open"}</div>
|
||||
<div className="text-xs text-amber-600">{paymentReasonLabel(refund.reason)}</div>
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-xs text-slate-500">
|
||||
<div className="font-mono" title={refund.user_id || ""}>{compactMono(refund.user_id)}</div>
|
||||
<div className="mt-0.5 font-mono text-blue-700" title={refund.intent_id || ""}>{compactMono(refund.intent_id, 12, 6)}</div>
|
||||
</td>
|
||||
<td className="py-2 pr-4 font-mono text-xs text-slate-500" title={refund.tx_hash || ""}>
|
||||
{compactMono(refund.tx_hash)}
|
||||
</td>
|
||||
<td className="py-2 pr-4 text-xs text-slate-500">
|
||||
{refund.handled_by || refund.created_by || "—"}
|
||||
</td>
|
||||
<td className="py-2 pr-4 whitespace-nowrap text-xs text-slate-500">
|
||||
{compactDate(refund.updated_at || refund.created_at)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader><CardTitle>成功支付记录 ({payments.length})</CardTitle></CardHeader>
|
||||
<CardContent>
|
||||
|
||||
Reference in New Issue
Block a user