[refactor] 訂閱查詢與 MCP 欄位組裝去重,壓 update_subscription 的認知複雜度 - #67
Merged
Merged
Conversation
✅ SonarQube Quality Gate passed — pathorsAI_internal0 open issues on this PR. |
YJack0000
enabled auto-merge (squash)
September 7, 2026 16:35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
接 #64 的後續修正。#64 已經 merge 進 main,但它的 Sonar quality gate 沒過,兩個條件都是 #64 自己新增的程式碼造成的,所以這裡單獨修掉。
1.
typescript:S3776CRITICAL —update_subscription認知複雜度 17(上限 15)src/lib/mcp/tools-client.ts的update_subscription.execute裡疊了十幾個if (... !== undefined) patch.x = ...,加上contractId的 unlink 分支之後超過門檻。subscriptionPatch(args):判斷「有沒有帶這個欄位」的邏輯收在內部的put()裡只寫一次,欄位變成一行一個宣告式呼叫。execute本體只剩兩個if。contractId的行為完全不變:明確傳null仍然是解除綁定,完全沒帶這個 key 仍然是不動它(optNumber會把兩者都收斂成undefined,所以null要先自己攔一次)。2.
new_duplicated_lines_density3.81%(上限 3%)src/db/queries.ts—listSubscriptions/getSubscription/listBillingBoard這三條訂閱讀取路徑,在 #64 補上contractId/contractTitle與leftJoin(contracts)之後,變成三份幾乎一模一樣的 select + join(jscpd 抓到 11 行與 7 行兩組 clone)。收成一個subscriptionRows(db)builder,三邊共用,往後加欄位只要改一個地方。順帶把看板那支原本獨有的
customerTaxId併進共用欄位,三條路徑形狀一致。getSubscriptionSchedule是自己組回傳物件的,多出來的欄位不會外流到任何 MCP output schema。src/lib/mcp/tools-client.ts—create_/update_×subscription/contract四支各抄一份「有帶才驗歸屬」的外鍵檢查。抽成assertClientRefs(db, orgId, {customerPartyId, projectId, contractId}),跨租戶檢查也因此只剩一個攔截點。另外把SUBSCRIPTION_ROW_PROPS的contractId移出與CONTRACT_ROW_PROPS重疊的前綴(純欄位順序調整,required仍由Object.keys推導,schema 語意不變)。jscpd 在同一組檔案上從 22 clones / 193 duplicated lines 降到 17 clones / 153 lines,消掉的 5 組正好都在 #64 的新程式碼裡。
驗證
bun run lint通過。bunx tsc --noEmit:src/零錯誤。bun run build通過。migrations/0022_subscription_contract.sql仍待手動套 prod)。