Merge pull request #8307 from tchak/refactor-dossier-clone

fix(dossier): clone should include more attachments
This commit is contained in:
Paul Chavard 2022-12-20 22:01:13 +01:00 committed by GitHub
commit 1e200262dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 35 deletions

View file

@ -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

View file

@ -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)

View file

@ -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