You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.3 KiB
80 lines
2.3 KiB
import { parseAdminAuth } from "@/lib/api/admin-auth-context";
|
|
import { fail, ok } from "@/lib/response";
|
|
import { writeAudit } from "@/modules/audit/service";
|
|
import { AuthError } from "@/modules/auth/errors";
|
|
import {
|
|
createBusinessCustomer,
|
|
listBusinessCustomers,
|
|
} from "@/modules/customer/business-customer-service";
|
|
|
|
type RouteContext = {
|
|
params: Promise<{ customer_id: string }>;
|
|
};
|
|
|
|
export async function GET(request: Request, context: RouteContext) {
|
|
try {
|
|
parseAdminAuth(request);
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
return fail(error.code, error.message, error.httpStatus);
|
|
}
|
|
throw error;
|
|
}
|
|
const { customer_id: customerId } = await context.params;
|
|
const list = await listBusinessCustomers(customerId);
|
|
return ok({ customer_id: customerId, list });
|
|
}
|
|
|
|
export async function POST(request: Request, context: RouteContext) {
|
|
let auth;
|
|
try {
|
|
auth = parseAdminAuth(request);
|
|
} catch (error) {
|
|
if (error instanceof AuthError) {
|
|
return fail(error.code, error.message, error.httpStatus);
|
|
}
|
|
throw error;
|
|
}
|
|
const { customer_id: customerId } = await context.params;
|
|
let body: unknown;
|
|
try {
|
|
body = await request.json();
|
|
} catch {
|
|
return fail("VALIDATION_FAILED", "请求体格式无效", 400);
|
|
}
|
|
const payload = body as {
|
|
business_customer_id?: unknown;
|
|
name?: unknown;
|
|
external_code?: unknown;
|
|
remark?: unknown;
|
|
};
|
|
if (typeof payload.business_customer_id !== "string") {
|
|
return fail("VALIDATION_FAILED", "业务客户标识无效", 400);
|
|
}
|
|
if (typeof payload.name !== "string") {
|
|
return fail("VALIDATION_FAILED", "客户名称无效", 400);
|
|
}
|
|
try {
|
|
const created = await createBusinessCustomer({
|
|
customerId,
|
|
businessCustomerId: payload.business_customer_id,
|
|
name: payload.name,
|
|
externalCode:
|
|
typeof payload.external_code === "string"
|
|
? payload.external_code
|
|
: undefined,
|
|
remark: typeof payload.remark === "string" ? payload.remark : undefined,
|
|
});
|
|
await writeAudit(
|
|
"business_customer:create",
|
|
auth.userId,
|
|
`${customerId}/${created.business_customer_id}`,
|
|
created,
|
|
);
|
|
return ok(created);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : "创建业务客户失败";
|
|
return fail("VALIDATION_FAILED", message, 400);
|
|
}
|
|
}
|