diff --git a/app/models/dossier_operation_log.rb b/app/models/dossier_operation_log.rb index 902626a85..06ff511e9 100644 --- a/app/models/dossier_operation_log.rb +++ b/app/models/dossier_operation_log.rb @@ -41,7 +41,9 @@ class DossierOperationLog < ApplicationRecord def self.purge_discarded not_deletion.destroy_all - with_data.each(&:move_to_cold_storage!) + + supprimer.map { _1.serialized.purge_later } + supprimer.update_all(data: nil) end def self.create_and_serialize(params) diff --git a/spec/models/dossier_operation_log_spec.rb b/spec/models/dossier_operation_log_spec.rb new file mode 100644 index 000000000..bd029de88 --- /dev/null +++ b/spec/models/dossier_operation_log_spec.rb @@ -0,0 +1,87 @@ +describe DossierOperationLog, type: :model do + describe '.purge_discarded' do + let(:dossier) { create(:dossier) } + let!(:witness_dossier) do + d = create(:dossier) + + DossierOperationLog.create_and_serialize( + dossier: d, + operation: DossierOperationLog.operations[:passer_en_instruction], + author: instructeur + ) + + DossierOperationLog.create_and_serialize( + dossier: d, + operation: DossierOperationLog.operations[:supprimer], + author: instructeur + ) + + d + end + + let(:instructeur) { create(:instructeur) } + + def dols = dossier.dossier_operation_logs.reload + def supprimer_dol = dols.find_by(operation: 'supprimer') + def witness_dols = witness_dossier.dossier_operation_logs.reload + def witness_supprimer_dol = witness_dols.find_by(operation: 'supprimer') + + it 'purges all operations but supprimer' do + DossierOperationLog.create_and_serialize( + dossier:, + operation: DossierOperationLog.operations[:passer_en_instruction], + author: instructeur + ) + + DossierOperationLog.create_and_serialize( + dossier:, + operation: DossierOperationLog.operations[:supprimer], + author: instructeur + ) + + expect(dols.count).to eq(2) + + dols.purge_discarded + + expect(dols.map(&:operation)).to match_array('supprimer') + expect(witness_dols.map(&:operation)).to match_array(['passer_en_instruction', 'supprimer']) + end + + it 'destroys the serialized json from supprimer dol' do + DossierOperationLog.create_and_serialize( + dossier:, + operation: DossierOperationLog.operations[:supprimer], + author: instructeur + ) + + # serialize data attribute to json and store it to cold storage + perform_enqueued_jobs do + dols.each(&:move_to_cold_storage!) + witness_dols.each(&:move_to_cold_storage!) + end + + expect(supprimer_dol.serialized.attached?).to be true + + perform_enqueued_jobs { dols.purge_discarded } + + expect(supprimer_dol.serialized.attached?).to be false + expect(witness_supprimer_dol.serialized.attached?).to be true + end + + it 'nillifies the data attribut of not net seriaized supprimer dol' do + DossierOperationLog.create_and_serialize( + dossier:, + operation: DossierOperationLog.operations[:supprimer], + author: instructeur + ) + + expect(supprimer_dol.data).to be_present + + dols.purge_discarded + + expect(supprimer_dol.serialized.attached?).to be false + expect(supprimer_dol.data).to be_nil + expect(witness_supprimer_dol.data).to be_present + end + end +end