amelioration(clone.piece_justificative): gestion du clonage des piece justificative dans une tache asynchrone

This commit is contained in:
Martin 2022-11-08 19:00:15 +01:00
parent 05dcf5e7cf
commit 90f7d265c6
7 changed files with 56 additions and 15 deletions

View file

@ -0,0 +1,5 @@
class ClonePieceJustificativeJob < ApplicationJob
def perform(from_champ, kopy_champ)
from_champ.clone_piece_justificative(kopy_champ)
end
end

View file

@ -30,6 +30,7 @@ class Champ < ApplicationRecord
has_many :geo_areas, -> { order(:created_at) }, dependent: :destroy, inverse_of: :champ
belongs_to :etablissement, optional: true, dependent: :destroy
has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
after_create_commit :after_clone, if: :cloned?
delegate :libelle,
:type_champ,
@ -223,19 +224,33 @@ class Champ < ApplicationRecord
kopy
end
def clone_piece_justificative(kopy)
if piece_justificative_file.attached?
piece_justificative_file.open do |tempfile|
kopy.piece_justificative_file.attach({
io: File.open(tempfile.path),
filename: piece_justificative_file.filename,
content_type: piece_justificative_file.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
})
end
def mark_for_delayed_clone_piece_justificative(from)
if from.piece_justificative_file.attached?
@cloned_from = from
@cloned_kopy = self
end
end
def after_clone
ClonePieceJustificativeJob.perform_later(@cloned_from, @cloned_kopy)
end
def cloned?
@cloned_from && @cloned_kopy
end
def clone_piece_justificative(kopy)
piece_justificative_file.open do |tempfile|
kopy.piece_justificative_file.attach({
io: File.open(tempfile.path),
filename: piece_justificative_file.filename,
content_type: piece_justificative_file.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
})
end
rescue ActiveStorage::FileNotFoundError, ActiveStorage::IntegrityError
end
private
def champs_for_condition

View file

@ -55,7 +55,7 @@ class Champs::PieceJustificativeChamp < Champ
def clone(dossier:, parent: nil)
kopy = super(dossier: dossier, parent: parent)
clone_piece_justificative(kopy)
kopy.mark_for_delayed_clone_piece_justificative(self)
kopy
end
end

View file

@ -47,7 +47,7 @@ class Champs::TitreIdentiteChamp < Champ
def clone(dossier:, parent: nil)
kopy = super(dossier: dossier, parent: parent)
clone_piece_justificative(kopy)
mark_for_delayed_clone_piece_justificative(kopy)
kopy
end
end

View file

@ -139,7 +139,9 @@ FactoryBot.define do
type_de_champ { association :type_de_champ_dossier_link, procedure: dossier.procedure }
value { create(:dossier).id }
end
factory :champ_without_piece_justificative, class: 'Champs::PieceJustificativeChamp' do
type_de_champ { association :type_de_champ_piece_justificative, procedure: dossier.procedure }
end
factory :champ_piece_justificative, class: 'Champs::PieceJustificativeChamp' do
type_de_champ { association :type_de_champ_piece_justificative, procedure: dossier.procedure }

View file

@ -0,0 +1,19 @@
describe ClonePieceJustificativeJob, type: :job do
describe 'perform' do
let(:dossier_from) { create(:dossier) }
let(:dossier_to) { create(:dossier, procedure: dossier_from.procedure) }
let(:champ_piece_justificative_from) { create(:champ, :with_piece_justificative_file, dossier_id: dossier_from.id) }
let(:champ_piece_justificative_to) { create(:champ_without_piece_justificative, dossier_id: dossier_to.id, piece_justificative_file: nil) }
it 'creates a piece_justificative_file' do
expect {
ClonePieceJustificativeJob.perform_now(champ_piece_justificative_from, champ_piece_justificative_to)
}.to change { champ_piece_justificative_to.piece_justificative_file.blob }.from(nil).to an_instance_of(ActiveStorage::Blob)
end
it 'creates a piece_justificative_file' do
ClonePieceJustificativeJob.perform_now(champ_piece_justificative_from, champ_piece_justificative_to)
expect(champ_piece_justificative_to.piece_justificative_file.blob.download)
.to eq(champ_piece_justificative_from.piece_justificative_file.blob.download)
end
end
end

View file

@ -1834,8 +1834,8 @@ describe Dossier do
let(:dossier) { create(:dossier) }
let(:champ_piece_justificative) { create(:champ_piece_justificative, dossier_id: dossier.id) }
before { dossier.champs << champ_piece_justificative }
it { expect(Champs::PieceJustificativeChamp.where(dossier: dossier).first.piece_justificative_file).not_to be_nil }
it { expect(Champs::PieceJustificativeChamp.where(dossier: new_dossier).first.piece_justificative_file.blob.id).not_to eq(champ_piece_justificative.piece_justificative_file.blob.id) }
it { expect(Champs::PieceJustificativeChamp.where(dossier: new_dossier).first.piece_justificative_file.blob).to be_nil }
it { expect { new_dossier }.to have_enqueued_job(ClonePieceJustificativeJob) }
end
end