Skip to content

Commit cb06ac9

Browse files
Remove destroy hooks on Patient
The `PatientDeleter` service class has been introduced as the designated mechanism for deleting patients. This class handles all the objects related to `Patient`, making these hooks redundant. Jira-Issue: MAV-7067
1 parent 0b207da commit cb06ac9

2 files changed

Lines changed: 1 addition & 44 deletions

File tree

app/models/patient.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Patient < ApplicationRecord
7777
has_many :consent_notifications
7878
has_many :consents
7979
has_many :gillick_assessments
80-
has_many :important_notices, dependent: :destroy
80+
has_many :important_notices
8181
has_many :notes
8282
has_many :notify_log_entries
8383
has_many :parent_relationships, -> { order(:created_at) }
@@ -498,8 +498,6 @@ class Patient < ApplicationRecord
498498
after_update :sync_vaccinations_to_nhs_immunisations_api
499499
after_commit :generate_important_notice_if_needed, on: :update
500500
after_commit :search_vaccinations_from_nhs_immunisations_api, on: :update
501-
before_destroy :destroy_childless_parents
502-
503501
delegate :fhir_record, to: :fhir_mapper
504502

505503
def sessions
@@ -824,17 +822,6 @@ def locations_are_correct_type
824822
end
825823
end
826824

827-
def destroy_childless_parents
828-
parents_to_check = parents.to_a # Store parents before destroying relationships
829-
830-
# Manually destroy the parent_relationships associated with this Child
831-
parent_relationships.each(&:destroy)
832-
833-
parents_to_check.each do |parent|
834-
parent.destroy! if parent.parent_relationships.count.zero?
835-
end
836-
end
837-
838825
def archive_due_to_deceased!
839826
archive_reasons =
840827
teams.map do |team|

spec/models/patient_spec.rb

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,36 +1446,6 @@
14461446
end
14471447
end
14481448

1449-
describe "#destroy_childless_parents" do
1450-
context "when parent has only one child" do
1451-
let(:parent) { create(:parent) }
1452-
let!(:patient) { create(:patient, parents: [parent]) }
1453-
1454-
it "destroys the parent when the patient is destroyed" do
1455-
expect { patient.destroy }.to change(Parent, :count).by(-1)
1456-
end
1457-
end
1458-
1459-
context "when parent has multiple children" do
1460-
let(:parent) { create(:parent) }
1461-
let!(:patient) { create(:patient, parents: [parent]) }
1462-
1463-
before { create(:patient, parents: [parent]) }
1464-
1465-
it "does not destroy the parent when one patient is destroyed" do
1466-
expect { patient.destroy }.not_to change(Parent, :count)
1467-
end
1468-
end
1469-
1470-
context "when patient has multiple parents" do
1471-
let!(:patient) { create(:patient, parents: create_list(:parent, 2)) }
1472-
1473-
it "destroys only the childless parents" do
1474-
expect { patient.destroy }.to change(Parent, :count).by(-2)
1475-
end
1476-
end
1477-
end
1478-
14791449
describe "#latest_pds_search_result" do
14801450
subject(:latest_pds_search_result) { patient.latest_pds_search_result }
14811451

0 commit comments

Comments
 (0)