Ver Fonte

fix(web): 补齐客户更新和测试数据的版本字段

sy há 8 horas atrás
pai
commit
55c363ec28

+ 8 - 0
web-scaffold/src/pages/ContractDetailPages.tsx

@@ -1100,6 +1100,7 @@ export function ContractEditorPage() {
     };
     setSubmitting(true);
     setError("");
+    let customerUpdated = false;
     try {
       if (editing) {
         const row = record.data;
@@ -1119,8 +1120,13 @@ export function ContractEditorPage() {
          * 两个事务,⛔ 合并——写回失败则整次不签合同。
          */
         if (customerId) {
+          const rowVersion = archivedCustomer?.row_version;
+          if (archivedCustomer?.customer_id !== customerId || archived.loading || archived.error || typeof rowVersion !== "number" || !Number.isSafeInteger(rowVersion) || rowVersion < 1) {
+            throw new ApiError(null, "未取得当前客户的档案版本,请重新读取客户资料后重试。");
+          }
           await customerUpdate({
             customer_id: customerId,
+            row_version: rowVersion,
             address: inline.address.trim(),
             contact_name: inline.businessName.trim(),
             contact_mobile: inline.businessPhone.trim(),
@@ -1133,6 +1139,7 @@ export function ContractEditorPage() {
               ? { license: { object_key: licenseObj.object_key, file_name: licenseObj.file_name } }
               : {}),
           });
+          customerUpdated = true;
         }
         const res = await contractCreate({
           ...shared,
@@ -1185,6 +1192,7 @@ export function ContractEditorPage() {
       const message = e instanceof ApiError ? e.message : "保存失败,请稍后重试。";
       setError(message);
       focusFirstErrorItem(message);
+      if (customerUpdated) await archived.reloadAsync();
       setSubmitting(false);
     }
   }

+ 5 - 1
web-scaffold/src/pages/CustomerEditorPage.tsx

@@ -204,6 +204,10 @@ export function CustomerEditorPage() {
         setCreated({ id: customerId, userId: res.user_id ?? null, code: res.activation_code ?? null });
         return;
       }
+      const rowVersion = original?.row_version;
+      if (typeof rowVersion !== "number" || !Number.isSafeInteger(rowVersion) || rowVersion < 1) {
+        throw new ApiError(null, "未取得客户档案版本,请刷新后重试。");
+      }
       await customerUpdate({
         customer_id: no,
         customer_name: draft.name.trim(),
@@ -214,7 +218,7 @@ export function CustomerEditorPage() {
         contact_mobile: draft.phone.trim() || undefined,
         address: draft.address.trim() || undefined,
         ops_team_ids: teamsPatch,
-        row_version: typeof original?.row_version === "number" ? original.row_version : undefined,
+        row_version: rowVersion,
       });
       if (draft.operatorId !== (original?.operator_id ?? "") || draft.partnerId !== (original?.partner_id ?? "")) {
         await customerAttributionUpdate({ customer_id: no, operator_id: draft.operatorId || undefined, partner_id: draft.partnerId || undefined });

+ 14 - 19
web-scaffold/src/pages/CustomerWorkspacePage.tsx

@@ -406,13 +406,9 @@ export function CustomerWorkspacePage() {
    */
   /** 详情口下发的乐观锁版本;行内 `customer/update` 必须原样带回(D-12/R-2)。 */
   const rowVersion = (() => {
-    const raw = customer?.row_version as number | string | null | undefined;
-    if (typeof raw === "number" && Number.isFinite(raw)) return raw;
-    if (typeof raw === "string" && raw.trim() !== "") {
-      const n = Number(raw);
-      return Number.isFinite(n) ? n : undefined;
-    }
-    return undefined;
+    const raw = customer?.row_version;
+    const version = Number(raw);
+    return Number.isSafeInteger(version) && version > 0 ? version : undefined;
   })();
 
   async function commitField(key: keyof ProfileDraft) {
@@ -430,32 +426,31 @@ export function CustomerWorkspacePage() {
     if (key === "idNo" && !/^[A-Z0-9]{18}$|^\d{15}$/.test(idNo)) { setFieldError({ key, message: "识别号格式不符:应为 18 位统一社会信用代码/组织代码,或 15 位旧营业执照号" }); return; }
     // 值没变:不写接口、不留痕,直接退出编辑。
     if (sameAsBaseline(key, current)) { setEditingField(null); setFieldError(null); setIdBasis(""); return; }
-    const needsRowVersion = key === "name" || key === "subject" || key === "idNo" || key === "contact" || key === "phone" || key === "address" || key === "teamId";
-    if (needsRowVersion && rowVersion === undefined) {
-      setFieldError({ key, message: "详情未下发 row_version,无法带版本提交;请刷新页面后重试。" });
-      return;
-    }
+    const updateField = (patch: Omit<Parameters<typeof customerUpdate>[0], "customer_id" | "row_version">) => {
+      if (rowVersion === undefined) throw new Error("未取得客户档案版本,请刷新后重试。");
+      return customerUpdate({ ...patch, customer_id: no, row_version: rowVersion });
+    };
     setBusy(true);
     setFieldError(null);
     try {
       switch (key) {
         case "name":
-          await customerUpdate({ customer_id: no, customer_name: current.name.trim(), row_version: rowVersion });
+          await updateField({ customer_name: current.name.trim() });
           break;
         case "subject":
-          await customerUpdate({ customer_id: no, subject_type: current.subject, row_version: rowVersion });
+          await updateField({ subject_type: current.subject });
           break;
         case "idNo":
-          await customerUpdate({ customer_id: no, id_code: idNo, row_version: rowVersion, ...(idBasis.trim() ? { change_basis: idBasis.trim() } : {}) });
+          await updateField({ id_code: idNo, ...(idBasis.trim() ? { change_basis: idBasis.trim() } : {}) });
           break;
         case "contact":
-          await customerUpdate({ customer_id: no, contact_name: current.contact.trim(), row_version: rowVersion });
+          await updateField({ contact_name: current.contact.trim() });
           break;
         case "phone":
-          await customerUpdate({ customer_id: no, contact_mobile: current.phone.trim(), row_version: rowVersion });
+          await updateField({ contact_mobile: current.phone.trim() });
           break;
         case "address":
-          await customerUpdate({ customer_id: no, address: current.address.trim(), row_version: rowVersion });
+          await updateField({ address: current.address.trim() });
           break;
         case "operatorId":
         case "partnerId":
@@ -466,7 +461,7 @@ export function CustomerWorkspacePage() {
           await customerMoveRegion({ customer_id: no, new_region_node_id: current.regionId });
           break;
         case "teamId":
-          await customerUpdate({ customer_id: no, ops_team_ids: [current.teamId], row_version: rowVersion });
+          await updateField({ ops_team_ids: [current.teamId] });
           break;
       }
       invalidateCustomerList();

+ 1 - 1
web-scaffold/src/pages/__tests__/partner-management.test.tsx

@@ -21,7 +21,7 @@ vi.mock("../../lib/api/bos", async (load) => ({
 vi.mock("../../lib/organization-demo", async (load) => {
   const partner: Partner = {
     id: "PARTNER", name: "浙江渠道合伙人", region: "PROVINCE", title: "", notify: "",
-    account: "在用", login: "zhejiang-channel", userId: "USER-1", shareMode: "按比例", shareValue: 0.08,
+    account: "在用", login: "zhejiang-channel", userId: "USER-1", shareMode: "按比例", shareValue: 0.08, rowVersion: 1,
     scope: { regions: ["PROVINCE"], enterprises: [] }, scopeCustomerIds: ["LEGACY"],
   };
   const data: OrganizationData = {

+ 2 - 2
web-scaffold/src/pages/__tests__/share-rule-ledger.test.tsx

@@ -16,7 +16,7 @@ vi.mock("../../lib/api/bos", async (load) => ({
   customerGetList: async () => ({ items: [] }),
 }));
 vi.mock("../../lib/organization-demo", async (load) => {
-  const base = { region: "P", reconcileDay: 5, payCycle: "按月", notify: "", account: "在用" as const, login: "login-x" };
+  const base = { rowVersion: 1, region: "P", reconcileDay: 5, payCycle: "按月", notify: "", account: "在用" as const, login: "login-x" };
   const operators: Operator[] = [
     { ...base, id: "OP-R", name: "比例运营商", brand: "电信", shareMode: "按比例", shareValue: 0.1 },
     { ...base, id: "OP-A", name: "金额运营商", brand: "联通", shareMode: "按金额", shareValue: 1200 },
@@ -25,7 +25,7 @@ vi.mock("../../lib/organization-demo", async (load) => {
   ];
   const partnerBase: Partner = {
     id: "PT-1", name: "金额合伙人", region: "P", title: "", notify: "", account: "在用", login: "partner-x",
-    scope: { regions: ["P"], enterprises: [] }, shareMode: "按金额", shareValue: 120,
+    scope: { regions: ["P"], enterprises: [] }, shareMode: "按金额", shareValue: 120, rowVersion: 1,
   };
   const partners: Partner[] = [partnerBase, { ...partnerBase, id: "PT-2", name: "每账号合伙人", shareMode: "按每账号", shareValue: 1200 }];
   const data: OrganizationData = {