2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-20 19:35:34 +02:00
|
|
|
class DossierPreloader
|
|
|
|
DEFAULT_BATCH_SIZE = 2000
|
|
|
|
|
2024-05-15 10:49:27 +02:00
|
|
|
def initialize(dossiers, includes_for_champ: [], includes_for_etablissement: [])
|
2022-07-20 19:35:34 +02:00
|
|
|
@dossiers = dossiers
|
2023-05-15 17:41:53 +02:00
|
|
|
@includes_for_etablissement = includes_for_etablissement
|
2024-05-15 10:49:27 +02:00
|
|
|
@includes_for_champ = includes_for_champ
|
2022-07-20 19:35:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def in_batches(size = DEFAULT_BATCH_SIZE)
|
|
|
|
dossiers = @dossiers.to_a
|
|
|
|
dossiers.each_slice(size) { |slice| load_dossiers(slice) }
|
|
|
|
dossiers
|
|
|
|
end
|
|
|
|
|
2024-05-17 17:36:40 +02:00
|
|
|
def in_batches_with_block(size = DEFAULT_BATCH_SIZE, &block)
|
|
|
|
@dossiers.in_batches(of: size) do |batch|
|
2024-07-01 15:31:32 +02:00
|
|
|
data = Dossier.where(id: batch.ids).includes(:individual, :traitement, :etablissement, user: :france_connect_informations, avis: :expert, commentaires: [:instructeur, :expert])
|
2024-05-17 17:36:40 +02:00
|
|
|
|
|
|
|
dossiers = data.to_a
|
|
|
|
load_dossiers(dossiers)
|
|
|
|
yield(dossiers)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-10 12:31:22 +02:00
|
|
|
def all(pj_template: false)
|
2022-07-20 21:29:54 +02:00
|
|
|
dossiers = @dossiers.to_a
|
2022-10-10 12:31:22 +02:00
|
|
|
load_dossiers(dossiers, pj_template:)
|
2022-07-20 21:29:54 +02:00
|
|
|
dossiers
|
|
|
|
end
|
|
|
|
|
2022-10-13 15:54:38 +02:00
|
|
|
def self.load_one(dossier, pj_template: false)
|
2024-07-01 15:31:32 +02:00
|
|
|
DossierPreloader.new([dossier]).all(pj_template:).first
|
2022-10-07 18:29:44 +02:00
|
|
|
end
|
|
|
|
|
2022-07-20 19:35:34 +02:00
|
|
|
private
|
|
|
|
|
2024-09-19 11:09:01 +02:00
|
|
|
def revisions
|
2024-07-01 15:31:32 +02:00
|
|
|
@revisions ||= ProcedureRevision.where(id: @dossiers.pluck(:revision_id).uniq)
|
2024-09-19 11:09:01 +02:00
|
|
|
.includes(types_de_champ: { piece_justificative_template_attachment: :blob })
|
2024-07-01 15:31:32 +02:00
|
|
|
.index_by(&:id)
|
|
|
|
end
|
|
|
|
|
2024-09-19 11:09:01 +02:00
|
|
|
# returns: { revision_id : { stable_id : position } }
|
|
|
|
def positions
|
|
|
|
@positions ||= revisions
|
|
|
|
.transform_values { |revision| revision.revision_types_de_champ.map { [_1.stable_id, _1.position] }.to_h }
|
|
|
|
end
|
|
|
|
|
2022-10-10 12:31:22 +02:00
|
|
|
def load_dossiers(dossiers, pj_template: false)
|
2024-05-15 10:49:27 +02:00
|
|
|
to_include = @includes_for_champ.dup
|
2023-05-15 17:41:53 +02:00
|
|
|
to_include << [piece_justificative_file_attachments: :blob]
|
2022-10-10 12:31:22 +02:00
|
|
|
|
2022-07-20 19:35:34 +02:00
|
|
|
all_champs = Champ
|
2022-10-10 12:31:22 +02:00
|
|
|
.includes(to_include)
|
2022-07-20 19:35:34 +02:00
|
|
|
.where(dossier_id: dossiers)
|
|
|
|
.to_a
|
|
|
|
|
2024-09-19 11:09:01 +02:00
|
|
|
children_champs, root_champs = all_champs.partition(&:child?)
|
|
|
|
champs_by_dossier = root_champs.group_by(&:dossier_id)
|
|
|
|
champs_by_dossier_by_parent = children_champs
|
|
|
|
.group_by(&:dossier_id)
|
|
|
|
.transform_values do |champs|
|
|
|
|
champs.group_by(&:parent_id)
|
|
|
|
end
|
2022-07-20 19:35:34 +02:00
|
|
|
|
|
|
|
dossiers.each do |dossier|
|
2024-09-19 11:09:01 +02:00
|
|
|
load_dossier(dossier, champs_by_dossier[dossier.id] || [], champs_by_dossier_by_parent[dossier.id] || {})
|
2022-07-20 19:35:34 +02:00
|
|
|
end
|
2024-07-01 15:31:32 +02:00
|
|
|
|
|
|
|
load_etablissements(all_champs)
|
2022-07-20 19:35:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_etablissements(champs)
|
2023-05-15 17:41:53 +02:00
|
|
|
to_include = @includes_for_etablissement.dup
|
2022-07-20 19:35:34 +02:00
|
|
|
champs_siret = champs.filter(&:siret?)
|
2023-05-15 17:41:53 +02:00
|
|
|
etablissements_by_id = Etablissement.includes(to_include).where(id: champs_siret.map(&:etablissement_id).compact).index_by(&:id)
|
2022-07-20 19:35:34 +02:00
|
|
|
champs_siret.each do |champ|
|
|
|
|
etablissement = etablissements_by_id[champ.etablissement_id]
|
|
|
|
champ.association(:etablissement).target = etablissement
|
|
|
|
if etablissement
|
|
|
|
etablissement.association(:champ).target = champ
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-19 11:09:01 +02:00
|
|
|
def load_dossier(dossier, champs, children_by_parent = {})
|
|
|
|
revision = revisions[dossier.revision_id]
|
2024-07-01 15:31:32 +02:00
|
|
|
if revision.present?
|
|
|
|
dossier.association(:revision).target = revision
|
|
|
|
end
|
|
|
|
|
2024-09-19 11:09:01 +02:00
|
|
|
champs_public, champs_private = champs.partition(&:public?)
|
2024-08-30 16:43:30 +02:00
|
|
|
|
2024-09-19 11:09:01 +02:00
|
|
|
dossier.association(:champs).target = []
|
|
|
|
load_champs(dossier, :champs_public, champs_public, dossier, children_by_parent)
|
|
|
|
load_champs(dossier, :champs_private, champs_private, dossier, children_by_parent)
|
2024-08-30 16:43:30 +02:00
|
|
|
|
2024-08-22 17:40:36 +02:00
|
|
|
# We need to do this because of the check on `Etablissement#champ` in
|
|
|
|
# `Etablissement#libelle_for_export`. By assigning `nil` to `target` we mark association
|
|
|
|
# as loaded and so the check on `Etablissement#champ` will not trigger n+1 query.
|
|
|
|
if dossier.etablissement
|
|
|
|
dossier.etablissement.association(:champ).target = nil
|
2024-08-30 16:43:30 +02:00
|
|
|
end
|
|
|
|
end
|
2024-09-19 11:09:01 +02:00
|
|
|
|
|
|
|
def load_champs(parent, name, champs, dossier, children_by_parent)
|
|
|
|
if champs.empty?
|
|
|
|
parent.association(name).target = [] # tells to Rails association has been loaded
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
champs.each do |champ|
|
|
|
|
champ.association(:dossier).target = dossier
|
|
|
|
|
|
|
|
if parent.is_a?(Champ)
|
|
|
|
champ.association(:parent).target = parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
dossier.association(:champs).target += champs
|
|
|
|
|
|
|
|
parent.association(name).target = champs
|
|
|
|
.filter { positions[dossier.revision_id][_1.stable_id].present? }
|
|
|
|
.sort_by { [_1.row_id, positions[dossier.revision_id][_1.stable_id]] }
|
|
|
|
|
|
|
|
# Load children champs
|
|
|
|
champs.filter(&:block?).each do |parent_champ|
|
|
|
|
champs = children_by_parent[parent_champ.id] || []
|
|
|
|
parent_champ.association(:dossier).target = dossier
|
|
|
|
|
|
|
|
load_champs(parent_champ, :champs, champs, dossier, children_by_parent)
|
|
|
|
end
|
|
|
|
end
|
2022-07-20 19:35:34 +02:00
|
|
|
end
|