diff --git a/app/components/timeline_entry/component.rb b/app/components/timeline_entry/component.rb index 77372f18a8..d1dffd50ef 100644 --- a/app/components/timeline_entry/component.rb +++ b/app/components/timeline_entry/component.rb @@ -44,19 +44,9 @@ def description_vars def status_changed_vars { old_status: - render( - StatusTag::Component.new( - timeline_event.old_value, - class_context: "timeline-event", - ), - ).strip, + render(StatusTag::Component.new(timeline_event.old_value)).strip, new_status: - render( - StatusTag::Component.new( - timeline_event.new_value, - class_context: "timeline-event", - ), - ).strip, + render(StatusTag::Component.new(timeline_event.new_value)).strip, } end @@ -71,51 +61,53 @@ def assessor_assigned_vars alias_method :reviewer_assigned_vars, :assessor_assigned_vars def assessment_section_recorded_vars - section = timeline_event.assessment_section - - # to handle old timeline events + assessment_section = timeline_event.assessment_section + + is_most_recent = + timeline_event + .application_form + .timeline_events + .assessment_section_recorded + .order(created_at: :desc) + .find_by(assessment_section:) == timeline_event + + # We need to transform the previous status values that aren't + # suitable for presenting to the user. status = if timeline_event.new_value == "action_required" "rejected" - elsif timeline_event.new_value == "completed" && - timeline_event.is_latest_of_type? - section.status + elsif timeline_event.new_value == "completed" && is_most_recent + assessment_section.status else timeline_event.new_value end + # We can only show failure reasons for the most recent timeline + # event as we pull them direct from the assessment section. selected_failure_reasons = ( - if timeline_event.is_latest_of_type? - section.selected_failure_reasons.to_a + if is_most_recent + assessment_section.selected_failure_reasons.to_a else [] end ) - visible_failure_reasons = - ( + { + section_name: assessment_section.key.titleize, + status:, + visible_failure_reasons: if selected_failure_reasons.count <= 2 selected_failure_reasons else selected_failure_reasons.take(1) - end - ) - - hidden_failure_reasons = - ( + end, + hidden_failure_reasons: if selected_failure_reasons.count <= 2 [] else selected_failure_reasons.drop(1) - end - ) - - { - section_name: section.key.titleize, - status:, - visible_failure_reasons:, - hidden_failure_reasons:, + end, } end @@ -203,19 +195,9 @@ def action_required_by_changed_vars def stage_changed_vars { old_stage: - render( - StatusTag::Component.new( - timeline_event.old_value, - class_context: "timeline-event", - ), - ).strip, + render(StatusTag::Component.new(timeline_event.old_value)).strip, new_stage: - render( - StatusTag::Component.new( - timeline_event.new_value, - class_context: "timeline-event", - ), - ).strip, + render(StatusTag::Component.new(timeline_event.new_value)).strip, } end diff --git a/spec/models/timeline_event_spec.rb b/spec/models/timeline_event_spec.rb index 003965c2d9..6cb61aec54 100644 --- a/spec/models/timeline_event_spec.rb +++ b/spec/models/timeline_event_spec.rb @@ -453,29 +453,4 @@ it { is_expected.to validate_absence_of(:note_text) } end end - - describe "#is_latest_of_type?" do - let(:application_form) { create(:application_form) } - let!(:timeline_event_1) do - create( - :timeline_event, - :assessment_section_recorded, - application_form:, - created_at: Date.new(2020, 1, 1), - ) - end - let!(:timeline_event_2) do - create( - :timeline_event, - :assessment_section_recorded, - application_form:, - created_at: Date.new(2020, 1, 2), - ) - end - - it "returns the correct value" do - expect(timeline_event_1.is_latest_of_type?).to be false - expect(timeline_event_2.is_latest_of_type?).to be true - end - end end