Skip to content

fix(clinical): detect duplicate schedules by SQL error number#229

Open
rlorenzo wants to merge 1 commit into
mainfrom
fix/clinical-duplicate-key-detection
Open

fix(clinical): detect duplicate schedules by SQL error number#229
rlorenzo wants to merge 1 commit into
mainfrom
fix/clinical-duplicate-key-detection

Conversation

@rlorenzo

Copy link
Copy Markdown
Contributor

What

In ScheduleEditService.AddInstructorAsync, the post-SaveChanges catch 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.

private static bool IsDuplicateKeyViolation(Exception ex)
{
    var sqlEx = ex as SqlException ?? ex.InnerException as SqlException;
    return sqlEx is not null
        && sqlEx.Errors.Cast<SqlError>().Any(e => e.Number is 2627 or 2601);
}

Why

The old check matched "duplicate" / "unique" / "constraint" / "violation of primary key" against message.ToLower(). Two problems:

  1. Latent bug: other constraint failures — foreign-key or check-constraint violations (error 547, whose message contains "constraint") — were mis-reported as "already scheduled." Now only true duplicate/unique violations get that message; everything else falls through to the generic "database operation failed" message.
  2. Locale fragility: 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

  • Behavior-preserving for the real duplicate case; only re-routes non-duplicate constraint errors to the correct (generic) message.
  • The duplicate pre-check (before SaveChanges) is unchanged; AddInstructorAsync_WithScheduleConflicts and all 2084 tests pass.
  • Supersedes the message-matching helper currently on Development via refactor(codeql): decompose complex conditions + harden Vite web-root check #227; the overlap will be resolved (keeping this version) at the Development merge.

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().
@codecov-commenter

Copy link
Copy Markdown

Bundle Report

Bundle size has no change ✅

@codecov-commenter

codecov-commenter commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 44.48%. Comparing base (f6dbe72) to head (384a05b).

Files with missing lines Patch % Lines
.../ClinicalScheduler/Services/ScheduleEditService.cs 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #229      +/-   ##
==========================================
- Coverage   44.49%   44.48%   -0.01%     
==========================================
  Files         895      895              
  Lines       51655    51654       -1     
  Branches     4812     4813       +1     
==========================================
- Hits        22983    22980       -3     
- Misses      28108    28110       +2     
  Partials      564      564              
Flag Coverage Δ
backend 44.64% <0.00%> (-0.01%) ⬇️
frontend 41.32% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 check SqlException.Errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants