Merge pull request #10435 from mfo/fix-clone-private-attachment

Correctif: ETQ usager, lorsque je clone mon dossier, je ne clone pas les PJs des annotations privées
This commit is contained in:
Colin Darie 2024-05-21 19:19:06 +00:00 committed by GitHub
commit dfc1563cfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 130 additions and 1 deletions

View file

@ -39,6 +39,7 @@ class Champ < ApplicationRecord
:departement?,
:region?,
:textarea?,
:piece_justificative?,
:titre_identite?,
:header_section?,
:checkbox?,
@ -238,7 +239,7 @@ class Champ < ApplicationRecord
kopy.write_attribute(:stable_id, original.stable_id)
kopy.write_attribute(:stream, 'main')
end
ClonePiecesJustificativesService.clone_attachments(original, kopy)
ClonePiecesJustificativesService.clone_attachments(original, kopy) if fork || !private?
end
end

View file

@ -0,0 +1,34 @@
# frozen_string_literal: true
module Maintenance
class BackfillClonedChampsPrivatePieceJustificativesTask < MaintenanceTasks::Task
def collection
Dossier.en_brouillon.where.not(parent_dossier_id: nil)
end
def process(cloned_dossier)
cloned_dossier.champs_private
.filter { checkable_pj?(_1, cloned_dossier) }
.map do |cloned_champ|
parent_champ = cloned_dossier.parent_dossier
.champs_private
.find { _1.stable_id == cloned_champ.stable_id }
next if !parent_champ
parent_blob_ids = parent_champ.piece_justificative_file.map(&:blob_id)
cloned_blob_ids = cloned_champ.piece_justificative_file.map(&:blob_id)
if parent_blob_ids.sort == cloned_blob_ids.sort
cloned_champ.piece_justificative_file.detach
end
end
end
def checkable_pj?(champ, dossier)
return false if champ.type != "Champs::PieceJustificativeChamp"
return false if !champ.piece_justificative_file.attached?
true
end
end
end

View file

@ -569,4 +569,44 @@ describe Champ do
it { expect(ActionView::RecordIdentifier.dom_id(champ.type_de_champ)).to eq("type_de_champ_#{champ.type_de_champ.id}") }
it { expect(ActionView::RecordIdentifier.dom_class(champ)).to eq("champ") }
end
describe 'clone' do
subject { champ.clone(fork) }
context 'when champ public' do
let(:champ) { create(:champ_piece_justificative, private: false) }
context 'when fork' do
let(:fork) { true }
it do
expect(subject.piece_justificative_file).to be_attached
end
end
context 'when not fork' do
let(:fork) { false }
it do
expect(subject.piece_justificative_file).to be_attached
end
end
end
context 'champ private' do
let(:champ) { create(:champ_piece_justificative, private: true) }
context 'when fork' do
let(:fork) { true }
it do
expect(subject.piece_justificative_file).to be_attached
end
end
context 'when not fork' do
let(:fork) { false }
it do
expect(subject.piece_justificative_file).not_to be_attached
end
end
end
end
end

View file

@ -0,0 +1,54 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe BackfillClonedChampsPrivatePieceJustificativesTask do
describe "#process" do
let(:procedure) { create(:procedure, types_de_champ_private:) }
let(:types_de_champ_private) { [{ type: :piece_justificative }, { type: :text }] }
let(:parent_dossier) { create(:dossier, procedure:) }
let(:cloned_dossier) { create(:dossier, procedure:) }
let(:parent_champ_pj) { parent_dossier.champs_private.find(&:piece_justificative?) }
let(:cloned_champ_pj) { cloned_dossier.champs_private.find(&:piece_justificative?) }
before do
cloned_dossier.update(parent_dossier:) # used on factorie, does not seed private_champs..
parent_champ_pj.piece_justificative_file.attach(
io: StringIO.new("x" * 2),
filename: "me.jpg",
content_type: "image/png",
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
end
subject { described_class.process(cloned_dossier) }
context 'when dossier and parent have the same pjs' do
it 'detaches sames blob between parent_dossier and dossier' do
cloned_champ_pj.piece_justificative_file.attach(parent_champ_pj.piece_justificative_file.first.blob)
subject
expect(cloned_champ_pj.reload.piece_justificative_file.attached?).to be_falsey
end
end
context 'when dossier and parent have different pjs' do
it 'keeps different blobs between parent_dossier and dossier' do
cloned_champ_pj.piece_justificative_file.attach(
io: StringIO.new("x" * 2),
filename: "me.jpg",
content_type: "image/png",
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
described_class.process(cloned_dossier)
subject
expect(cloned_champ_pj.reload.piece_justificative_file.attached?).to be_truthy
end
end
end
end
end