faster pjs_for_dossier

This commit is contained in:
simon lehericey 2022-04-04 14:40:49 +02:00
parent 62e0553a4e
commit f2fea1f882

View file

@ -4,7 +4,7 @@ class PiecesJustificativesService
pjs_commentaires = pjs_for_commentaires(dossier)
pjs_dossier = pjs_for_dossier(dossier, for_expert)
pjs_champs + pjs_commentaires + pjs_dossier.filter(&:attached?)
pjs_champs + pjs_commentaires + pjs_dossier
end
def self.liste_pieces_justificatives(dossier)
@ -138,12 +138,9 @@ class PiecesJustificativesService
end
def self.pjs_for_dossier(dossier, for_expert = false)
pjs = [
dossier.justificatif_motivation,
dossier.attestation&.pdf,
dossier.etablissement&.entreprise_attestation_sociale,
dossier.etablissement&.entreprise_attestation_fiscale
].flatten.compact
pjs = motivation(dossier) +
attestation(dossier) +
etablissement(dossier)
if !for_expert
pjs += operation_logs_and_signatures(dossier)
@ -152,6 +149,30 @@ class PiecesJustificativesService
pjs
end
def self.etablissement(dossier)
etablissement = Etablissement.where(dossier: dossier)
ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "Etablissement", record_id: etablissement)
end
def self.motivation(dossier)
ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "Dossier", name: "justificatif_motivation", record_id: dossier)
end
def self.attestation(dossier)
attestation = Attestation
.joins(:pdf_attachment)
.where(dossier: dossier)
ActiveStorage::Attachment
.includes(:blob)
.where(record_type: "Attestation", record_id: attestation)
end
def self.operation_logs_and_signatures(dossier)
dol_ids_bill_id = DossierOperationLog
.where(dossier: dossier)