feat: add system configuration platform
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import { createAccountRecord, createSessionRecord, normalizeEmail, setSessionCookie } from "@/modules/core/server/auth";
|
||||
import { createRegistration, setSessionCookie } 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";
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
@@ -21,6 +19,7 @@ export async function POST(request: Request): Promise<Response> {
|
||||
const name = readString(body, "name");
|
||||
const email = readString(body, "email");
|
||||
const password = readString(body, "password");
|
||||
const organizationId = readString(body, "organizationId");
|
||||
|
||||
if (!name || !email || !password) {
|
||||
return jsonFailure("姓名、邮箱和密码不能为空");
|
||||
@@ -30,59 +29,18 @@ export async function POST(request: Request): Promise<Response> {
|
||||
return jsonFailure("密码至少需要6位");
|
||||
}
|
||||
|
||||
const normalizedEmail = normalizeEmail(email);
|
||||
const created = await updateData((data) => {
|
||||
const exists = data.accounts.some((account) => account.email === normalizedEmail);
|
||||
if (exists) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const account = createAccountRecord({
|
||||
try {
|
||||
const created = await createRegistration({
|
||||
name,
|
||||
email,
|
||||
password,
|
||||
role: "caregiver",
|
||||
organizationId: organizationId || undefined,
|
||||
});
|
||||
const session = createSessionRecord(account.id);
|
||||
|
||||
data.accounts.push(account);
|
||||
data.sessions.push(session);
|
||||
|
||||
return { account, session };
|
||||
});
|
||||
|
||||
if (!created) {
|
||||
return jsonFailure("账号已存在", 409);
|
||||
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,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
action: "account.register",
|
||||
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,
|
||||
status: created.account.status,
|
||||
createdAt: created.account.createdAt,
|
||||
updatedAt: created.account.updatedAt,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user