Skip to content

Commit 96808dd

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 25ad0b5 commit 96808dd

5 files changed

Lines changed: 78 additions & 1 deletion

File tree

app/components/app_activity_log_component.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ def initialize(team:, patient:, programme_type: nil, session: nil)
9393
.includes(:created_by, :patient, :session)
9494
.then { |scope| session ? scope.where(session:) : scope }
9595

96+
@parent_relationship_audits =
97+
@patient
98+
.associated_audits
99+
.destroys
100+
.where(auditable_type: "ParentRelationship")
101+
.includes(:user)
102+
96103
@notify_log_entries =
97104
@patient
98105
.notify_log_entries
@@ -173,6 +180,7 @@ def initialize(team:, patient:, programme_type: nil, session: nil)
173180
:consents,
174181
:gillick_assessments,
175182
:notes,
183+
:parent_relationship_audits,
176184
:notify_log_entries,
177185
:patient,
178186
:patient_locations,
@@ -192,6 +200,7 @@ def all_events
192200
gillick_assessment_events,
193201
note_events,
194202
notify_events,
203+
parent_relationship_events,
195204
patient_merge_events,
196205
patient_specific_direction_events,
197206
pre_screening_events,
@@ -403,6 +412,17 @@ def notify_events
403412
end
404413
end
405414

415+
def parent_relationship_events
416+
parent_relationship_audits.map do |audit|
417+
{
418+
title: "Parent relationship removed",
419+
body: audit.comment,
420+
at: audit.created_at,
421+
by: audit.user
422+
}
423+
end
424+
end
425+
406426
def patient_merge_events
407427
patient_merge_log_entries.map do |patient_merge_log_entry|
408428
{

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,21 @@
806806
by: "JOY, Nurse"
807807
end
808808

809+
describe "parent relationship removal events" do
810+
before do
811+
relationship =
812+
patient.parent_relationships.includes(:parent).find_by!(parent: mum)
813+
814+
Audited.audit_class.as_user(user) { relationship.destroy! }
815+
end
816+
817+
include_examples "card",
818+
title: "Parent relationship removed",
819+
date: "1 January 2026 at 12:00am",
820+
notes: "Jane Doe (mum) removed from child record",
821+
by: "JOY, Nurse"
822+
end
823+
809824
describe "decision expiration events" do
810825
let(:hpv_programme) { Programme.hpv }
811826
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)