Skip to content

Commit

Permalink
Merge pull request demarches-simplifiees#9960 from mfo/US/refactor-li…
Browse files Browse the repository at this point in the history
…ste_documents

tech(PiecesJustificativesService): amélioration du services qui liste les fichiers pour les exports/dossiers
  • Loading branch information
mfo authored Feb 16, 2024
2 parents 4dfb3b8 + 2200310 commit 0b3a0d5
Show file tree
Hide file tree
Showing 41 changed files with 421 additions and 324 deletions.
2 changes: 1 addition & 1 deletion app/controllers/administrateurs/archives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def index

def create
type = params[:type]
archive = Archive.find_or_create_archive(type, year_month, all_groupe_instructeurs)
archive = Archive.find_or_create_archive(type, year_month, all_groupe_instructeurs, current_administrateur)
if archive.pending?
ArchiveCreationJob.perform_later(@procedure, archive, current_administrateur)
flash[:notice] = "Votre demande a été prise en compte. Selon le nombre de dossiers, cela peut prendre de quelques minutes à plusieurs heures. Vous recevrez un courriel lorsque le fichier sera disponible."
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/administrateurs/exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ExportsController < AdministrateurController
before_action :ensure_not_super_admin!

def download
export = Export.find_or_create_fresh_export(export_format, all_groupe_instructeurs, current_administrateur.instructeur, **export_options)
export = Export.find_or_create_fresh_export(export_format, all_groupe_instructeurs, current_administrateur, **export_options)
@dossiers_count = export.count

if export.available?
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/experts/avis_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ def bilans_bdf
end

def telecharger_pjs
@dossier = dossier_with_champs

files = ActiveStorage::DownloadableFile.create_list_from_dossiers(Dossier.where(id: @dossier.id), include_avis_for_expert: current_expert)
files = ActiveStorage::DownloadableFile.create_list_from_dossiers(user_profile: current_expert, dossiers: Dossier.where(id: @dossier.id))
cleaned_files = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(files)

zipline(cleaned_files, "dossier-#{@dossier.id}.zip")
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/instructeurs/archives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def index

def create
type = params[:type]
archive = Archive.find_or_create_archive(type, year_month, groupe_instructeurs)
archive = Archive.find_or_create_archive(type, year_month, groupe_instructeurs, current_instructeur)
if archive.pending?
ArchiveCreationJob.perform_later(@procedure, archive, current_instructeur)
flash[:notice] = "Votre demande a été prise en compte. Selon le nombre de dossiers, cela peut prendre de quelques minutes à plusieurs heures. Vous recevrez un courriel lorsque le fichier sera disponible."
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/instructeurs/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ def bilans_bdf
def show
@demande_seen_at = current_instructeur.follows.find_by(dossier: dossier_with_champs)&.demande_seen_at
@is_dossier_in_batch_operation = dossier.batch_operation.present?

respond_to do |format|
format.pdf do
@include_infos_administration = true
@acls = PiecesJustificativesService.new(user_profile: current_instructeur).acl_for_dossier_export
render(template: 'dossiers/show', formats: [:pdf])
end
format.all
Expand Down Expand Up @@ -322,7 +321,7 @@ def annotation
end

def telecharger_pjs
files = ActiveStorage::DownloadableFile.create_list_from_dossiers(Dossier.where(id: dossier.id), with_champs_private: true, include_infos_administration: true)
files = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers: Dossier.where(id: dossier.id), user_profile: current_instructeur)
cleaned_files = ActiveStorage::DownloadableFile.cleanup_list_from_dossier(files)

zipline(cleaned_files, "dossier-#{dossier.id}.zip")
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/users/dossiers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ def index
end

def show
pj_service = PiecesJustificativesService.new(user_profile: current_user)
respond_to do |format|
format.pdf do
@dossier = dossier_with_champs(pj_template: false)
@include_infos_administration = false
@acls = pj_service.acl_for_dossier_export
render(template: 'dossiers/show', formats: [:pdf])
end
format.all do
Expand Down
13 changes: 4 additions & 9 deletions app/lib/active_storage/downloadable_file.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
require 'fog/openstack'

class ActiveStorage::DownloadableFile
def self.create_list_from_dossiers(
dossiers,
with_bills: false,
with_champs_private: false,
include_infos_administration: false,
include_avis_for_expert: false
)
PiecesJustificativesService.generate_dossier_export(dossiers, include_infos_administration:, include_avis_for_expert:) +
PiecesJustificativesService.liste_documents(dossiers, with_bills:, with_champs_private:, with_avis_piece_justificative: include_infos_administration)
def self.create_list_from_dossiers(dossiers:, user_profile:)
pj_service = PiecesJustificativesService.new(user_profile:)

pj_service.generate_dossiers_export(dossiers) + pj_service.liste_documents(dossiers)
end

def self.cleanup_list_from_dossier(files)
Expand Down
3 changes: 2 additions & 1 deletion app/models/administrateur.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class Administrateur < ApplicationRecord
has_many :api_tokens, inverse_of: :administrateur, dependent: :destroy
has_many :commentaire_groupe_gestionnaires, as: :sender
has_and_belongs_to_many :default_zones, class_name: 'Zone', join_table: 'default_zones_administrateurs'

has_many :archives, as: :user_profile
has_many :exports, as: :user_profile
belongs_to :user
belongs_to :groupe_gestionnaire, optional: true

Expand Down
5 changes: 3 additions & 2 deletions app/models/archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Archive < ApplicationRecord
has_and_belongs_to_many :groupe_instructeurs

has_one_attached :file
belongs_to :user_profile, polymorphic: true, optional: true

scope :for_groupe_instructeur, -> (groupe_instructeur) {
joins(:archives_groupe_instructeurs)
Expand All @@ -30,9 +31,9 @@ def filename(procedure)
end
end

def self.find_or_create_archive(time_span_type, month, groupe_instructeurs)
def self.find_or_create_archive(time_span_type, month, groupe_instructeurs, user_profile)
create_with(groupe_instructeurs: groupe_instructeurs)
.create_or_find_by(time_span_type: time_span_type, month: month, key: generate_cache_key(groupe_instructeurs))
.create_or_find_by(time_span_type:, month:, user_profile:, key: generate_cache_key(groupe_instructeurs))
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/models/attestation_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def unspecified_champs_for_dossier(dossier)

def dup
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
PiecesJustificativesService.clone_attachments(self, attestation_template)
ClonePiecesJustificativesService.clone_attachments(self, attestation_template)
attestation_template
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def clone(fork = false)
relationships = fork || !private? ? [:etablissement, :geo_areas] : []

deep_clone(only: champ_attributes + value_attributes, include: relationships, validate: !fork) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/dossier_clone_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def clone(user: nil, fork: false)
.transform_values { [_1, _1.clone(fork)] }

cloned_dossier = deep_clone(only: dossier_attributes, include: relationships) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)

if original.is_a?(Dossier)
if fork
Expand Down
11 changes: 0 additions & 11 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -740,17 +740,6 @@ def duree_totale_conservation_in_months
procedure.duree_conservation_dossiers_dans_ds + (conservation_extension / 1.month.to_i)
end

def avis_for_instructeur(instructeur)
if instructeur.dossiers.include?(self)
avis.order(created_at: :asc)
else
avis
.where(confidentiel: false)
.or(avis.where(claimant: instructeur))
.order(created_at: :asc)
end
end

def avis_for_expert(expert)
Avis
.where(dossier_id: id, confidentiel: false)
Expand Down
7 changes: 4 additions & 3 deletions app/models/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Export < ApplicationRecord
has_and_belongs_to_many :groupe_instructeurs
belongs_to :procedure_presentation, optional: true
belongs_to :instructeur, optional: true
belongs_to :user_profile, polymorphic: true, optional: true

has_one_attached :file

Expand Down Expand Up @@ -65,7 +66,7 @@ def filtered?
procedure_presentation_id.present?
end

def self.find_or_create_fresh_export(format, groupe_instructeurs, instructeur, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil)
def self.find_or_create_fresh_export(format, groupe_instructeurs, user_profile, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil)
attributes = {
format:,
time_span_type:,
Expand All @@ -81,7 +82,7 @@ def self.find_or_create_fresh_export(format, groupe_instructeurs, instructeur, t
return recent_export if recent_export.present?

create!(**attributes, groupe_instructeurs:,
instructeur:,
user_profile:,
procedure_presentation:,
procedure_presentation_snapshot: procedure_presentation&.snapshot)
end
Expand Down Expand Up @@ -146,7 +147,7 @@ def dossiers_for_export
end

def blob
service = ProcedureExportService.new(procedure, dossiers_for_export)
service = ProcedureExportService.new(procedure, dossiers_for_export, user_profile)

case format.to_sym
when :csv
Expand Down
3 changes: 2 additions & 1 deletion app/models/instructeur.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class Instructeur < ApplicationRecord
has_many :followed_dossiers, through: :follows, source: :dossier
has_many :previously_followed_dossiers, -> { distinct }, through: :previous_follows, source: :dossier
has_many :trusted_device_tokens, dependent: :destroy
has_many :archives
has_many :bulk_messages, dependent: :destroy
has_many :exports, as: :user_profile
has_many :archives, as: :user_profile

belongs_to :user

Expand Down
2 changes: 1 addition & 1 deletion app/models/procedure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def clone(admin, from_library)
}
include_list[:groupe_instructeurs] = [:instructeurs, :contact_information] if !is_different_admin
procedure = self.deep_clone(include: include_list) do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)
end
procedure.path = SecureRandom.uuid
procedure.aasm_state = :brouillon
Expand Down
2 changes: 1 addition & 1 deletion app/models/procedure_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def compare_type_de_champ(from_type_de_champ, to_type_de_champ, from_coordinates

def replace_type_de_champ_by_clone(coordinate)
cloned_type_de_champ = coordinate.type_de_champ.deep_clone do |original, kopy|
PiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy)
end
coordinate.update!(type_de_champ: cloned_type_de_champ)
cloned_type_de_champ
Expand Down
9 changes: 8 additions & 1 deletion app/serializers/dossier_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ def cerfa
end

def pieces_justificatives
PiecesJustificativesService.serialize_champs_as_pjs(object)
object.champs_public.filter { |champ| champ.type_de_champ.old_pj }.map do |champ|
{
created_at: champ.created_at&.in_time_zone('UTC'),
type_de_piece_justificative_id: champ.type_de_champ.old_pj[:stable_id],
content_url: champ.for_api,
user: champ.dossier.user
}
end.flatten
end

def attestation
Expand Down
33 changes: 33 additions & 0 deletions app/services/clone_pieces_justificatives_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class ClonePiecesJustificativesService
def self.clone_attachments(original, kopy)
case original
when Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp
clone_many_attachments(original, kopy, :piece_justificative_file)
when TypeDeChamp
clone_one_attachment(original, kopy, :piece_justificative_template)
when Procedure
clone_one_attachment(original, kopy, :logo)
clone_one_attachment(original, kopy, :notice)
clone_one_attachment(original, kopy, :deliberation)
when AttestationTemplate
clone_one_attachment(original, kopy, :logo)
clone_one_attachment(original, kopy, :signature)
when Etablissement
clone_one_attachment(original, kopy, :entreprise_attestation_sociale)
clone_one_attachment(original, kopy, :entreprise_attestation_fiscale)
end
end

def self.clone_many_attachments(original, kopy, attachments_name)
original.public_send(attachments_name).attachments.each do |attachment|
kopy.public_send(attachments_name).attach(attachment.blob)
end
end

def self.clone_one_attachment(original, kopy, attachment_name)
attachment = original.public_send(attachment_name)
if attachment.attached?
kopy.public_send(attachment_name).attach(attachment.blob)
end
end
end
Loading

0 comments on commit 0b3a0d5

Please sign in to comment.