feat: add admin accounts and image templates
This commit is contained in:
51
app/api/admin/accounts/groups/route.ts
Normal file
51
app/api/admin/accounts/groups/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { jsonError, jsonOk, readJsonBody } from "@/lib/server/api";
|
||||
import { requireAdminSession } from "@/lib/server/auth/current-user";
|
||||
import {
|
||||
createOrganizationGroup,
|
||||
getOrganizationApiConfig,
|
||||
listOrganizationGroups
|
||||
} 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.configured) {
|
||||
throw Object.assign(new Error(`组织接口配置不完整:${config.missing.join(", ")}`), { status: 503 });
|
||||
}
|
||||
const body = await readJsonBody<Record<string, unknown>>(request);
|
||||
const organizationId = requiredString(body, "organizationId", "组织");
|
||||
const groupName = requiredString(body, "groupName", "部门名称");
|
||||
await createOrganizationGroup({
|
||||
organizationId,
|
||||
groupName,
|
||||
groupDesc: optionalString(body.groupDesc),
|
||||
parentId: optionalString(body.parentId)
|
||||
}, context);
|
||||
const groups = await listOrganizationGroups(organizationId, context);
|
||||
const group = groups.find((item) => item.groupName === groupName) || null;
|
||||
return jsonOk({ ok: true, group, groups });
|
||||
} catch (error) {
|
||||
return jsonError(error, 500, { request, source: "api.admin.accounts.groups", logClientErrors: true });
|
||||
}
|
||||
}
|
||||
|
||||
function requiredString(body: Record<string, unknown>, key: string, label: string): string {
|
||||
const value = optionalString(body[key]);
|
||||
if (!value) throw badRequest(`${label}不能为空。`);
|
||||
return value;
|
||||
}
|
||||
|
||||
function optionalString(value: unknown): string | undefined {
|
||||
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function badRequest(message: string): Error & { status: number } {
|
||||
const error = new Error(message) as Error & { status: number };
|
||||
error.status = 400;
|
||||
return error;
|
||||
}
|
||||
Reference in New Issue
Block a user