Skip to content

Commit

Permalink
Merge pull request #3853 from DFE-Digital/6543-fix-missing-placement-…
Browse files Browse the repository at this point in the history
…entries-in-the-timeline

[6543] Include multiple placement events in timeline
  • Loading branch information
stevehook authored Dec 14, 2023
2 parents 202f170 + a30c0bd commit 332b307
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
17 changes: 15 additions & 2 deletions app/services/trainees/create_timeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,22 @@ def trainee_creation?(audit)

# The audited gem creates multiple audits when multiple associations are
# created e.g. when a user saves more than one disability for a trainee.
# For now, just show one 'create' timeline entry.
# For now, just show one 'create' timeline entry unless the group contains
# placement entries in which case we show all.
def grouped_audits
audits.includes(:user, :auditable).group_by(&:request_uuid).map { |_, audits| audits.first }
audits
.includes(:user, :auditable)
.group_by(&:request_uuid)
.map { |_, audits| first_or_placements(audits) }
.flatten
end

def first_or_placements(audits)
if audits.any? { |audit| audit.auditable_type == Placement.name }
audits
else
audits.first
end
end

def audits
Expand Down
17 changes: 17 additions & 0 deletions spec/services/trainees/create_timeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ module Trainees
expect(subject.count).to eq(4)
end
end

context "when multiple placements have been created in the same request" do
let(:trainee) { create(:trainee) }

before do
create(:placement, trainee:)
create(:placement, trainee:)
trainee.update_column(:submitted_for_trn_at, 1.day.ago)
::Audited::Audit.where(auditable_type: Placement.name).update_all(request_uuid: SecureRandom.uuid)
reload_audits
end

it "returns all the placement events" do
expect(trainee.own_and_associated_audits.where(auditable_type: Placement.name).count).to eq(2)
expect(subject.count).to eq(3)
end
end
end

def update_name
Expand Down

0 comments on commit 332b307

Please sign in to comment.