62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
import { getCloudViewer } from "@/app/cloud-auth";
|
|
import {
|
|
createServiceIncident,
|
|
resolveServiceIncident,
|
|
} from "@/db/repository";
|
|
import { apiError, readJsonMutation } from "../../respond";
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const viewer = await getCloudViewer();
|
|
if (!viewer) {
|
|
return Response.json(
|
|
{ error: "authentication_required", message: "请先登录" },
|
|
{ status: 401 },
|
|
);
|
|
}
|
|
if (!viewer.isAdmin) {
|
|
return Response.json(
|
|
{ error: "forbidden", message: "没有公测后台权限" },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
const payload = await readJsonMutation<{
|
|
action?: string;
|
|
incidentId?: string;
|
|
severity?: string;
|
|
title?: string;
|
|
message?: string;
|
|
resolution?: string;
|
|
reason?: string;
|
|
idempotencyKey?: string;
|
|
}>(request);
|
|
|
|
if (payload.action === "create") {
|
|
const incident = await createServiceIncident({
|
|
actorId: viewer.userId,
|
|
severity: payload.severity ?? "",
|
|
title: payload.title ?? "",
|
|
message: payload.message ?? "",
|
|
reason: payload.reason ?? "",
|
|
idempotencyKey: payload.idempotencyKey ?? "",
|
|
});
|
|
return Response.json({ incident }, { status: 201 });
|
|
}
|
|
if (payload.action === "resolve") {
|
|
const incident = await resolveServiceIncident({
|
|
incidentId: payload.incidentId ?? "",
|
|
actorId: viewer.userId,
|
|
resolution: payload.resolution ?? "",
|
|
reason: payload.reason ?? "",
|
|
});
|
|
return Response.json({ incident });
|
|
}
|
|
return Response.json(
|
|
{ error: "invalid_incident_action", message: "请选择有效的故障公告操作" },
|
|
{ status: 400 },
|
|
);
|
|
} catch (error) {
|
|
return apiError(error);
|
|
}
|
|
}
|