tasks: fix the commentaires migration task to work with hidden dossiers

By default `commentaire.dossier` doesn't return the dossier if it is
hidden.
This commit is contained in:
Pierre de La Morinerie 2019-08-01 12:46:47 +00:00
parent 541b3dc8f8
commit 6459e9cf37
3 changed files with 32 additions and 7 deletions

View file

@ -14,12 +14,13 @@ namespace :'2019_05_29_migrate_commentaire_pj' do
progress = ProgressReport.new(commentaires.count)
commentaires.find_each do |commentaire|
if commentaire.file.present?
dossier = Dossier.unscope(where: :hidden_at).find(commentaire.dossier_id)
uri = URI.parse(URI.escape(commentaire.file_url))
response = Typhoeus.get(uri)
if response.success?
filename = commentaire.file.filename || commentaire.file_identifier
updated_at = commentaire.updated_at
dossier_updated_at = commentaire.dossier.updated_at
dossier_updated_at = dossier.updated_at
commentaire.piece_jointe.attach(
io: StringIO.new(response.body),
filename: filename,
@ -27,7 +28,7 @@ namespace :'2019_05_29_migrate_commentaire_pj' do
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
commentaire.update_column(:updated_at, updated_at)
commentaire.dossier.update_column(:updated_at, dossier_updated_at)
dossier.update_column(:updated_at, dossier_updated_at)
end
end
progress.inc

View file

@ -39,6 +39,10 @@ FactoryBot.define do
archived { false }
end
trait :hidden do
hidden_at { Time.zone.now }
end
trait :with_dossier_link do
after(:create) do |dossier, _evaluator|
linked_dossier = create(:dossier)

View file

@ -1,14 +1,16 @@
describe '2019_05_29_migrate_commentaire_pj.rake' do
let(:rake_task) { Rake::Task['2019_05_29_migrate_commentaire_pj:run'] }
let!(:commentaires) do
create(:commentaire)
create(:commentaire, :with_file)
create(:commentaire, :with_file)
let(:commentaires) do
[
create(:commentaire),
create(:commentaire, :with_file),
create(:commentaire, :with_file)
]
end
before do
Commentaire.all.each do |commentaire|
commentaires.each do |commentaire|
if commentaire.file.present?
stub_request(:get, commentaire.file_url)
.to_return(status: 200, body: File.read(commentaire.file.path))
@ -39,4 +41,22 @@ describe '2019_05_29_migrate_commentaire_pj.rake' do
expect(Commentaire.where(file: nil).count).to eq(1)
expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, true, false])
end
context 'when a commentaires dossier is hidden' do
let(:hidden_dossier) { create(:dossier, :en_construction, :hidden) }
let(:commentaire) { create(:commentaire, :with_file, dossier: hidden_dossier) }
let(:commentaires) { [commentaire] }
it 'should migrate the pj' do
comment_updated_at = commentaire.reload.updated_at
dossier_updated_at = hidden_dossier.reload.updated_at
rake_task.invoke
commentaires.each(&:reload)
expect(commentaire.piece_jointe.attached?).to be true
expect(commentaire.updated_at).to eq(comment_updated_at)
expect(hidden_dossier.updated_at).to eq(dossier_updated_at)
end
end
end