41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { getCloudViewer } from "@/app/cloud-auth";
|
|
import { beginRelayMigration } from "@/db/relay-migrations";
|
|
import { apiError, readJsonMutation } from "../../respond";
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const viewer = await getCloudViewer();
|
|
if (!viewer) {
|
|
return Response.json(
|
|
{ error_code: "authentication_required", message: "请先登录", retryable: false },
|
|
{ status: 401 },
|
|
);
|
|
}
|
|
if (!viewer.isAdmin) {
|
|
return Response.json(
|
|
{ error_code: "forbidden", message: "没有 Relay 迁移权限", retryable: false },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
const payload = await readJsonMutation<{
|
|
tenant_id?: string;
|
|
target_node_id?: string;
|
|
reason?: string;
|
|
idempotency_key?: string;
|
|
}>(request);
|
|
const migration = await beginRelayMigration({
|
|
tenantId: payload.tenant_id ?? "",
|
|
targetNodeId: payload.target_node_id ?? "",
|
|
actorId: viewer.userId,
|
|
reason: payload.reason ?? "",
|
|
idempotencyKey: payload.idempotency_key ?? "",
|
|
});
|
|
return Response.json(
|
|
{ migration_id: migration.id, state: migration.state, started_at: migration.started_at },
|
|
{ status: 202, headers: { "cache-control": "no-store" } },
|
|
);
|
|
} catch (error) {
|
|
return apiError(error);
|
|
}
|
|
}
|