Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/components/app_activity_log_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def all_events
gillick_assessment_events,
note_events,
notify_events,
parent_relationship_events,
patient_merge_events,
patient_specific_direction_events,
pre_screening_events,
Expand Down Expand Up @@ -270,6 +271,17 @@ def notify_events
end
end

def parent_relationship_events
parent_relationship_audits.map do |audit|
{
title: "Parent relationship removed",
body: audit.comment,
at: audit.created_at,
by: audit.user
}
end
end

def patient_merge_events
patient_merge_log_entries.map do |patient_merge_log_entry|
{
Expand Down Expand Up @@ -536,6 +548,16 @@ def notify_log_entries
end
end

def parent_relationship_audits
return [] if include_programme_specific_events?

patient
.associated_audits
.destroys
.where(auditable_type: "ParentRelationship")
.includes(:user)
end

def patient_locations
return [] unless include_programme_specific_events?

Expand Down
5 changes: 4 additions & 1 deletion app/jobs/bulk_remove_parent_relationships_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def perform(
CohortImportsParentRelationship.where(
parent_relationship_id: parent_relationship_ids
).delete_all
parent_relationships_to_remove.each(&:destroy!)

Audited
.audit_class
.as_user(user) { parent_relationships_to_remove.each(&:destroy!) }

Parent
.where(id: parent_ids_to_check)
Expand Down
8 changes: 8 additions & 0 deletions app/models/parent_relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class ParentRelationship < ApplicationRecord
accepts_nested_attributes_for :parent
validates_associated :parent

before_destroy :set_removal_audit_comment, prepend: true

def label
other? ? "Other – #{other_name}" : human_enum_name(:type).capitalize
end
Expand All @@ -71,4 +73,10 @@ def ordinal_label
"#{index.ordinalize} parent or guardian"
end
end

private

def set_removal_audit_comment
self.audit_comment = "#{label_with_parent} removed from child record"
end
end
17 changes: 17 additions & 0 deletions spec/components/app_activity_log_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,23 @@
by: "JOY, Nurse"
end

describe "parent relationship removal events" do
let(:component) { described_class.new(patient:, team:) }

before do
relationship =
patient.parent_relationships.includes(:parent).find_by!(parent: mum)

Audited.audit_class.as_user(user) { relationship.destroy! }
end

include_examples "card",
title: "Parent relationship removed",
date: "1 January 2026 at 12:00am",
notes: "Jane Doe (mum) removed from child record",
by: "JOY, Nurse"
end

describe "decision expiration events" do
let(:hpv_programme) { Programme.hpv }
let(:flu_programme) { Programme.flu }
Expand Down
31 changes: 31 additions & 0 deletions spec/models/parent_relationship_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,35 @@
it { should eq("Unknown") }
end
end

context "removal is audited" do
subject(:destroy!) do
Audited.audit_class.as_user(user) { relationship.destroy! }
end

let(:user) { create(:user) }
let(:patient) { create(:patient) }
let(:parent) { create(:parent, full_name: "Jane Doe") }
let!(:relationship) do
create(:parent_relationship, :mother, patient:, parent:)
end

it "creates a destroy audit with the patient association and comment" do
expect { destroy! }.to change { relationship.audits.destroys.count }.by(1)

audit =
Audited
.audit_class
.auditable_finder(relationship.id, "ParentRelationship")
.destroys
.last

expect(audit).to have_attributes(
action: "destroy",
associated: patient,
user:,
comment: "Jane Doe (mum) removed from child record"
)
end
end
end
Loading