Skip to content

Commit

Permalink
Merge pull request #11057 from mfo/US/fix-autoarchive-stalled
Browse files Browse the repository at this point in the history
ETQ DS, je souhaite ne pas envoyer plus de mail que prévu quand une demarche se clos a une date prévue
  • Loading branch information
mfo authored Nov 18, 2024
2 parents 1ed0599 + babdf95 commit aeced40
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
16 changes: 16 additions & 0 deletions app/jobs/auto_archive_procedure_dossiers_job.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 2 additions & 5 deletions app/jobs/cron/auto_archive_procedure_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions spec/jobs/auto_archive_procedure_dossiers_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 0 additions & 14 deletions spec/jobs/cron/auto_archive_procedure_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aeced40

Please sign in to comment.