Skip to content

fix: re-fetch PatientRoom from DB to fix stale session cache blocking admin discharge#21481

Merged
buddhika75 merged 2 commits into
developmentfrom
21480-fix-admin-discharge-stale-room-cache
Jun 14, 2026
Merged

fix: re-fetch PatientRoom from DB to fix stale session cache blocking admin discharge#21481
buddhika75 merged 2 commits into
developmentfrom
21480-fix-admin-discharge-stale-room-cache

Conversation

@buddhika75

@buddhika75 buddhika75 commented Jun 14, 2026

Copy link
Copy Markdown
Member

Summary

  • Fixes administrative discharge being incorrectly blocked with "Cannot discharge patient: the current room has not been discharged" even when the room is fully discharged in the database
  • Root cause: BhtSummeryController is @SessionScoped, so patientEncounter.currentPatientRoom is a stale Java object loaded at page-open time — after room discharge updates the DB, the session-held reference still has dischargedAt = null
  • Fix: call patientRoomFacade.find(currentRoom.getId()) to re-fetch a fresh PatientRoom from the database before the discharge guard check

Closes #21480

Test plan

  • Admit a patient with a room type that has roomChargesAllowed = true
  • Open the Interim Bill page (loads the session bean)
  • Perform room discharge from the Room Discharge page
  • Return to the Interim Bill page and click Discharge → should succeed without the stale-cache error
  • Verify that clicking Discharge when the room is genuinely NOT yet discharged still shows the correct error

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved patient discharge validation by ensuring the system checks the most up-to-date room status before allowing discharge, reducing cases where outdated room information could block or incorrectly permit the process.

BhtSummeryController is @SessionScoped, so patientEncounter.currentPatientRoom
is a stale in-memory entity loaded at page-open time. After the user performs
room discharge, the DB record is updated but the session-held Java object still
has dischargedAt=null, causing the discharge() guard to block administrative
discharge even when the room is fully discharged in the DB.

Fix: call patientRoomFacade.find() to get a fresh PatientRoom before checking
dischargedAt, bypassing the stale session-scoped reference.

Closes #21480

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1493ed35-5073-4fad-8b82-5e95de233968

📥 Commits

Reviewing files that changed from the base of the PR and between f23e613 and b171dc1.

📒 Files selected for processing (1)
  • src/main/java/com/divudi/bean/inward/BhtSummeryController.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/java/com/divudi/bean/inward/BhtSummeryController.java

Walkthrough

In BhtSummeryController.discharge(), the discharge validation guard is updated to re-fetch the PatientRoom entity from patientRoomFacade by ID before checking dischargedAt. This replaces reading the dischargedAt field directly from the encounter's in-memory currentPatientRoom instance, eliminating stale session-scoped state.

Changes

Discharge Guard DB Re-fetch

Layer / File(s) Summary
Re-fetch PatientRoom before dischargedAt check
src/main/java/com/divudi/bean/inward/BhtSummeryController.java
In discharge(), the current room is loaded fresh via patientRoomFacade.find(currentPatientRoom.getId()) before the dischargedAt == null guard, replacing the stale session-scoped in-memory reference.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related PRs

  • hmislk/hmis#21377: Modifies BhtSummeryController's discharge validation logic including checks around PatientRoom.dischargedAt in discharge() at the same code paths.
  • hmislk/hmis#21305: Also modifies BhtSummeryController.discharge() around PatientRoom.dischargedAt handling — removes logic that overwrote dischargedAt during interim bill discharge.
  • hmislk/hmis#21454: Updates discharge navigation flow that routes discharge PatientRoom/PatientEncounter notifications through BhtSummeryController.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately and concisely describes the main change: re-fetching PatientRoom from the database to fix a stale session cache issue blocking administrative discharge.
Linked Issues check ✅ Passed The code changes directly address the requirements from issue #21480 by re-fetching PatientEncounter from the database to obtain the authoritative currentPatientRoom before validation.
Out of Scope Changes check ✅ Passed All changes in the PR are strictly scoped to fixing the stale session cache issue in BhtSummeryController.discharge(); no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch 21480-fix-admin-discharge-stale-room-cache

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f23e613b08

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/main/java/com/divudi/bean/inward/BhtSummeryController.java Outdated
…m FK

Previous fix re-fetched the PatientRoom by the session-cached FK, but that FK
itself can be stale when a room change happens after the Interim Bill page
loads: RoomChangeController updates PatientEncounter.currentPatientRoom in the
DB, but the session-scoped object still holds the old room reference. Checking
the old room (which is already discharged) lets the guard pass while the new
current room is still active.

Fix: fetch a fresh PatientEncounter via patientEncounterFacade.find() to obtain
the authoritative currentPatientRoom FK from the DB/L2 cache, then check that
room's dischargedAt. This covers both the simple room-discharge-before-admin-
discharge scenario and the room-change-then-admin-discharge scenario.

Addresses Codex review comment on PR #21481.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@buddhika75 buddhika75 merged commit ea9bc0a into development Jun 14, 2026
3 checks passed
@buddhika75 buddhika75 deleted the 21480-fix-admin-discharge-stale-room-cache branch June 14, 2026 11:12
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.

fix: Administrative discharge blocked by stale session cache — room already discharged in DB

1 participant