fix(bill): filter create payload by template components

- 只提交模板声明过的字段,避免 412「无法识别的属性」
- 缺失必填字段时本地报错,而不是让服务端拒绝
- 版本化 specificationId 解析已在前一提交修复
This commit is contained in:
gitea committed 2026-10-23 17:41:05 +08:00
1 parent 3c00a0c0a6
commit dda9b2c7a0
3 files changed
+216 -136

No files matched your search

+42
View File
@@ -0,0 +1,42 @@
import { describe, expect, it } from "bun:test";
import { filterBillFormByTemplate } from "./create.js";
const components = [
{ field: "title", optional: false },
{ field: "submitterId", optional: false },
{ field: "description", optional: true },
{ field: "u_自定义字段", optional: true },
];
describe("filterBillFormByTemplate", () => {
it("只保留模板声明的字段,剔除未声明字段", () => {
const { form, missingRequired } = filterBillFormByTemplate(
{
specificationId: "spec",
title: "t",
submitterId: "s",
u_发票总金额: { standard: "0" },
details: [],
},
components
);
expect(form).toEqual({ specificationId: "spec", title: "t", submitterId: "s", details: [] });
expect(missingRequired).toEqual([]);
});
it("返回缺失的必填字段", () => {
const { missingRequired } = filterBillFormByTemplate(
{ specificationId: "spec", details: [] },
components
);
expect(missingRequired).toEqual(["title", "submitterId"]);
});
it("模板没有 components 时保留 specificationId 与 details", () => {
const { form } = filterBillFormByTemplate(
{ specificationId: "spec", title: "t", details: [] },
[]
);
expect(form).toEqual({ specificationId: "spec", details: [] });
});
});
+39 -1
View File
@@ -27,6 +27,34 @@ export interface CreateBillOptions {
expenseDate?: number;
}
export interface TemplateComponent {
field?: string;
optional?: boolean;
type?: string;
}
/**
* 按模板字段白名单过滤表单:只保留模板声明过的字段 + specificationId/details。
* 返回缺失的必填字段,供调用方明确报错(而不是让服务端 412)。
*/
export function filterBillFormByTemplate(
form: Record<string, unknown>,
components: TemplateComponent[]
): { form: Record<string, unknown>; missingRequired: string[] } {
const allowed = new Set<string>(["specificationId", "details"]);
for (const component of components) {
if (component.field) allowed.add(component.field);
}
const filtered: Record<string, unknown> = {};
for (const [key, value] of Object.entries(form)) {
if (allowed.has(key)) filtered[key] = value;
}
const missingRequired = components
.filter((component) => component.field && component.optional === false && !(component.field in filtered))
.map((component) => component.field as string);
return { form: filtered, missingRequired };
}
export interface CreateBillResult {
flowId: string;
code: string;
@@ -162,7 +190,7 @@ export async function createBillDraft(
},
};
const form = {
const candidate = {
specificationId: versionedSpecificationId,
title,
submitterId,
@@ -171,6 +199,16 @@ export async function createBillDraft(
...(options.reason?.trim() ? { description: options.reason.trim() } : {}),
details: [detail],
};
const { form, missingRequired } = filterBillFormByTemplate(
candidate as Record<string, unknown>,
((specification as { components?: TemplateComponent[] }).components ?? []) as TemplateComponent[]
);
if (missingRequired.length > 0) {
throw new ValidationError(
`模板要求以下字段但 CLI 未提供:${missingRequired.join("、")}。` +
`当前 create 仅自动填充标准表头与一条费用明细,请补充对应模板字段后再创建。`
);
}
const response = await client.post("api/flow/v1/flows", {
searchParams: { corpId },