From bb198000fecff92f8da0f9116798c1c05168bb4a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 21 Nov 2024 11:36:49 +0100 Subject: [PATCH] refactor: include corrections --- .../sva_svr_decision_badge_component.rb | 28 ++++++++----------- app/services/dossier_projection_service.rb | 21 ++------------ .../dossiers/_header_top.html.haml | 2 +- .../instructeurs/procedures/show.html.haml | 6 ++-- .../sva_svr_decision_badge_component_spec.rb | 20 +------------ .../dossier_projection_service_spec.rb | 28 ------------------- 6 files changed, 20 insertions(+), 85 deletions(-) diff --git a/app/components/instructeurs/sva_svr_decision_badge_component.rb b/app/components/instructeurs/sva_svr_decision_badge_component.rb index afc647749..cb8e63081 100644 --- a/app/components/instructeurs/sva_svr_decision_badge_component.rb +++ b/app/components/instructeurs/sva_svr_decision_badge_component.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true class Instructeurs::SVASVRDecisionBadgeComponent < ApplicationComponent - attr_reader :object + attr_reader :dossier attr_reader :procedure attr_reader :with_label - def initialize(projection_or_dossier:, procedure:, with_label: false) - @object = projection_or_dossier + def initialize(dossier:, procedure:, with_label: false) + @dossier = dossier @procedure = procedure @decision = procedure.sva_svr_configuration.decision.to_sym @with_label = with_label @@ -15,11 +15,11 @@ class Instructeurs::SVASVRDecisionBadgeComponent < ApplicationComponent def render? return false unless procedure.sva_svr_enabled? - [:en_construction, :en_instruction].include? object.state.to_sym + [:en_construction, :en_instruction].include? dossier.state.to_sym end def without_date? - object.sva_svr_decision_on.nil? + dossier.sva_svr_decision_on.nil? end def classes @@ -31,17 +31,17 @@ class Instructeurs::SVASVRDecisionBadgeComponent < ApplicationComponent end def soon? - return false if object.sva_svr_decision_on.nil? + return false if dossier.sva_svr_decision_on.nil? - object.sva_svr_decision_on < 7.days.from_now.to_date + dossier.sva_svr_decision_on < 7.days.from_now.to_date end def pending_correction? - object.pending_correction? + dossier.pending_correction? end def days_count - (object.sva_svr_decision_on - Date.current).to_i + (dossier.sva_svr_decision_on - Date.current).to_i end def sva? @@ -66,7 +66,7 @@ class Instructeurs::SVASVRDecisionBadgeComponent < ApplicationComponent elsif pending_correction? t(".dossier_terminated_x_days_after_correction", count: days_count) else - t(".dossier_terminated_on", date: helpers.l(object.sva_svr_decision_on)) + t(".dossier_terminated_on", date: helpers.l(dossier.sva_svr_decision_on)) end end @@ -75,14 +75,10 @@ class Instructeurs::SVASVRDecisionBadgeComponent < ApplicationComponent end def previously_termine? - return if !object.respond_to?(:previously_termine?) - - object.previously_termine? + dossier.previously_termine? end def depose_before_configuration? - return if !object.respond_to?(:sva_svr_decision_triggered_at) - - object.sva_svr_decision_on.nil? && object.sva_svr_decision_triggered_at.nil? + dossier.sva_svr_decision_on.nil? && dossier.sva_svr_decision_triggered_at.nil? end end diff --git a/app/services/dossier_projection_service.rb b/app/services/dossier_projection_service.rb index 8720a0797..b8d424e4f 100644 --- a/app/services/dossier_projection_service.rb +++ b/app/services/dossier_projection_service.rb @@ -1,19 +1,7 @@ # frozen_string_literal: true class DossierProjectionService - class DossierProjection < Struct.new(:dossier, :corrections, :columns) do - def pending_correction? - return false if corrections.blank? - - corrections.any? { _1[:resolved_at].nil? } - end - - def resolved_corrections? - return false if corrections.blank? - - corrections.all? { _1[:resolved_at].present? } - end - end + class DossierProjection < Struct.new(:dossier, :columns) end def self.for_tiers_translation(array) @@ -51,9 +39,7 @@ class DossierProjectionService end champ_value = champ_value_formatter(dossiers_ids, fields) - dossier_corrections = { TABLE => 'dossier_corrections', COLUMN => 'resolved_at' } - - ([dossier_corrections] + fields) + fields .each { |f| f[:id_value_h] = {} } .group_by { |f| f[TABLE] } # one query per table .each do |table, fields| @@ -163,12 +149,11 @@ class DossierProjectionService end end - dossiers = Dossier.find(dossiers_ids) + dossiers = Dossier.includes(:corrections, :pending_corrections).find(dossiers_ids) dossiers_ids.map do |dossier_id| DossierProjection.new( dossiers.find { _1.id == dossier_id }, - dossier_corrections[:id_value_h][dossier_id], fields.map { |f| f[:id_value_h][dossier_id] } ) end diff --git a/app/views/instructeurs/dossiers/_header_top.html.haml b/app/views/instructeurs/dossiers/_header_top.html.haml index f01fc0d4b..583021b7a 100644 --- a/app/views/instructeurs/dossiers/_header_top.html.haml +++ b/app/views/instructeurs/dossiers/_header_top.html.haml @@ -13,7 +13,7 @@ = pending_correction_badge(:for_instructeur) - elsif dossier.en_construction? && dossier.last_correction_resolved? = correction_resolved_badge - = render Instructeurs::SVASVRDecisionBadgeComponent.new(projection_or_dossier: dossier, procedure: dossier.procedure, with_label: true) + = render Instructeurs::SVASVRDecisionBadgeComponent.new(dossier:, procedure: dossier.procedure, with_label: true) .header-actions.fr-ml-auto diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index 6cb301ba6..dd6391901 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -152,16 +152,16 @@ %td - status = [status_badge(dossier.state)] - - if p.pending_correction? + - if dossier.pending_correction? - status << pending_correction_badge(:for_instructeur, html_class: "fr-mt-1v") - - elsif dossier.state.to_sym == :en_construction && p.resolved_corrections? + - elsif dossier.last_correction_resolved? - status << correction_resolved_badge(html_class: "fr-mt-1v") = link_to_if(dossier.hidden_by_administration_at.blank?, safe_join(status), path, class: "flex column") - if @procedure.sva_svr_enabled? %td %span - = link_to_if dossier.hidden_by_administration_at.blank?, render(Instructeurs::SVASVRDecisionBadgeComponent.new(projection_or_dossier: dossier, procedure: @procedure)), path + = link_to_if dossier.hidden_by_administration_at.blank?, render(Instructeurs::SVASVRDecisionBadgeComponent.new(dossier:, procedure: @procedure)), path %td.follow-col %ul.fr-btns-group.fr-btns-group--sm.fr-btns-group--inline.fr-btns-group--icon-right diff --git a/spec/components/instructeurs/sva_svr_decision_badge_component_spec.rb b/spec/components/instructeurs/sva_svr_decision_badge_component_spec.rb index 357ff17ba..3eb3901f0 100644 --- a/spec/components/instructeurs/sva_svr_decision_badge_component_spec.rb +++ b/spec/components/instructeurs/sva_svr_decision_badge_component_spec.rb @@ -10,7 +10,7 @@ RSpec.describe Instructeurs::SVASVRDecisionBadgeComponent, type: :component do context 'with dossier object' do subject do - render_inline(described_class.new(projection_or_dossier: dossier, procedure:, with_label:)) + render_inline(described_class.new(dossier:, procedure:, with_label:)) end let(:title) { subject.at_css("span")["title"] } @@ -54,22 +54,4 @@ RSpec.describe Instructeurs::SVASVRDecisionBadgeComponent, type: :component do it { expect(subject).to have_text("4 j. après correction") } end end - - context 'with projection object' do - subject do - render_inline(described_class.new(projection_or_dossier: projection, procedure:, with_label:)) - end - - context 'dossier en instruction' do - let(:projection) { DossierProjectionService::DossierProjection.new(dossier_id: 12, state: :en_instruction, sva_svr_decision_on: Date.new(2023, 9, 5)) } - - it { expect(subject).to have_text("dans 4 jours") } - end - - context 'dossier without sva decision date' do - let(:projection) { DossierProjectionService::DossierProjection.new(dossier_id: 12, state: :en_instruction) } - - it { expect(subject).to have_text("Instruction manuelle") } - end - end end diff --git a/spec/services/dossier_projection_service_spec.rb b/spec/services/dossier_projection_service_spec.rb index e08743c0c..0cf12fcef 100644 --- a/spec/services/dossier_projection_service_spec.rb +++ b/spec/services/dossier_projection_service_spec.rb @@ -249,34 +249,6 @@ describe DossierProjectionService do it { is_expected.to eq('38') } end - - context 'for dossier corrections table' do - let(:table) { 'dossier_corrections' } - let(:column) { 'resolved_at' } - let(:procedure) { create(:procedure) } - let(:columns) { [Column.new(procedure_id: procedure.id, table:, column:)] } # should somehow be present in column concern - let(:dossier) { create(:dossier, :en_construction, procedure:) } - subject { described_class.project(dossiers_ids, columns)[0] } - - context "when dossier has pending correction" do - before { create(:dossier_correction, dossier:) } - - it { expect(subject.pending_correction?).to be(true) } - it { expect(subject.resolved_corrections?).to eq(false) } - end - - context "when dossier has a resolved correction" do - before { create(:dossier_correction, :resolved, dossier:) } - - it { expect(subject.pending_correction?).to eq(false) } - it { expect(subject.resolved_corrections?).to eq(true) } - end - - context "when dossier has no correction at all" do - it { expect(subject.pending_correction?).to eq(false) } - it { expect(subject.resolved_corrections?).to eq(false) } - end - end end end end