fix(kyc): Dr Green decides verification, not a local flag - #223
Merged
Conversation
checkUserKycStatus short-circuited on the local
consultation_questionnaires.isKycVerified column and returned ACTIVE
without ever calling Dr Green. The persist step below it only ever wrote
`true`, never `false`, so the column was a one-way latch: once set, the
API was never consulted again and the two systems could drift apart
permanently.
Observed in production 2026-07-29: a client saw "You're verified — start
shopping" from that latch while Dr Green production had no such client at
all. Nothing surfaced it until checkout, where the order died with a 500
("Could not retrieve clientCartId... Client record could not be found").
The green badge and the failed order were the same account, minutes apart.
Verification state is answered by the system that owns it, at the moment
it is asked.
- the local flag no longer short-circuits; Dr Green is called every time
- the local row is now a MIRROR written in BOTH directions, so drift
corrects itself. It is kept only because /api/consultation/status and
components/shop/RestrictedRegionGate still read it — it carries no
authority here.
- the orphan-tenant migration path no longer hardcodes isKycVerified:true,
which would have re-created the latch for users whose questionnaire row
lives under another tenant
- REJECTED is no longer an early return before the mirror, so a client who
was verified once and later rejected stops reading as verified elsewhere
- unreachable API still fails CLOSED as status API_ERROR, which the
dashboard already renders distinctly from "not verified"
Tests cover: local true + Dr Green false, false mirrored back, verified
path, REJECTED clearing the flag, and fail-closed on API error.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesKYC source-of-truth synchronization
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Suggested reviewers: ✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
The bug, from today
checkUserKycStatusshort-circuited on BudStacks' ownconsultation_questionnaires.isKycVerifiedcolumn:It returned ACTIVE without ever calling Dr Green. And the persist step below only ever wrote
true, neverfalse— so the column was a one-way latch. Once set, the API was never consulted again and the two systems could drift apart permanently.Production, 2026-07-29: a client saw "You're verified — your account is approved — start shopping" from that latch, while Dr Green production had no such client at all. Nothing surfaced it until checkout, where the order died with a 500:
Same account, minutes apart. The green badge and the hard failure were both "correct" according to the system that produced them.
Change
Verification is answered by the system that owns it, at the moment it is asked.
/api/consultation/statusandcomponents/shop/RestrictedRegionGatestill read it — it carries no authority here.isKycVerified: true, which would have re-created the latch for any user whose questionnaire row lives under a different tenant.REJECTEDis no longer an early return before the mirror — a client verified once and later rejected kept reading as verified on every other surface.API_ERROR, which the dashboard already renders distinctly (rose, different copy) from "not verified". A real customer during an outage is told we can't check right now, not that they're unverified.Expected visible effect
Anyone currently holding a stale
isKycVerified = truewith no matching Dr Green client will flip from verified to unverified on their next dashboard load. That is the correct answer — they could not have completed an order anyway — but it is customer-visible, so it's worth knowing the count first:against how many of those users have a
drGreenClientIdprod still recognises.Tests
tests/unit/kyc-check-source-of-truth.test.ts— localtrue+ Dr Greenfalse(API is called, Dr Green wins);falsemirrored back; the verified path still works;REJECTEDclears the flag; fail-closed on API error.Not in this PR
getClientByIdon the backend throws a bareErrorfor a missing client, so it returns 500 instead of 404. That's why BudStacks can't distinguish "absent" from "broken" and burns a 40-page bidirectional scan on every miss.RestrictedRegionGatereads the mirrored column directly rather than asking Dr Green. Now self-correcting, but still second-hand.Summary by CodeRabbit
Bug Fixes
Tests