From 538e24fa7eb3327fbe3c57cb90a48d09ed542d48 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 27 Mar 2023 16:23:11 +0200 Subject: [PATCH] feat(dossier/projection): supports pending_correction? --- app/services/dossier_projection_service.rb | 24 +++++++++++++++++-- .../dossier_projection_service_spec.rb | 23 ++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/app/services/dossier_projection_service.rb b/app/services/dossier_projection_service.rb index 4a32aae39..c7f97a3da 100644 --- a/app/services/dossier_projection_service.rb +++ b/app/services/dossier_projection_service.rb @@ -1,5 +1,11 @@ class DossierProjectionService - class DossierProjection < Struct.new(:dossier_id, :state, :archived, :hidden_by_user_at, :hidden_by_administration_at, :batch_operation_id, :columns) + class DossierProjection < Struct.new(:dossier_id, :state, :archived, :hidden_by_user_at, :hidden_by_administration_at, :batch_operation_id, :resolutions, :columns) do + def pending_correction? + return false if resolutions.blank? + + resolutions.any? { _1[:resolved_at].nil? } + end + end end TABLE = 'table' @@ -23,7 +29,8 @@ class DossierProjectionService batch_operation_field = { TABLE => 'self', COLUMN => 'batch_operation_id' } hidden_by_user_at_field = { TABLE => 'self', COLUMN => 'hidden_by_user_at' } hidden_by_administration_at_field = { TABLE => 'self', COLUMN => 'hidden_by_administration_at' } - ([state_field, archived_field, hidden_by_user_at_field, hidden_by_administration_at_field, batch_operation_field] + fields) # the view needs state and archived dossier attributes + dossier_resolutions = { TABLE => 'dossier_resolutions', COLUMN => 'resolved_at' } + ([state_field, archived_field, hidden_by_user_at_field, hidden_by_administration_at_field, batch_operation_field, dossier_resolutions] + fields) # the view needs state and archived dossier attributes .each { |f| f[:id_value_h] = {} } .group_by { |f| f[TABLE] } # one query per table .each do |table, fields| @@ -76,6 +83,18 @@ class DossierProjectionService .where(id: dossiers_ids) .pluck('dossiers.id, groupe_instructeurs.label') .to_h + when 'dossier_resolutions' + columns = fields.map { _1[COLUMN].to_sym } + + id_value_h = DossierResolution.where(dossier_id: dossiers_ids) + .pluck(:dossier_id, *columns) + .group_by(&:first) # group resolutions by dossier_id + .transform_values do |values| # build each resolution has an hash column => value + values.map { Hash[columns.zip(_1[1..-1])] } + end + + fields[0][:id_value_h] = id_value_h + when 'procedure' Dossier .joins(:procedure) @@ -111,6 +130,7 @@ class DossierProjectionService hidden_by_user_at_field[:id_value_h][dossier_id], hidden_by_administration_at_field[:id_value_h][dossier_id], batch_operation_field[:id_value_h][dossier_id], + dossier_resolutions[:id_value_h][dossier_id], fields.map { |f| f[:id_value_h][dossier_id] } ) end diff --git a/spec/services/dossier_projection_service_spec.rb b/spec/services/dossier_projection_service_spec.rb index ea72f3ba3..cf11a1363 100644 --- a/spec/services/dossier_projection_service_spec.rb +++ b/spec/services/dossier_projection_service_spec.rb @@ -248,6 +248,29 @@ describe DossierProjectionService do it { is_expected.to eq("") } end end + + context 'for dossier reolutions table' do + let(:table) { 'dossier_resolutions' } + let(:column) { 'resolved_at' } + let(:dossier) { create(:dossier, :en_construction) } + subject { described_class.project(dossiers_ids, fields)[0] } + + context "when dossier has pending correction" do + before { create(:dossier_resolution, dossier:) } + + it { expect(subject.pending_correction?).to be(true) } + end + + context "when dossier has a resolved correction" do + before { create(:dossier_resolution, :resolved, dossier:) } + + it { expect(subject.pending_correction?).to eq(false) } + end + + context "when dossier has no correction at all" do + it { expect(subject.pending_correction?).to eq(false) } + end + end end end end