From 28b338452afcbc974a8fe7e9d45b7c8aacd33d5c Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Tue, 28 Nov 2023 16:47:37 +0100 Subject: [PATCH] feat(dossier_purge): when the dols are purged, only keep supprimer dol but destroy its data Note, the only remaining data are dossier_id, automatic_operation, digest and bill_id, created_at --- app/models/dossier_operation_log.rb | 4 +- spec/models/dossier_operation_log_spec.rb | 87 +++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 spec/models/dossier_operation_log_spec.rb 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