fix(clinical): detect duplicate schedules by SQL error number#229
fix(clinical): detect duplicate schedules by SQL error number#229rlorenzo wants to merge 1 commit into
Conversation
Bundle ReportBundle size has no change ✅ |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #229 +/- ##
==========================================
+ Coverage 44.51% 44.53% +0.01%
==========================================
Files 895 895
Lines 51760 51759 -1
Branches 4827 4828 +1
==========================================
+ Hits 23041 23050 +9
+ Misses 28149 28138 -11
- Partials 570 571 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
There was a problem hiding this comment.
Pull request overview
Refines error handling in the Clinical Scheduler’s ScheduleEditService.AddInstructorAsync so that “already scheduled” is only returned for true SQL Server duplicate/unique key violations, using stable SQL error numbers instead of locale-dependent substring matching on exception messages.
Changes:
- Replaces exception message substring matching with SQL Server error-number detection (2627, 2601) to identify duplicate-key violations.
- Adds a focused helper (
IsDuplicateKeyViolation) to unwrap EF/SQL exceptions and checkSqlException.Errors.
384a05b to
06ab238
Compare
|
@coderabbitai full review |
✅ Action performedFull review finished. |
📝 WalkthroughWalkthrough
ChangesDuplicate-key error classification
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@web/Areas/ClinicalScheduler/Services/ScheduleEditService.cs`:
- Around line 250-252: Add targeted tests for the exception classification logic
in IsDuplicateKeyViolation within ScheduleEditService so both branches are
covered. Create tests that feed the service a duplicate-key SqlException with
error numbers 2627 and 2601 and verify the duplicate-schedule path, plus a
non-duplicate SqlException/DbUpdateException that verifies the generic
database-failure path. Make sure the tests assert the downstream API error
mapping contract stays distinct for these two cases.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 109ef738-ea05-4f15-b163-60f3a498908e
📒 Files selected for processing (1)
web/Areas/ClinicalScheduler/Services/ScheduleEditService.cs
Replace locale-fragile message-substring matching with SQL Server error numbers 2627/2601 (unique constraint / duplicate key in a unique index) to decide whether a save failure means the instructor is already scheduled. Other database errors (foreign-key, check constraints) now fall through to the generic message instead of mis-reporting "already scheduled", and the check no longer depends on English error text or a culture-specific ToLower().
06ab238 to
b9765ea
Compare
| public override int SaveChanges() | ||
| => SaveException is not null ? throw SaveException : base.SaveChanges(); | ||
|
|
||
| public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) | ||
| => SaveException is not null ? Task.FromException<int>(SaveException) : base.SaveChangesAsync(cancellationToken); |
What
In
ScheduleEditService.AddInstructorAsync, the post-SaveChangescatch decides whether a DB failure means "instructor already scheduled" by SQL Server error number (2627 unique-constraint, 2601 duplicate-key-in-unique-index) instead of substring-matching the exception message.Why
The old check matched
"duplicate" / "unique" / "constraint" / "violation of primary key"againstmessage.ToLower(). Two problems:ToLower()plus English error-text matching is culture-sensitive (Turkish-I) and breaks if SQL messages are localized. Error numbers are stable and locale-invariant.Notes
AddInstructorAsync_WithScheduleConflictsand all 2084 tests pass.Developmentvia refactor(codeql): decompose complex conditions + harden Vite web-root check #227; the overlap will be resolved (keeping this version) at the Development merge.