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-20 14:54:26 +02:00
|
|
|
def revisions(pj_template: false)
|
2024-07-01 15:31:32 +02:00
|
|
|
@revisions ||= ProcedureRevision.where(id: @dossiers.pluck(:revision_id).uniq)
|
2024-10-11 12:20:24 +02:00
|
|
|
.includes(procedure: [], revision_types_de_champ: { parent: :type_de_champ }, types_de_champ_public: [], types_de_champ_private: [], types_de_champ: pj_template ? { piece_justificative_template_attachment: :blob } : [])
|
2024-07-01 15:31:32 +02:00
|
|
|
.index_by(&:id)
|
|
|
|
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-20 14:54:26 +02:00
|
|
|
champs_by_dossier = all_champs.group_by(&:dossier_id)
|
2022-07-20 19:35:34 +02:00
|
|
|
|
|
|
|
dossiers.each do |dossier|
|
2024-09-20 14:54:26 +02:00
|
|
|
load_dossier(dossier, champs_by_dossier[dossier.id] || [], pj_template:)
|
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
|
2024-10-18 18:17:17 +02:00
|
|
|
# `champs.siret?` will delegate to type_de_champ; this is not what we want here
|
|
|
|
champs_siret = champs.filter { _1.type == 'Champs::SiretChamp' }
|
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-20 14:54:26 +02:00
|
|
|
def load_dossier(dossier, champs, pj_template: false)
|
|
|
|
revision = revisions(pj_template:)[dossier.revision_id]
|
2024-07-01 15:31:32 +02:00
|
|
|
if revision.present?
|
|
|
|
dossier.association(:revision).target = revision
|
|
|
|
end
|
2024-09-20 14:54:26 +02:00
|
|
|
dossier.association(:champs).target = champs
|
2024-07-01 15:31:32 +02:00
|
|
|
|
2024-09-19 11:09:01 +02:00
|
|
|
champs.each do |champ|
|
|
|
|
champ.association(:dossier).target = dossier
|
|
|
|
end
|
|
|
|
|
2024-09-20 14:54:26 +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-09-19 11:09:01 +02:00
|
|
|
end
|
2024-11-06 22:30:31 +01:00
|
|
|
|
|
|
|
dossier.send(:reset_champs_cache)
|
2024-09-19 11:09:01 +02:00
|
|
|
end
|
2022-07-20 19:35:34 +02:00
|
|
|
end
|