feat: add system configuration platform
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { createAccountRecord, createSessionRecord, setSessionCookie } from "@/modules/core/server/auth";
|
||||
import { setSessionCookie, setupFirstPlatformAdmin } from "@/modules/core/server/auth";
|
||||
import { jsonFailure, jsonSuccess, readJsonBody } from "@/modules/core/server/api";
|
||||
import { recordAuditLog } from "@/modules/core/server/audit";
|
||||
import { updateData } from "@/modules/core/server/store";
|
||||
import { ensureSystemDefaults } from "@/modules/core/server/permissions";
|
||||
|
||||
function readString(source: Record<string, unknown>, key: string): string {
|
||||
const value = source[key];
|
||||
@@ -31,60 +30,14 @@ export async function POST(request: Request): Promise<Response> {
|
||||
return jsonFailure("密码至少需要6位");
|
||||
}
|
||||
|
||||
const created = await updateData((data) => {
|
||||
if (data.accounts.length > 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const account = createAccountRecord({
|
||||
name,
|
||||
email,
|
||||
password,
|
||||
role: "admin",
|
||||
organization,
|
||||
});
|
||||
const session = createSessionRecord(account.id);
|
||||
|
||||
data.accounts.push(account);
|
||||
data.sessions.push(session);
|
||||
|
||||
return { account, session };
|
||||
});
|
||||
|
||||
if (!created) {
|
||||
return jsonFailure("系统已完成初始化", 409);
|
||||
try {
|
||||
await ensureSystemDefaults();
|
||||
const created = await setupFirstPlatformAdmin({ organization, name, email, password });
|
||||
await setSessionCookie(created.session.id);
|
||||
return jsonSuccess("初始化完成", { account: created.account });
|
||||
} catch (error) {
|
||||
const reason = error instanceof Error ? error.message : "初始化失败";
|
||||
const status = reason === "系统已完成初始化" ? 409 : 500;
|
||||
return jsonFailure(reason, status);
|
||||
}
|
||||
|
||||
await setSessionCookie(created.session.id);
|
||||
await recordAuditLog({
|
||||
actor: {
|
||||
id: created.account.id,
|
||||
name: created.account.name,
|
||||
email: created.account.email,
|
||||
role: created.account.role,
|
||||
organization: created.account.organization,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
action: "account.setup",
|
||||
targetType: "account",
|
||||
targetId: created.account.id,
|
||||
result: "success",
|
||||
reason: "初始化管理员账号",
|
||||
});
|
||||
|
||||
return jsonSuccess("初始化完成", {
|
||||
account: {
|
||||
id: created.account.id,
|
||||
name: created.account.name,
|
||||
email: created.account.email,
|
||||
role: created.account.role,
|
||||
organization: created.account.organization,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user