Skip to content

Commit 25c09c8

Browse files
committed
Fix missing child activity log entries when parent relationships are removed
This change fixes a gap in audit visibility when parent-child relationships are removed from a child record. Previously, removing a parent relationship through either the direct child record flow or the bulk import cleanup flow deleted the relationship successfully, but did not surface a meaningful activity log entry on the child record. Jira-Issue: MAV-2789
1 parent b499ce6 commit 25c09c8

5 files changed

Lines changed: 82 additions & 1 deletion

File tree

app/components/app_activity_log_component.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def all_events
5757
gillick_assessment_events,
5858
note_events,
5959
notify_events,
60+
parent_relationship_events,
6061
patient_merge_events,
6162
patient_specific_direction_events,
6263
pre_screening_events,
@@ -270,6 +271,17 @@ def notify_events
270271
end
271272
end
272273

274+
def parent_relationship_events
275+
parent_relationship_audits.map do |audit|
276+
{
277+
title: "Parent relationship removed",
278+
body: audit.comment,
279+
at: audit.created_at,
280+
by: audit.user
281+
}
282+
end
283+
end
284+
273285
def patient_merge_events
274286
patient_merge_log_entries.map do |patient_merge_log_entry|
275287
{
@@ -536,6 +548,16 @@ def notify_log_entries
536548
end
537549
end
538550

551+
def parent_relationship_audits
552+
return [] if include_programme_specific_events?
553+
554+
patient
555+
.associated_audits
556+
.destroys
557+
.where(auditable_type: "ParentRelationship")
558+
.includes(:user)
559+
end
560+
539561
def patient_locations
540562
return [] unless include_programme_specific_events?
541563

app/jobs/bulk_remove_parent_relationships_job.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ def perform(
4545
CohortImportsParentRelationship.where(
4646
parent_relationship_id: parent_relationship_ids
4747
).delete_all
48-
parent_relationships_to_remove.each(&:destroy!)
48+
49+
Audited
50+
.audit_class
51+
.as_user(user) { parent_relationships_to_remove.each(&:destroy!) }
4952

5053
Parent
5154
.where(id: parent_ids_to_check)

app/models/parent_relationship.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class ParentRelationship < ApplicationRecord
5252
accepts_nested_attributes_for :parent
5353
validates_associated :parent
5454

55+
before_destroy :set_removal_audit_comment, prepend: true
56+
5557
def label
5658
other? ? "Other – #{other_name}" : human_enum_name(:type).capitalize
5759
end
@@ -71,4 +73,10 @@ def ordinal_label
7173
"#{index.ordinalize} parent or guardian"
7274
end
7375
end
76+
77+
private
78+
79+
def set_removal_audit_comment
80+
self.audit_comment = "#{label_with_parent} removed from child record"
81+
end
7482
end

spec/components/app_activity_log_component_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,23 @@
810810
by: "JOY, Nurse"
811811
end
812812

813+
describe "parent relationship removal events" do
814+
let(:component) { described_class.new(patient:, team:) }
815+
816+
before do
817+
relationship =
818+
patient.parent_relationships.includes(:parent).find_by!(parent: mum)
819+
820+
Audited.audit_class.as_user(user) { relationship.destroy! }
821+
end
822+
823+
include_examples "card",
824+
title: "Parent relationship removed",
825+
date: "1 January 2026 at 12:00am",
826+
notes: "Jane Doe (mum) removed from child record",
827+
by: "JOY, Nurse"
828+
end
829+
813830
describe "decision expiration events" do
814831
let(:hpv_programme) { Programme.hpv }
815832
let(:flu_programme) { Programme.flu }

spec/models/parent_relationship_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,35 @@
8484
it { should eq("Unknown") }
8585
end
8686
end
87+
88+
context "removal is audited" do
89+
subject(:destroy!) do
90+
Audited.audit_class.as_user(user) { relationship.destroy! }
91+
end
92+
93+
let(:user) { create(:user) }
94+
let(:patient) { create(:patient) }
95+
let(:parent) { create(:parent, full_name: "Jane Doe") }
96+
let!(:relationship) do
97+
create(:parent_relationship, :mother, patient:, parent:)
98+
end
99+
100+
it "creates a destroy audit with the patient association and comment" do
101+
expect { destroy! }.to change { relationship.audits.destroys.count }.by(1)
102+
103+
audit =
104+
Audited
105+
.audit_class
106+
.auditable_finder(relationship.id, "ParentRelationship")
107+
.destroys
108+
.last
109+
110+
expect(audit).to have_attributes(
111+
action: "destroy",
112+
associated: patient,
113+
user:,
114+
comment: "Jane Doe (mum) removed from child record"
115+
)
116+
end
117+
end
87118
end

0 commit comments

Comments
 (0)