fix(bill): filter detail fields by fee type schema
- 费用类型结构:feeType.expenseSpecificationId → /api/form/v2/specifications/[id] - 明细只发 schema 声明的字段(amount/apportions),剔除 feeDate 等未声明字段 - 金额作为核心字段始终保留
This commit is contained in:
1 parent
af7362a346
commit
ebcebbfffc
3 files changed
+260
-213
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { filterBillFormByTemplate } from "./create.js";
|
||||
import { filterBillFormByTemplate, filterFeeTypeFormBySpec } from "./create.js";
|
||||
|
||||
const components = [
|
||||
{ field: "title", optional: false },
|
||||
@@ -40,3 +40,26 @@ describe("filterBillFormByTemplate", () => {
|
||||
expect(form).toEqual({ specificationId: "spec", details: [] });
|
||||
});
|
||||
});
|
||||
|
||||
describe("filterFeeTypeFormBySpec", () => {
|
||||
it("剔除费用类型未声明的字段(如 feeDate)", () => {
|
||||
const form = filterFeeTypeFormBySpec(
|
||||
{ amount: { standard: "1" }, feeDate: 123 },
|
||||
[{ field: "amount" }, { field: "apportions" }]
|
||||
);
|
||||
expect(form).toEqual({ amount: { standard: "1" } });
|
||||
});
|
||||
|
||||
it("schema 为空时保留候选字段", () => {
|
||||
const candidate = { amount: { standard: "1" }, feeDate: 123 };
|
||||
expect(filterFeeTypeFormBySpec(candidate, [])).toEqual(candidate);
|
||||
});
|
||||
|
||||
it("保留 schema 声明的其它字段", () => {
|
||||
const form = filterFeeTypeFormBySpec(
|
||||
{ amount: { standard: "1" }, apportions: [], feeDate: 123 },
|
||||
[{ field: "amount" }, { field: "apportions" }]
|
||||
);
|
||||
expect(form).toEqual({ amount: { standard: "1" }, apportions: [] });
|
||||
});
|
||||
});
|
||||
+35
-11
@@ -55,6 +55,22 @@ export function filterBillFormByTemplate(
|
||||
return { form: filtered, missingRequired };
|
||||
}
|
||||
|
||||
/**
|
||||
* 按费用类型 schema 过滤明细字段:费用类型没有声明的字段不发(如 feeDate)。
|
||||
* schema 拉取失败(components 为空)时保留候选字段,由服务端兜底。
|
||||
*/
|
||||
export function filterFeeTypeFormBySpec(
|
||||
candidate: Record<string, unknown>,
|
||||
components: TemplateComponent[]
|
||||
): Record<string, unknown> {
|
||||
const allowed = new Set<string>();
|
||||
for (const component of components) {
|
||||
if (component.field) allowed.add(component.field);
|
||||
}
|
||||
if (allowed.size === 0) return candidate;
|
||||
return Object.fromEntries(Object.entries(candidate).filter(([key]) => allowed.has(key)));
|
||||
}
|
||||
|
||||
export interface CreateBillResult {
|
||||
flowId: string;
|
||||
code: string;
|
||||
@@ -174,20 +190,28 @@ export async function createBillDraft(
|
||||
const expenseDate = options.expenseDate ?? Date.now();
|
||||
const title = options.title?.trim() || specification.name;
|
||||
|
||||
const amountMoney = {
|
||||
standard: amount,
|
||||
standardUnit: "元",
|
||||
standardScale: 2,
|
||||
standardSymbol: "¥",
|
||||
standardNumCode: "156",
|
||||
standardStrCode: "CNY",
|
||||
};
|
||||
const detailSpec = await getFormSpecification(client, corpId, feeType.expenseSpecificationId);
|
||||
const detailComponents = ((detailSpec as { components?: TemplateComponent[] } | undefined)?.components ??
|
||||
[]) as TemplateComponent[];
|
||||
const feeTypeForm = filterFeeTypeFormBySpec(
|
||||
{ amount: amountMoney, feeDate: expenseDate },
|
||||
detailComponents
|
||||
);
|
||||
// 金额是核心字段:即使 schema 未声明也必须保留。
|
||||
if (!("amount" in feeTypeForm)) feeTypeForm.amount = amountMoney;
|
||||
|
||||
const detail = {
|
||||
feeTypeId: feeType.id,
|
||||
specificationId: feeType.expenseSpecificationId,
|
||||
feeTypeForm: {
|
||||
amount: {
|
||||
standard: amount,
|
||||
standardUnit: "元",
|
||||
standardScale: 2,
|
||||
standardSymbol: "¥",
|
||||
standardNumCode: "156",
|
||||
standardStrCode: "CNY",
|
||||
},
|
||||
feeDate: expenseDate,
|
||||
},
|
||||
feeTypeForm,
|
||||
};
|
||||
|
||||
const candidate = {
|
||||
|
||||
Reference in new issue
Block a user