Files
nekonest-cloud/app/api/account/deletion/route.ts
T

62 lines
1.8 KiB
TypeScript

import { getCloudViewer } from "@/app/cloud-auth";
import {
cancelAccountDeletion,
DomainError,
getOrCreateAccount,
requestAccountDeletion,
} 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, headers: { "cache-control": "no-store" } },
);
}
const payload = await readJsonMutation<{
action?: string;
requestId?: string;
confirmed?: boolean;
reason?: string;
idempotencyKey?: string;
}>(request);
const account = await getOrCreateAccount(viewer);
if (payload.action === "request") {
const deletionRequest = await requestAccountDeletion({
accountId: account.id,
actorId: viewer.userId,
confirmed: payload.confirmed === true,
reason: payload.reason ?? "",
idempotencyKey: payload.idempotencyKey ?? "",
});
return Response.json(
{ deletionRequest },
{ status: 201, headers: { "cache-control": "no-store" } },
);
}
if (payload.action === "cancel") {
const deletionRequest = await cancelAccountDeletion({
accountId: account.id,
actorId: viewer.userId,
requestId: payload.requestId ?? "",
});
return Response.json(
{ deletionRequest },
{ headers: { "cache-control": "no-store" } },
);
}
throw new DomainError(
"invalid_deletion_action",
"请选择有效的注销申请操作",
);
} catch (error) {
const response = apiError(error);
response.headers.set("cache-control", "no-store");
return response;
}
}