Browse Source

fix(contract): 孤儿附件回收失败关闭;移除未落库附件时当场退回

- attachment_discard:引用面查询结果不可解析时不再按 0 处理(原逻辑把一次查询故障
  当成「没人引用」⇒ 可能删掉在用的附件),改为返回 internal 并明确「未删除」;补注释说明失败关闭
- attachment_discard:如实登记已知残余——浏览器被强杀/崩溃时上传完成却既未保存也未离开页面,
  那一枚对象仍会无引用留下;本服务不建巡检作业(与「按桶策略回收、不自建清理作业」同口径),
  且合同档案尚无留存期口径,现在落前缀规则会误删仍在引用的附件,故本轮不落
- 前端:保存/创建成功后清空「本页待回收对象键」清单(对象已被合同引用,服务端也会拒删);
  「移除」按钮只对本页刚上传、尚未落库的那一份发起回收,库里那份留待写口在保存后处理

验证:cargo test -p account 104 通过;tsc --noEmit 无错;npm test(94+27)全过

Co-authored-by: Cursor <cursoragent@cursor.com>
cg 1 week ago
parent
commit
f47e4e7087

+ 16 - 1
src/modules/account/src/handlers/contract.rs

@@ -4177,6 +4177,13 @@ pub async fn delete(
 /// 幂等:`referenced=true` 与「对象已不在」都不算失败(前端可放心重试/放弃)。
 /// ⚠️ 不写审计:审计主体是合同,而这份对象**从未挂到合同上**(`resource_id` 无值可用);
 /// 回收结果只回响应与 `tracing`,且对象键按纪律⛔ 进日志。
+///
+/// **已知残余**(如实登记,⛔ 用「会到期」搪塞):浏览器被强杀/崩溃时,上传完成后既没保存
+/// 也没有离开页面的机会 ⇒ 那一枚对象仍会无引用地留下。本服务**不建巡检作业**(与需求
+/// 「影像文件由平台按桶策略回收、系统不自建清理作业」同口径;平台侧也明确不做后台遍历删对象),
+/// 因此这一类的兜底只能在**业务定出附件留存期之后**,按 `klt/customer/license/` 同一通路
+/// (`object/lifecycle/apply@v1`)落一条前缀规则——⚠️ 合同档案至今没有「留存 N 天」口径,
+/// 现在落规则等于按天数删**仍在引用**的合同附件,故本轮不落。
 pub async fn attachment_discard(
     State(state): State<AppState>,
     RequireAuth(auth): RequireAuth,
@@ -4205,6 +4212,7 @@ pub async fn attachment_discard(
     };
 
     // 引用面:任何合同仍指向它 ⇒ 不删。用 COUNT 而不是取 id:不把「谁在用它」跨范围带出去。
+    // ⚠️ 取数/解析失败一律**失败关闭**(不删):把一次查询故障当成「没人引用」就会删掉在用的附件。
     let holders = match state
         .db
         .query_all::<CountRow>(
@@ -4213,7 +4221,14 @@ pub async fn attachment_discard(
         )
         .await
     {
-        Ok(rows) => rows.first().and_then(|r| r.c.parse::<i64>().ok()).unwrap_or(0),
+        Ok(rows) => match rows.first().and_then(|r| r.c.trim().parse::<i64>().ok()) {
+            Some(n) => n,
+            None => {
+                return json_err(BosError::internal(
+                    "附件引用面查询结果不可解析 ⇒ 未删除(失败关闭,不把查询故障当成没人引用)",
+                ))
+            }
+        },
         Err(e) => return json_err(e),
     };
     if holders > 0 {

+ 11 - 1
web-scaffold/src/pages/ContractDetailPages.tsx

@@ -347,6 +347,8 @@ export function ContractEditorPage() {
           row_version: version,
           ...shared,
         });
+        // 落库成功 ⇒ 清单里的对象键已被合同引用(服务端也会拒删),⛔ 别再当孤儿回收。
+        pendingAttachmentKeys.current = [];
         navigate(`/contracts/${encodeURIComponent(row.contract_id)}`);
       } else {
         const res = await contractCreate({
@@ -372,6 +374,8 @@ export function ContractEditorPage() {
                 },
               }),
         });
+        // 合同已生成 ⇒ 附件对象已被引用(服务端也会拒删)⇒ 清空孤儿清单。
+        pendingAttachmentKeys.current = [];
         // 内联建档成功 ⇒ 停在回执页展示客户号与主账号(激活码明文由「导出激活码」取,⛔ 在此伪造)。
         if (res?.inline_customer) {
           setCreated({ contractId: res.contract_id, customer: res.inline_customer });
@@ -715,7 +719,13 @@ export function ContractEditorPage() {
                 <Upload size={15} />{attachmentBusy ? "正在上传…" : draft.attachmentUrl.trim() ? "更换附件" : "选择并上传附件"}
               </Button>
               {attachmentView && <Button variant="ghost" size="sm" onClick={() => window.open(attachmentView, "_blank", "noopener,noreferrer")}>查看附件</Button>}
-              {draft.attachmentUrl.trim() && <Button variant="ghost" size="sm" disabled={attachmentBusy || submitting} onClick={() => { set("attachmentUrl", ""); setAttachmentName(""); setAttachmentError(""); }}><X size={15} />移除</Button>}
+              {draft.attachmentUrl.trim() && <Button variant="ghost" size="sm" disabled={attachmentBusy || submitting} onClick={() => {
+                // 移除的若是「本页刚上传、还没落库」的那一份 ⇒ 它当场失去引用,立即退回;
+                // 库里那份仍被合同引用(保存后才由写口回收),这里⛔ 提前删。
+                const current = draft.attachmentUrl.trim();
+                if (current && current !== record.data?.attachment_url) discardPendingAttachments([current]);
+                set("attachmentUrl", ""); setAttachmentName(""); setAttachmentError("");
+              }}><X size={15} />移除</Button>}
             </div>
             {attachmentError && <p className="ops-form-error" role="alert">{attachmentError}</p>}
             <p className="contract-form-summary">文件选中即直传平台对象存储(字节流与上传凭证都不经本服务),成功后随合同保存对象键;单份,重传即替换,保存前不影响库里已有附件。查看地址由详情读口按需现签。</p>