diff --git a/app/jobs/auto_archive_procedure_dossiers_job.rb b/app/jobs/auto_archive_procedure_dossiers_job.rb new file mode 100644 index 00000000000..44b3387ab5f --- /dev/null +++ b/app/jobs/auto_archive_procedure_dossiers_job.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class AutoArchiveProcedureDossiersJob < ApplicationJob + def perform(procedure) + procedure + .dossiers + .state_en_construction + .find_each do |d| + begin + d.passer_automatiquement_en_instruction! + rescue StandardError => e + Sentry.capture_exception(e, extra: { procedure_id: procedure.id }) + end + end + end +end diff --git a/app/jobs/cron/auto_archive_procedure_job.rb b/app/jobs/cron/auto_archive_procedure_job.rb index 130238cb9a3..6dd5593fb91 100644 --- a/app/jobs/cron/auto_archive_procedure_job.rb +++ b/app/jobs/cron/auto_archive_procedure_job.rb @@ -2,18 +2,15 @@ class Cron::AutoArchiveProcedureJob < Cron::CronJob self.schedule_expression = "every 1 minute" + queue_as :critical def perform(*args) procedures_to_close.each do |procedure| # A buggy procedure should NEVER prevent the closing of another procedure # we therefore exceptionally add a `begin resue` block. begin - procedure - .dossiers - .state_en_construction - .find_each(&:passer_automatiquement_en_instruction!) - procedure.close! + AutoArchiveProcedureDossiersJob.perform_later(procedure) rescue StandardError => e Sentry.capture_exception(e, extra: { procedure_id: procedure.id }) end diff --git a/spec/jobs/auto_archive_procedure_dossiers_job_spec.rb b/spec/jobs/auto_archive_procedure_dossiers_job_spec.rb new file mode 100644 index 00000000000..45423ea1e87 --- /dev/null +++ b/spec/jobs/auto_archive_procedure_dossiers_job_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +RSpec.describe AutoArchiveProcedureDossiersJob, type: :job do + let!(:procedure) { create(:procedure, :published, :with_instructeur) } + let!(:job) { AutoArchiveProcedureDossiersJob.new } + before do + procedure.auto_archive_on = 1.day.ago.to_date + procedure.save(validate: false) + end + subject { job.perform(procedure) } + + context "when procedures have auto_archive_on set on yesterday or today" do + let!(:dossier1) { create(:dossier, procedure: procedure) } + let!(:dossier2) { create(:dossier, :en_construction, procedure: procedure) } + let!(:dossier3) { create(:dossier, :en_construction, procedure: procedure) } + let!(:dossier4) { create(:dossier, :en_construction, procedure: procedure) } + let!(:dossier5) { create(:dossier, :en_instruction, procedure: procedure) } + let!(:dossier6) { create(:dossier, :accepte, procedure: procedure) } + let!(:dossier7) { create(:dossier, :refuse, procedure: procedure) } + let!(:dossier8) { create(:dossier, :sans_suite, procedure: procedure) } + let(:last_operation) { dossier2.dossier_operation_logs.last } + + before do + subject + + [dossier1, dossier2, dossier3, dossier4, dossier5, dossier6, dossier7, dossier8].each(&:reload) + + procedure.reload + end + + it { + expect(dossier1.state).to eq Dossier.states.fetch(:brouillon) + expect(dossier2.state).to eq Dossier.states.fetch(:en_instruction) + expect(last_operation.operation).to eq('passer_en_instruction') + expect(last_operation.automatic_operation?).to be_truthy + expect(dossier3.state).to eq Dossier.states.fetch(:en_instruction) + expect(dossier4.state).to eq Dossier.states.fetch(:en_instruction) + expect(dossier5.state).to eq Dossier.states.fetch(:en_instruction) + expect(dossier6.state).to eq Dossier.states.fetch(:accepte) + expect(dossier7.state).to eq Dossier.states.fetch(:refuse) + expect(dossier8.state).to eq Dossier.states.fetch(:sans_suite) + } + end +end diff --git a/spec/jobs/cron/auto_archive_procedure_job_spec.rb b/spec/jobs/cron/auto_archive_procedure_job_spec.rb index 36bf8b96f6b..f24789c8f8d 100644 --- a/spec/jobs/cron/auto_archive_procedure_job_spec.rb +++ b/spec/jobs/cron/auto_archive_procedure_job_spec.rb @@ -46,20 +46,6 @@ procedure_aujourdhui.reload end - it { - expect(dossier1.state).to eq Dossier.states.fetch(:brouillon) - expect(dossier2.state).to eq Dossier.states.fetch(:en_instruction) - expect(last_operation.operation).to eq('passer_en_instruction') - expect(last_operation.automatic_operation?).to be_truthy - expect(dossier3.state).to eq Dossier.states.fetch(:en_instruction) - expect(dossier4.state).to eq Dossier.states.fetch(:en_instruction) - expect(dossier5.state).to eq Dossier.states.fetch(:en_instruction) - expect(dossier6.state).to eq Dossier.states.fetch(:accepte) - expect(dossier7.state).to eq Dossier.states.fetch(:refuse) - expect(dossier8.state).to eq Dossier.states.fetch(:sans_suite) - expect(dossier9.state).to eq Dossier.states.fetch(:en_instruction) - } - it { expect(procedure_hier.close?).to eq true expect(procedure_aujourdhui.close?).to eq true