From 227296682b507ea33f55aa7910d0dcd9f492d60b Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 16 Dec 2022 15:43:36 +0100 Subject: [PATCH] fix(dossier): clone should include more attachments --- app/models/champ.rb | 19 ++++-------- app/models/dossier.rb | 29 ++++++++++--------- app/services/pieces_justificatives_service.rb | 22 ++++++++------ 3 files changed, 35 insertions(+), 35 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index 1833f5cde..69dd3bc11 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -220,21 +220,14 @@ class Champ < ApplicationRecord end end - def clone(dossier:, parent: nil) - kopy = deep_clone(only: (private? ? [] : [:value, :value_json, :data, :external_id]) + [:private, :row, :type, :type_de_champ_id], - include: private? ? [] : [:etablissement, :geo_areas]) + def clone + champ_attributes = [:parent_id, :private, :row, :type, :type_de_champ_id] + value_attributes = private? ? [] : [:value, :value_json, :data, :external_id] + relationships = private? ? [] : [:etablissement, :geo_areas] - kopy.dossier = dossier - kopy.parent = parent if parent - case self - when Champs::RepetitionChamp - kopy.champs = (private? ? champs.where(row: 0) : champs).map do |champ_de_repetition| - champ_de_repetition.clone(dossier: dossier, parent: kopy) - end - when Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp - PiecesJustificativesService.clone_attachments(self, kopy) if !private? && piece_justificative_file.attached? + deep_clone(only: champ_attributes + value_attributes, include: relationships) do |original, kopy| + PiecesJustificativesService.clone_attachments(original, kopy) end - kopy end private diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 34421ec7e..000640ca0 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1211,27 +1211,30 @@ class Dossier < ApplicationRecord @sections[champ.parent || (champ.public? ? :public : :private)] end - # while cloning we do not have champ.id. it comes after transaction - # so we collect a list of jobs to process. then enqueue this list def clone - cloned_dossier = deep_clone(only: [:autorisation_donnees, :user_id, :revision_id, :groupe_instructeur_id], - include: [:individual, :etablissement]) do |original, kopy| + dossier_attributes = [:autorisation_donnees, :user_id, :revision_id, :groupe_instructeur_id] + relationships = [:individual, :etablissement] + + cloned_dossier = deep_clone(only: dossier_attributes, include: relationships) do |original, kopy| + PiecesJustificativesService.clone_attachments(original, kopy) + if original.is_a?(Dossier) kopy.parent_dossier_id = original.id kopy.state = Dossier.states.fetch(:brouillon) - kopy.champs_public = original.champs_public.map do |champ| - champ.clone(dossier: kopy) - end - kopy.champs_private = original.champs_private.map do |champ| - champ.clone(dossier: kopy) + cloned_champs = original.champs + .index_by(&:id) + .transform_values(&:clone) + + kopy.champs = cloned_champs.values.map do |champ| + champ.dossier = kopy + champ.parent = cloned_champs[champ.parent_id] if champ.child? + champ end end end - transaction do - cloned_dossier.save! - end - cloned_dossier + transaction { cloned_dossier.save! } + cloned_dossier.reload end def find_champs_by_stable_ids(stable_ids) diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 0c6317108..51efb97c7 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -62,20 +62,24 @@ class PiecesJustificativesService kopy.piece_justificative_file.attach(attachment.blob) end when TypeDeChamp - clone_attachment(original.piece_justificative_template, kopy.piece_justificative_template) + clone_attachment(original, kopy, :piece_justificative_template) when Procedure - clone_attachment(original.logo, kopy.logo) - clone_attachment(original.notice, kopy.notice) - clone_attachment(original.deliberation, kopy.deliberation) + clone_attachment(original, kopy, :logo) + clone_attachment(original, kopy, :notice) + clone_attachment(original, kopy, :deliberation) when AttestationTemplate - clone_attachment(original.logo, kopy.logo) - clone_attachment(original.signature, kopy.signature) + clone_attachment(original, kopy, :logo) + clone_attachment(original, kopy, :signature) + when Etablissement + clone_attachment(original, kopy, :entreprise_attestation_sociale) + clone_attachment(original, kopy, :entreprise_attestation_fiscale) end end - def self.clone_attachment(original_attachment, copy_attachment) - if original_attachment.attached? - copy_attachment.attach(original_attachment.blob) + def self.clone_attachment(original, kopy, attachment_name) + attachment = original.public_send(attachment_name) + if attachment.attached? + kopy.public_send(attachment_name).attach(attachment.blob) end end