update state date in aasm after callbacks
call aasm event methods, not state methods directly
This commit is contained in:
parent
161b63f487
commit
f631acd118
5 changed files with 52 additions and 41 deletions
|
@ -149,7 +149,7 @@ module Users
|
||||||
errors = update_dossier_and_compute_errors
|
errors = update_dossier_and_compute_errors
|
||||||
|
|
||||||
if passage_en_construction? && errors.blank?
|
if passage_en_construction? && errors.blank?
|
||||||
@dossier.en_construction!
|
@dossier.passer_en_construction!
|
||||||
NotificationMailer.send_initiated_notification(@dossier).deliver_later
|
NotificationMailer.send_initiated_notification(@dossier).deliver_later
|
||||||
@dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
|
@dossier.groupe_instructeur.instructeurs.with_instant_email_dossier_notifications.each do |instructeur|
|
||||||
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
|
DossierMailer.notify_new_dossier_depose_to_instructeur(@dossier, instructeur.email).deliver_later
|
||||||
|
|
|
@ -282,7 +282,6 @@ class Dossier < ApplicationRecord
|
||||||
delegate :types_de_champ, to: :procedure
|
delegate :types_de_champ, to: :procedure
|
||||||
delegate :france_connect_information, to: :user
|
delegate :france_connect_information, to: :user
|
||||||
|
|
||||||
before_validation :update_state_dates, if: -> { state_changed? }
|
|
||||||
before_save :build_default_champs, if: Proc.new { groupe_instructeur_id_was.nil? }
|
before_save :build_default_champs, if: Proc.new { groupe_instructeur_id_was.nil? }
|
||||||
before_save :update_search_terms
|
before_save :update_search_terms
|
||||||
|
|
||||||
|
@ -508,20 +507,23 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def after_passer_en_construction
|
||||||
|
update!(en_construction_at: Time.zone.now) if self.en_construction_at.nil?
|
||||||
|
end
|
||||||
|
|
||||||
def after_passer_en_instruction(instructeur)
|
def after_passer_en_instruction(instructeur)
|
||||||
instructeur.follow(self)
|
instructeur.follow(self)
|
||||||
|
|
||||||
|
update!(en_instruction_at: Time.zone.now) if self.en_instruction_at.nil?
|
||||||
log_dossier_operation(instructeur, :passer_en_instruction)
|
log_dossier_operation(instructeur, :passer_en_instruction)
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_passer_automatiquement_en_instruction
|
def after_passer_automatiquement_en_instruction
|
||||||
|
update!(en_instruction_at: Time.zone.now) if self.en_instruction_at.nil?
|
||||||
log_automatic_dossier_operation(:passer_en_instruction)
|
log_automatic_dossier_operation(:passer_en_instruction)
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_repasser_en_construction(instructeur)
|
def after_repasser_en_construction(instructeur)
|
||||||
self.en_instruction_at = nil
|
|
||||||
|
|
||||||
save!
|
|
||||||
log_dossier_operation(instructeur, :repasser_en_construction)
|
log_dossier_operation(instructeur, :repasser_en_construction)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -529,6 +531,7 @@ class Dossier < ApplicationRecord
|
||||||
self.archived = false
|
self.archived = false
|
||||||
self.processed_at = nil
|
self.processed_at = nil
|
||||||
self.motivation = nil
|
self.motivation = nil
|
||||||
|
self.en_instruction_at = Time.zone.now
|
||||||
attestation&.destroy
|
attestation&.destroy
|
||||||
|
|
||||||
save!
|
save!
|
||||||
|
@ -543,6 +546,8 @@ class Dossier < ApplicationRecord
|
||||||
self.justificatif_motivation.attach(justificatif)
|
self.justificatif_motivation.attach(justificatif)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.processed_at = Time.zone.now
|
||||||
|
|
||||||
if attestation.nil?
|
if attestation.nil?
|
||||||
self.attestation = build_attestation
|
self.attestation = build_attestation
|
||||||
end
|
end
|
||||||
|
@ -559,6 +564,7 @@ class Dossier < ApplicationRecord
|
||||||
self.attestation = build_attestation
|
self.attestation = build_attestation
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.processed_at = Time.zone.now
|
||||||
save!
|
save!
|
||||||
NotificationMailer.send_closed_notification(self).deliver_later
|
NotificationMailer.send_closed_notification(self).deliver_later
|
||||||
log_automatic_dossier_operation(:accepter, self)
|
log_automatic_dossier_operation(:accepter, self)
|
||||||
|
@ -571,6 +577,7 @@ class Dossier < ApplicationRecord
|
||||||
self.justificatif_motivation.attach(justificatif)
|
self.justificatif_motivation.attach(justificatif)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.processed_at = Time.zone.now
|
||||||
save!
|
save!
|
||||||
NotificationMailer.send_refused_notification(self).deliver_later
|
NotificationMailer.send_refused_notification(self).deliver_later
|
||||||
log_dossier_operation(instructeur, :refuser, self)
|
log_dossier_operation(instructeur, :refuser, self)
|
||||||
|
@ -583,6 +590,7 @@ class Dossier < ApplicationRecord
|
||||||
self.justificatif_motivation.attach(justificatif)
|
self.justificatif_motivation.attach(justificatif)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.processed_at = Time.zone.now
|
||||||
save!
|
save!
|
||||||
NotificationMailer.send_without_continuation_notification(self).deliver_later
|
NotificationMailer.send_without_continuation_notification(self).deliver_later
|
||||||
log_dossier_operation(instructeur, :classer_sans_suite, self)
|
log_dossier_operation(instructeur, :classer_sans_suite, self)
|
||||||
|
@ -766,16 +774,6 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_state_dates
|
|
||||||
if en_construction? && !self.en_construction_at
|
|
||||||
self.en_construction_at = Time.zone.now
|
|
||||||
elsif en_instruction? && !self.en_instruction_at
|
|
||||||
self.en_instruction_at = Time.zone.now
|
|
||||||
elsif TERMINE.include?(state) && !self.processed_at
|
|
||||||
self.processed_at = Time.zone.now
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def send_dossier_received
|
def send_dossier_received
|
||||||
if saved_change_to_state? && en_instruction? && !procedure.declarative_accepte?
|
if saved_change_to_state? && en_instruction? && !procedure.declarative_accepte?
|
||||||
NotificationMailer.send_dossier_received(self).deliver_later
|
NotificationMailer.send_dossier_received(self).deliver_later
|
||||||
|
|
|
@ -194,7 +194,7 @@ describe Instructeurs::DossiersController, type: :controller do
|
||||||
describe '#terminer' do
|
describe '#terminer' do
|
||||||
context "with refuser" do
|
context "with refuser" do
|
||||||
before do
|
before do
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
sign_in(instructeur.user)
|
sign_in(instructeur.user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -235,7 +235,7 @@ describe Instructeurs::DossiersController, type: :controller do
|
||||||
|
|
||||||
context "with classer_sans_suite" do
|
context "with classer_sans_suite" do
|
||||||
before do
|
before do
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
sign_in(instructeur.user)
|
sign_in(instructeur.user)
|
||||||
end
|
end
|
||||||
context 'without attachment' do
|
context 'without attachment' do
|
||||||
|
@ -277,7 +277,7 @@ describe Instructeurs::DossiersController, type: :controller do
|
||||||
|
|
||||||
context "with accepter" do
|
context "with accepter" do
|
||||||
before do
|
before do
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
sign_in(instructeur.user)
|
sign_in(instructeur.user)
|
||||||
|
|
||||||
expect(NotificationMailer).to receive(:send_closed_notification)
|
expect(NotificationMailer).to receive(:send_closed_notification)
|
||||||
|
|
|
@ -657,7 +657,7 @@ describe Users::DossiersController, type: :controller do
|
||||||
let!(:invite) { create(:invite, dossier: dossier, user: user) }
|
let!(:invite) { create(:invite, dossier: dossier, user: user) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
dossier.en_construction!
|
dossier.passer_en_construction!
|
||||||
subject
|
subject
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -346,13 +346,14 @@ describe Dossier do
|
||||||
let(:state) { Dossier.states.fetch(:brouillon) }
|
let(:state) { Dossier.states.fetch(:brouillon) }
|
||||||
let(:dossier) { create(:dossier, state: state) }
|
let(:dossier) { create(:dossier, state: state) }
|
||||||
let(:beginning_of_day) { Time.zone.now.beginning_of_day }
|
let(:beginning_of_day) { Time.zone.now.beginning_of_day }
|
||||||
|
let(:instructeur) { create(:instructeur) }
|
||||||
|
|
||||||
before { Timecop.freeze(beginning_of_day) }
|
before { Timecop.freeze(beginning_of_day) }
|
||||||
after { Timecop.return }
|
after { Timecop.return }
|
||||||
|
|
||||||
context 'when dossier is en_construction' do
|
context 'when dossier is en_construction' do
|
||||||
before do
|
before do
|
||||||
dossier.en_construction!
|
dossier.passer_en_construction!
|
||||||
dossier.reload
|
dossier.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -361,8 +362,8 @@ describe Dossier do
|
||||||
|
|
||||||
it 'should keep first en_construction_at date' do
|
it 'should keep first en_construction_at date' do
|
||||||
Timecop.return
|
Timecop.return
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
dossier.en_construction!
|
dossier.repasser_en_construction!(instructeur)
|
||||||
|
|
||||||
expect(dossier.en_construction_at).to eq(beginning_of_day)
|
expect(dossier.en_construction_at).to eq(beginning_of_day)
|
||||||
end
|
end
|
||||||
|
@ -370,9 +371,10 @@ describe Dossier do
|
||||||
|
|
||||||
context 'when dossier is en_instruction' do
|
context 'when dossier is en_instruction' do
|
||||||
let(:state) { Dossier.states.fetch(:en_construction) }
|
let(:state) { Dossier.states.fetch(:en_construction) }
|
||||||
|
let(:instructeur) { create(:instructeur) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
dossier.reload
|
dossier.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -381,39 +383,48 @@ describe Dossier do
|
||||||
|
|
||||||
it 'should keep first en_instruction_at date if dossier is set to en_construction again' do
|
it 'should keep first en_instruction_at date if dossier is set to en_construction again' do
|
||||||
Timecop.return
|
Timecop.return
|
||||||
dossier.en_construction!
|
dossier.repasser_en_construction!(instructeur)
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
|
|
||||||
expect(dossier.en_instruction_at).to eq(beginning_of_day)
|
expect(dossier.en_instruction_at).to eq(beginning_of_day)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples 'dossier is processed' do |new_state|
|
|
||||||
before do
|
|
||||||
dossier.update(state: new_state)
|
|
||||||
dossier.reload
|
|
||||||
end
|
|
||||||
|
|
||||||
it { expect(dossier.state).to eq(new_state) }
|
|
||||||
it { expect(dossier.processed_at).to eq(beginning_of_day) }
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when dossier is accepte' do
|
context 'when dossier is accepte' do
|
||||||
let(:state) { Dossier.states.fetch(:en_instruction) }
|
let(:state) { Dossier.states.fetch(:en_instruction) }
|
||||||
|
|
||||||
it_behaves_like 'dossier is processed', Dossier.states.fetch(:accepte)
|
before do
|
||||||
|
dossier.accepter!(instructeur, nil, nil)
|
||||||
|
dossier.reload
|
||||||
|
end
|
||||||
|
|
||||||
|
it { expect(dossier.state).to eq(Dossier.states.fetch(:accepte)) }
|
||||||
|
it { expect(dossier.processed_at).to eq(beginning_of_day) }
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when dossier is refuse' do
|
context 'when dossier is refuse' do
|
||||||
let(:state) { Dossier.states.fetch(:en_instruction) }
|
let(:state) { Dossier.states.fetch(:en_instruction) }
|
||||||
|
|
||||||
it_behaves_like 'dossier is processed', Dossier.states.fetch(:refuse)
|
before do
|
||||||
|
dossier.refuser!(instructeur, nil, nil)
|
||||||
|
dossier.reload
|
||||||
|
end
|
||||||
|
|
||||||
|
it { expect(dossier.state).to eq(Dossier.states.fetch(:refuse)) }
|
||||||
|
it { expect(dossier.processed_at).to eq(beginning_of_day) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when dossier is sans_suite' do
|
context 'when dossier is sans_suite' do
|
||||||
let(:state) { Dossier.states.fetch(:en_instruction) }
|
let(:state) { Dossier.states.fetch(:en_instruction) }
|
||||||
|
|
||||||
it_behaves_like 'dossier is processed', Dossier.states.fetch(:sans_suite)
|
before do
|
||||||
|
dossier.classer_sans_suite!(instructeur, nil, nil)
|
||||||
|
dossier.reload
|
||||||
|
end
|
||||||
|
|
||||||
|
it { expect(dossier.state).to eq(Dossier.states.fetch(:sans_suite)) }
|
||||||
|
it { expect(dossier.processed_at).to eq(beginning_of_day) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -478,13 +489,14 @@ describe Dossier do
|
||||||
describe "#send_dossier_received" do
|
describe "#send_dossier_received" do
|
||||||
let(:procedure) { create(:procedure) }
|
let(:procedure) { create(:procedure) }
|
||||||
let(:dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_construction)) }
|
let(:dossier) { create(:dossier, procedure: procedure, state: Dossier.states.fetch(:en_construction)) }
|
||||||
|
let(:instructeur) { create(:instructeur) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(NotificationMailer).to receive(:send_dossier_received).and_return(double(deliver_later: nil))
|
allow(NotificationMailer).to receive(:send_dossier_received).and_return(double(deliver_later: nil))
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sends an email when the dossier becomes en_instruction" do
|
it "sends an email when the dossier becomes en_instruction" do
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
expect(NotificationMailer).to have_received(:send_dossier_received).with(dossier)
|
expect(NotificationMailer).to have_received(:send_dossier_received).with(dossier)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -777,6 +789,7 @@ describe Dossier do
|
||||||
|
|
||||||
describe 'webhook' do
|
describe 'webhook' do
|
||||||
let(:dossier) { create(:dossier) }
|
let(:dossier) { create(:dossier) }
|
||||||
|
let(:instructeur) { create(:instructeur) }
|
||||||
|
|
||||||
it 'should not call webhook' do
|
it 'should not call webhook' do
|
||||||
expect {
|
expect {
|
||||||
|
@ -792,7 +805,7 @@ describe Dossier do
|
||||||
}.to_not have_enqueued_job(WebHookJob)
|
}.to_not have_enqueued_job(WebHookJob)
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
dossier.en_construction!
|
dossier.passer_en_construction!
|
||||||
}.to have_enqueued_job(WebHookJob)
|
}.to have_enqueued_job(WebHookJob)
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
|
@ -800,7 +813,7 @@ describe Dossier do
|
||||||
}.to_not have_enqueued_job(WebHookJob)
|
}.to_not have_enqueued_job(WebHookJob)
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
dossier.en_instruction!
|
dossier.passer_en_instruction!(instructeur)
|
||||||
}.to have_enqueued_job(WebHookJob)
|
}.to have_enqueued_job(WebHookJob)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue