Prevent orphaned CE referrals when deleting enrollments - #6000
Draft
gigxz wants to merge 5 commits into
Draft
Conversation
gigxz
commented
Nov 12, 2025
| private def check_active_outgoing_direct_referrals | ||
| return unless outgoing_ce_referrals.originated_from_direct_send.active.exists? | ||
|
|
||
| raise ActiveRecord::DeleteRestrictionError, 'Cannot delete enrollment because it has active outgoing referrals. Please complete or reject the referrals first.' |
Contributor
Author
There was a problem hiding this comment.
This prevents the deletion, but the descriptive message doesn't make its way to the end-user (yet)
gigxz
marked this pull request as ready for review
November 12, 2025 19:59
gigxz
marked this pull request as draft
November 12, 2025 20:09
This was referenced Nov 12, 2025
gigxz
commented
Nov 17, 2025
| end | ||
|
|
||
| # Nullifies source_enrollment_id on waitlist-based referrals when enrollment is deleted. | ||
| # Waitlist-based referrals can exist without as source enrollment since they're already resolved. |
Contributor
Author
There was a problem hiding this comment.
needs ticket: support changing/adding source enrollment to referral
gigxz
commented
Nov 17, 2025
|
|
||
| # Destroys all direct send referrals when enrollment is deleted. | ||
| # Direct send referrals require the source enrollment to identify which project sent them. | ||
| # Without the source enrollment, they would "float" and not appear on the source project's outgoing referrals list. |
Contributor
Author
There was a problem hiding this comment.
not as compelling, revisit this one.. its more about the target project no?
gigxz
commented
Nov 17, 2025
Comment on lines
+115
to
+117
| before_destroy :check_active_outgoing_direct_referrals | ||
| before_destroy :nullify_outgoing_waitlist_referrals | ||
| before_destroy :destroy_outgoing_direct_referrals |
Contributor
Author
There was a problem hiding this comment.
To do - add handling for destroying target enrollment
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.
Merging this PR
Description
Prevent orphaned CE referrals when deleting enrollments
Problem
When an
Enrollmentwith outgoing CE referrals was destroyed, the referrals were left with asource_enrollment_idpointing to a non-existent enrollment. This caused issues:Solution
Implemented custom
before_destroyhooks that handle referrals based on their origin and status:source_enrollment_idset toNULLsince they can exist without the source enrollment contextAlso added validation requiring
source_enrollment_idfor direct referrals to prevent creating orphaned direct referrals.Alternatives Considered
Considered simpler approaches for the referral relation on Enrollment, I couldn't square any of these:
dependent: :nullifyProblem: Works for waitlist referrals, but direct referrals become orphaned. The sending project loses visibility of the referral on their outgoing referrals list (though it remains visible on the receiving project's incoming referrals list).dependent: :restrict_with_exceptionProblem: Prevents deletion of any enrollment that has ever been tied to a referral, even if the referral is completed. This is too restrictive and prevents necessary cleanup.dependent: :destroyProblem: Might unintentionally delete legitimate referrals in waitlist-based scenarios where referrals should persist even if the source enrollment is removed.Type of change
Bug fix
Checklist before requesting review
//: # NOTE: system tests may fail if there is no branch on the hmis-frontend that matches the Source or Target branch of this PR. This is expected