Merge pull request #3283 from betagouv/fix_3268_fix_admin_procedures

Fix 3268 fix admin procedures
This commit is contained in:
Pierre de La Morinerie 2019-01-17 10:42:21 +01:00 committed by GitHub
commit e39fd7b171
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 146 additions and 47 deletions

View file

@ -3,11 +3,10 @@ class AutoArchiveProcedureJob < ApplicationJob
def perform(*args)
Procedure.publiees.where("auto_archive_on <= ?", Date.today).each do |procedure|
gestionnaire = procedure.gestionnaire_for_cron_job
procedure.dossiers.state_en_construction.find_each do |dossier|
dossier.passer_en_instruction!(gestionnaire)
end
procedure
.dossiers
.state_en_construction
.find_each(&:passer_automatiquement_en_instruction!)
procedure.archive!
end

View file

@ -3,17 +3,18 @@ class AutoReceiveDossiersForProcedureJob < ApplicationJob
def perform(procedure_id, state)
procedure = Procedure.find(procedure_id)
gestionnaire = procedure.gestionnaire_for_cron_job
case state
when Dossier.states.fetch(:en_instruction)
procedure.dossiers.state_en_construction.find_each do |dossier|
dossier.passer_en_instruction!(gestionnaire)
end
procedure
.dossiers
.state_en_construction
.find_each(&:passer_automatiquement_en_instruction!)
when Dossier.states.fetch(:accepte)
procedure.dossiers.state_en_construction.find_each do |dossier|
dossier.accepter!(gestionnaire, '')
end
procedure
.dossiers
.state_en_construction
.find_each(&:accepter_automatiquement!)
else
raise "Receiving Procedure##{procedure_id} in invalid state \"#{state}\""
end

View file

@ -274,6 +274,12 @@ class Dossier < ApplicationRecord
log_dossier_operation(gestionnaire, :passer_en_instruction)
end
def passer_automatiquement_en_instruction!
en_instruction!
log_dossier_operation(nil, :passer_en_instruction, automatic_operation: true)
end
def repasser_en_construction!(gestionnaire)
self.en_instruction_at = nil
en_construction!
@ -295,6 +301,19 @@ class Dossier < ApplicationRecord
log_dossier_operation(gestionnaire, :accepter)
end
def accepter_automatiquement!
self.en_instruction_at ||= Time.zone.now
accepte!
if attestation.nil?
update(attestation: build_attestation)
end
NotificationMailer.send_closed_notification(self).deliver_later
log_dossier_operation(nil, :accepter, automatic_operation: true)
end
def refuser!(gestionnaire, motivation)
self.motivation = motivation
self.en_instruction_at ||= Time.zone.now
@ -317,10 +336,11 @@ class Dossier < ApplicationRecord
private
def log_dossier_operation(gestionnaire, operation)
def log_dossier_operation(gestionnaire, operation, automatic_operation: false)
dossier_operation_logs.create(
gestionnaire: gestionnaire,
operation: DossierOperationLog.operations.fetch(operation)
operation: DossierOperationLog.operations.fetch(operation),
automatic_operation: automatic_operation
)
end

View file

@ -350,12 +350,6 @@ class Procedure < ApplicationRecord
where.not(aasm_state: :archivee).where("path LIKE ?", "%#{path}%")
end
def gestionnaire_for_cron_job
administrateur_email = administrateur.email
gestionnaire = Gestionnaire.find_by(email: administrateur_email)
gestionnaire || gestionnaires.first
end
def populate_champ_stable_ids
TypeDeChamp.where(procedure: self, stable_id: nil).find_each do |type_de_champ|
type_de_champ.update_column(:stable_id, type_de_champ.id)

View file

@ -0,0 +1,5 @@
class AddAutomaticOperationColumnToDossierOperationLog < ActiveRecord::Migration[5.2]
def change
add_column :dossier_operation_logs, :automatic_operation, :bool, default: false, null: false
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_12_21_103901) do
ActiveRecord::Schema.define(version: 2019_01_10_163655) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -218,6 +218,7 @@ ActiveRecord::Schema.define(version: 2018_12_21_103901) do
t.bigint "gestionnaire_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "automatic_operation", default: false, null: false
t.index ["dossier_id"], name: "index_dossier_operation_logs_on_dossier_id"
t.index ["gestionnaire_id"], name: "index_dossier_operation_logs_on_gestionnaire_id"
end

View file

@ -40,6 +40,7 @@ RSpec.describe AutoArchiveProcedureJob, type: :job do
it {
expect(dossier1.state).to eq Dossier.states.fetch(:brouillon)
expect(dossier2.state).to eq Dossier.states.fetch(:en_instruction)
expect(dossier2.dossier_operation_logs.pluck(:gestionnaire_id, :operation, :automatic_operation)).to match([[nil, 'passer_en_instruction', true]])
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)

View file

@ -13,13 +13,17 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
before do
Timecop.freeze(date)
nouveau_dossier1
nouveau_dossier2
dossier_recu
dossiers = [
nouveau_dossier1,
nouveau_dossier2,
dossier_recu,
dossier_brouillon
]
create(:attestation_template, procedure: procedure)
AutoReceiveDossiersForProcedureJob.new.perform(procedure.id, state)
dossiers.each(&:reload)
end
after { Timecop.return }
@ -29,17 +33,18 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
let(:state) { Dossier.states.fetch(:en_instruction) }
it {
expect(nouveau_dossier1.reload.en_instruction?).to be true
expect(nouveau_dossier1.reload.en_instruction_at).to eq(date)
expect(nouveau_dossier1.en_instruction?).to be true
expect(nouveau_dossier1.en_instruction_at).to eq(date)
expect(nouveau_dossier1.dossier_operation_logs.pluck(:gestionnaire_id, :operation, :automatic_operation)).to match([[nil, 'passer_en_instruction', true]])
expect(nouveau_dossier2.reload.en_instruction?).to be true
expect(nouveau_dossier2.reload.en_instruction_at).to eq(date)
expect(nouveau_dossier2.en_instruction?).to be true
expect(nouveau_dossier2.en_instruction_at).to eq(date)
expect(dossier_recu.reload.en_instruction?).to be true
expect(dossier_recu.reload.en_instruction_at).to eq(instruction_date)
expect(dossier_recu.en_instruction?).to be true
expect(dossier_recu.en_instruction_at).to eq(instruction_date)
expect(dossier_brouillon.reload.brouillon?).to be true
expect(dossier_brouillon.reload.en_instruction_at).to eq(nil)
expect(dossier_brouillon.brouillon?).to be true
expect(dossier_brouillon.en_instruction_at).to eq(nil)
}
end
@ -47,23 +52,24 @@ RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do
let(:state) { Dossier.states.fetch(:accepte) }
it {
expect(nouveau_dossier1.reload.accepte?).to be true
expect(nouveau_dossier1.reload.en_instruction_at).to eq(date)
expect(nouveau_dossier1.reload.processed_at).to eq(date)
expect(nouveau_dossier1.reload.attestation).to be_present
expect(nouveau_dossier1.accepte?).to be true
expect(nouveau_dossier1.en_instruction_at).to eq(date)
expect(nouveau_dossier1.processed_at).to eq(date)
expect(nouveau_dossier1.attestation).to be_present
expect(nouveau_dossier1.dossier_operation_logs.pluck(:gestionnaire_id, :operation, :automatic_operation)).to match([[nil, 'accepter', true]])
expect(nouveau_dossier2.reload.accepte?).to be true
expect(nouveau_dossier2.reload.en_instruction_at).to eq(date)
expect(nouveau_dossier2.reload.processed_at).to eq(date)
expect(nouveau_dossier2.reload.attestation).to be_present
expect(nouveau_dossier2.accepte?).to be true
expect(nouveau_dossier2.en_instruction_at).to eq(date)
expect(nouveau_dossier2.processed_at).to eq(date)
expect(nouveau_dossier2.attestation).to be_present
expect(dossier_recu.reload.en_instruction?).to be true
expect(dossier_recu.reload.en_instruction_at).to eq(instruction_date)
expect(dossier_recu.reload.processed_at).to eq(nil)
expect(dossier_recu.en_instruction?).to be true
expect(dossier_recu.en_instruction_at).to eq(instruction_date)
expect(dossier_recu.processed_at).to eq(nil)
expect(dossier_brouillon.reload.brouillon?).to be true
expect(dossier_brouillon.reload.en_instruction_at).to eq(nil)
expect(dossier_brouillon.reload.processed_at).to eq(nil)
expect(dossier_brouillon.brouillon?).to be true
expect(dossier_brouillon.en_instruction_at).to eq(nil)
expect(dossier_brouillon.processed_at).to eq(nil)
}
end
end

View file

@ -760,4 +760,76 @@ describe Dossier do
it { expect(long_expired_dossier).to be_retention_expired }
end
end
describe '#accepter!' do
let(:dossier) { create(:dossier) }
let!(:gestionnaire) { create(:gestionnaire) }
let!(:now) { Time.zone.parse('01/01/2100') }
let(:attestation) { Attestation.new }
before do
allow(NotificationMailer).to receive(:send_closed_notification).and_return(double(deliver_later: true))
allow(dossier).to receive(:build_attestation).and_return(attestation)
Timecop.freeze(now)
dossier.accepter!(gestionnaire, 'motivation')
dossier.reload
end
after { Timecop.return }
it { expect(dossier.motivation).to eq('motivation') }
it { expect(dossier.en_instruction_at).to eq(now) }
it { expect(dossier.processed_at).to eq(now) }
it { expect(dossier.state).to eq('accepte') }
it { expect(dossier.dossier_operation_logs.pluck(:gestionnaire_id, :operation, :automatic_operation)).to match([[gestionnaire.id, 'accepter', false]]) }
it { expect(NotificationMailer).to have_received(:send_closed_notification).with(dossier) }
it { expect(dossier.attestation).to eq(attestation) }
end
describe '#accepter_automatiquement!' do
let(:dossier) { create(:dossier) }
let!(:now) { Time.zone.parse('01/01/2100') }
let(:attestation) { Attestation.new }
before do
allow(NotificationMailer).to receive(:send_closed_notification).and_return(double(deliver_later: true))
allow(dossier).to receive(:build_attestation).and_return(attestation)
Timecop.freeze(now)
dossier.accepter_automatiquement!
dossier.reload
end
after { Timecop.return }
it { expect(dossier.motivation).to eq(nil) }
it { expect(dossier.en_instruction_at).to eq(now) }
it { expect(dossier.processed_at).to eq(now) }
it { expect(dossier.state).to eq('accepte') }
it { expect(dossier.dossier_operation_logs.pluck(:gestionnaire_id, :operation, :automatic_operation)).to match([[nil, 'accepter', true]]) }
it { expect(NotificationMailer).to have_received(:send_closed_notification).with(dossier) }
it { expect(dossier.attestation).to eq(attestation) }
end
describe '#passer_en_instruction!' do
let(:dossier) { create(:dossier) }
let(:gestionnaire) { create(:gestionnaire) }
before { dossier.passer_en_instruction!(gestionnaire) }
it { expect(dossier.state).to eq('en_instruction') }
it { expect(dossier.followers_gestionnaires).to include(gestionnaire) }
it { expect(dossier.dossier_operation_logs.pluck(:gestionnaire_id, :operation)).to match([[gestionnaire.id, 'passer_en_instruction']]) }
end
describe '#passer_automatiquement_en_instruction!' do
let(:dossier) { create(:dossier) }
let(:gestionnaire) { create(:gestionnaire) }
before { dossier.passer_automatiquement_en_instruction! }
it { expect(dossier.followers_gestionnaires).not_to include(gestionnaire) }
it { expect(dossier.dossier_operation_logs.pluck(:gestionnaire_id, :operation, :automatic_operation)).to match([[nil, 'passer_en_instruction', true]]) }
end
end