feat(dossier/projection): supports pending_correction?

This commit is contained in:
Colin Darie 2023-03-27 16:23:11 +02:00
parent 7a9917fb32
commit 538e24fa7e
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 45 additions and 2 deletions

View file

@ -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

View file

@ -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