refactor(data-connections): single definition of the product/connection rule - #481
Draft
alukach wants to merge 2 commits into
Draft
refactor(data-connections): single definition of the product/connection rule#481alukach wants to merge 2 commits into
alukach wants to merge 2 commits into
Conversation
…on rule
`createProduct` reimplemented inline the same two rules `canUseDataConnectionFor`
already encoded: the connection must permit the caller
(`Actions.UseDataConnection`, which covers read-only and flag-gated connections)
and must be available to the product's account (system-level or owned by it).
Two copies of a security rule that have to stay in sync.
The duplication existed because `createProduct` reports each rule as a distinct
form field error, which a boolean predicate can't express. Introduce
`denyDataConnectionFor`, which returns the reason ("not-usable" |
"wrong-account") or null; `canUseDataConnectionFor` becomes the boolean wrapper
over it, and `createProduct` switches on the reason. Both messages and their
`fieldErrors` keys are unchanged, as is the order in which the two rules are
reported.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
Claude finished @alukach's task in 46s —— View job ✅ No blocking issues — safe to merge. Reviewed the diff (
No correctness, security, or best-practice issues found. |
# Conflicts: # src/lib/actions/products.ts # src/lib/data-connections.ts
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.
What was duplicated
createProduct(src/lib/actions/products.ts) reimplemented inline the same tworules that
canUseDataConnectionFor(src/lib/data-connections.ts) alreadyencoded, as two separate
ifblocks:isAuthorized(session, connection, Actions.UseDataConnection), which is what covers read-only connections and connections gated behind an account flag;Two copies of the same security rule that have to be kept in sync by hand.
Why the duplication existed
canUseDataConnectionForreturns a bare boolean, andcreateProductneeds toreport the two rules as distinct form errors — rule 1 as a form-level message
with empty
fieldErrors, rule 2 as adata_connection_idfield error. A booleancan't carry which rule failed, so the call site open-coded both.
The change
denyDataConnectionFor(session, connection, accountId): DataConnectionDenial | nullinsrc/lib/data-connections.ts, returning"not-usable","wrong-account", ornull. It is now the single definition of the rule.canUseDataConnectionForis kept exported (other callers:src/lib/actions/product-mirrors.ts,src/app/(app)/edit/product/[account_id]/[product_id]/data-connections/page.tsx) and becomes the boolean wrapper:denyDataConnectionFor(...) === null. Its doc comment moved to the new function and the now-stale sentence aboutcreateProductapplying the rules inline was dropped.createProductcallsdenyDataConnectionForonce andswitches on the reason.Messages preserved verbatim. Both strings and the
fieldErrorsshape arebyte-identical to before:
"not-usable"→message: "You are not permitted to use the selected data connection",fieldErrors: {}"wrong-account"→message: "Invalid data connection for this account",fieldErrors.data_connection_id: ["Selected data connection is not available for this account"]Evaluation order is also unchanged:
"not-usable"is checked and reported aheadof
"wrong-account". Theallowed_visibilitiescheck that follows is adifferent, non-duplicated rule and was not touched.
Tests
denyDataConnectionFordescribe block insrc/lib/data-connections.test.ts(same style as the file, realisAuthorizedviajest.requireActual): unowned →null, owned by the account →null, owned by another account →"wrong-account", read-only →"not-usable", flag-gated the caller lacks →"not-usable", plus one asserting"not-usable"wins when both rules fail. The existingcanUseDataConnectionFortests are unchanged and still pass.src/lib/actions/products.test.tsalready had a test per denial path but asserted onlysuccess: false/fieldErrors.data_connection_idbeing defined. Both now assert the exactmessageand the exactfieldErrorscontents, so the strings are pinned against future drift.Verification
npx tsc --noEmit -p tsconfig.typecheck.json— 14 errors, identical before and after this change (all pre-existing, insrc/components/features/analytics/AdminBreakdownChart.tsxandpanels.tsx). No new errors.npx jest --forceExit— 5 failed, 486 passed, 491 total; 3 failed suites, 53 passed, 56 total. That matches theorigin/mainbaseline exactly:DropdownSection.integration.test.tsxplus 4 analytics card tests, all pre-existing and unrelated (jsdom/recharts). No new failures. The touched suites (data-connections.test.ts,actions/products.test.ts,actions/product-mirrors.test.ts) are 71/71 passing.npm run lint— exit 0. Diffed against baseline: output is identical apart from four line numbers shifting by 1 inproducts.tsfrom the added import. No new warnings.🤖 Generated with Claude Code