61 lines
2.3 KiB
TypeScript
61 lines
2.3 KiB
TypeScript
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
|
import { requireAdminSession } from "@/lib/server/auth/current-user";
|
|
import { getOrganizationApiConfig, resetPlatformUserPassword } from "@/lib/server/organization-client";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const session = await requireAdminSession();
|
|
const context = { accessToken: session.accessToken };
|
|
const config = getOrganizationApiConfig(context.accessToken);
|
|
if (!config.staffConfigured) {
|
|
throw Object.assign(new Error(`企业端用户接口配置不完整:${config.staffMissing.join(", ")}`), { status: 503 });
|
|
}
|
|
const body = await readJsonBody<Record<string, unknown>>(request);
|
|
await resetPlatformUserPassword({
|
|
tenantId: tenantIdNumber(config.tenantId),
|
|
userId: requiredNumber(body.userId, "用户 ID"),
|
|
newPassword: requiredString(body, "newPassword", "新密码"),
|
|
mustChangePassword: booleanValue(body.mustChangePassword, true)
|
|
}, context);
|
|
return jsonOk({ ok: true });
|
|
} catch (error) {
|
|
return jsonError(error, 500, { request, source: "api.admin.accounts.password", logClientErrors: true });
|
|
}
|
|
}
|
|
|
|
function requiredString(body: Record<string, unknown>, key: string, label: string): string {
|
|
const value = body[key];
|
|
if (typeof value === "string" && value.trim()) return value.trim();
|
|
throw badRequest(`${label}不能为空。`);
|
|
}
|
|
|
|
function requiredNumber(value: unknown, label: string): number {
|
|
const parsed = typeof value === "number" ? value : Number(value);
|
|
if (Number.isFinite(parsed)) return parsed;
|
|
throw badRequest(`${label}必须是数字。`);
|
|
}
|
|
|
|
function tenantIdNumber(value: string): number {
|
|
const tenantId = Number(value);
|
|
if (!Number.isFinite(tenantId)) throw badRequest("租户 ID 必须是数字。");
|
|
return tenantId;
|
|
}
|
|
|
|
function booleanValue(value: unknown, fallback: boolean): boolean {
|
|
if (typeof value === "boolean") return value;
|
|
if (typeof value === "string") {
|
|
if (value === "true") return true;
|
|
if (value === "false") return false;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
function badRequest(message: string): Error & { status: number } {
|
|
const error = new Error(message) as Error & { status: number };
|
|
error.status = 400;
|
|
return error;
|
|
}
|