diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index d9582eeda..3273384ee 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -115,20 +115,25 @@ class Backoffice::DossiersController < Backoffice::DossiersListController case params[:process_action] when "refuse" - next_step = "refuse" + dossier.refuse! notice = "Dossier considéré comme refusé." template = dossier.procedure.refused_mail_template when "without_continuation" - next_step = "without_continuation" + dossier.sans_suite! notice = "Dossier considéré comme sans suite." template = dossier.procedure.without_continuation_mail_template when "close" - next_step = "close" + dossier.attestation = dossier.build_attestation + dossier.accepte! notice = "Dossier traité avec succès." template = dossier.procedure.closed_mail_template end - dossier.next_step! 'gestionnaire', next_step, motivation + if motivation + dossier.motivation = motivation + end + + dossier.save # needed to force Carrierwave to provide dossier.attestation.pdf.read # when the Feature.remote_storage is true, otherwise pdf.read is a closed stream. diff --git a/app/controllers/commentaires_controller.rb b/app/controllers/commentaires_controller.rb index 75a11fa05..f59bed25c 100644 --- a/app/controllers/commentaires_controller.rb +++ b/app/controllers/commentaires_controller.rb @@ -19,11 +19,9 @@ class CommentairesController < ApplicationController if is_gestionnaire? @commentaire.email = current_gestionnaire.email @commentaire.dossier = current_gestionnaire.dossiers.find_by(id: dossier_id) || current_gestionnaire.avis.find_by!(dossier_id: dossier_id).dossier - @commentaire.dossier.next_step! 'gestionnaire', 'comment' else @commentaire.email = current_user.email @commentaire.dossier = current_user.dossiers.find_by(id: dossier_id) || current_user.invites.find_by!(dossier_id: dossier_id).dossier - @commentaire.dossier.next_step! 'user', 'comment' if current_user.email == @commentaire.dossier.user.email end @commentaire.file = params["file"] diff --git a/app/controllers/new_gestionnaire/dossiers_controller.rb b/app/controllers/new_gestionnaire/dossiers_controller.rb index 7b4bb8140..1041bc2e4 100644 --- a/app/controllers/new_gestionnaire/dossiers_controller.rb +++ b/app/controllers/new_gestionnaire/dossiers_controller.rb @@ -37,7 +37,6 @@ module NewGestionnaire def follow current_gestionnaire.follow(dossier) - dossier.next_step!('gestionnaire', 'follow') flash.notice = 'Dossier suivi' redirect_back(fallback_location: procedures_url) end @@ -82,20 +81,25 @@ module NewGestionnaire case params[:process_action] when "refuser" - next_step = "refuse" + dossier.refuse! notice = "Dossier considéré comme refusé." template = procedure.refused_mail_template when "classer_sans_suite" - next_step = "without_continuation" + dossier.sans_suite! notice = "Dossier considéré comme sans suite." template = procedure.without_continuation_mail_template when "accepter" - next_step = "close" + dossier.attestation = dossier.build_attestation + dossier.accepte! notice = "Dossier traité avec succès." template = procedure.closed_mail_template end - dossier.next_step!('gestionnaire', next_step, motivation) + if motivation + dossier.motivation = motivation + end + + dossier.save # needed to force Carrierwave to provide dossier.attestation.pdf.read # when the Feature.remote_storage is true, otherwise pdf.read is a closed stream. diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 70c95f5fd..374aecbd4 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -83,7 +83,6 @@ class Users::DescriptionController < UsersController else flash.notice = 'Nouveaux fichiers envoyés' if flash.alert.nil? - @dossier.next_step! 'user', 'update' end return redirect_to users_dossiers_invite_path(id: current_user.invites.find_by_dossier_id(@dossier.id).id) if invite diff --git a/app/controllers/users/recapitulatif_controller.rb b/app/controllers/users/recapitulatif_controller.rb index 6d4aac291..bdd1df237 100644 --- a/app/controllers/users/recapitulatif_controller.rb +++ b/app/controllers/users/recapitulatif_controller.rb @@ -10,7 +10,7 @@ class Users::RecapitulatifController < UsersController def initiate create_dossier_facade - @facade.dossier.next_step! 'user', 'initiate' + @facade.dossier.en_construction! flash.notice = 'Dossier soumis avec succès.' redirect_to users_dossier_recapitulatif_path diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 7842a5a00..e3936ae4d 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -144,61 +144,6 @@ class Dossier < ActiveRecord::Base champs.joins(', types_de_piece_justificative').where("pieces_justificatives.type_de_piece_justificative_id = types_de_piece_justificative.id AND types_de_piece_justificative.procedure_id = #{procedure.id}").order('order_place ASC') end - def next_step! role, action, motivation = nil - unless %w(initiate follow update comment receive refuse without_continuation close).include?(action) - fail 'action is not valid' - end - - unless %w(user gestionnaire).include?(role) - fail 'role is not valid' - end - - case role - when 'user' - case action - when 'initiate' - if brouillon? - en_construction! - end - end - when 'gestionnaire' - case action - when 'close' - if en_instruction? - self.attestation = build_attestation - save - - accepte! - - if motivation - self.motivation = motivation - save - end - end - when 'refuse' - if en_instruction? - refuse! - - if motivation - self.motivation = motivation - save - end - end - when 'without_continuation' - if en_instruction? - sans_suite! - - if motivation - self.motivation = motivation - save - end - end - end - end - - state - end - def brouillon? BROUILLON.include?(state) end @@ -379,14 +324,14 @@ class Dossier < ActiveRecord::Base end end - private - def build_attestation if procedure.attestation_template.present? && procedure.attestation_template.activated? procedure.attestation_template.attestation_for(self) end end + private + def update_state_dates if en_construction? && !self.en_construction_at self.en_construction_at = DateTime.now diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 6bf3a1431..71738ce3a 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -32,7 +32,6 @@ describe NewGestionnaire::DossiersController, type: :controller do describe '#follow' do before do - expect_any_instance_of(Dossier).to receive(:next_step!).with('gestionnaire', 'follow') patch :follow, params: { procedure_id: procedure.id, dossier_id: dossier.id } end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 9359525d4..c0b468df5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -116,200 +116,6 @@ describe Dossier do end end end - - describe '#next_step' do - let(:dossier) { create(:dossier) } - let(:role) { 'user' } - let(:action) { 'initiate' } - - subject { dossier.next_step! role, action } - - context 'when action is not valid' do - let(:action) { 'test' } - it { expect { subject }.to raise_error('action is not valid') } - end - - context 'when role is not valid' do - let(:role) { 'test' } - it { expect { subject }.to raise_error('role is not valid') } - end - - context 'when dossier is at state brouillon' do - before do - dossier.brouillon! - end - - context 'when user is connected' do - let(:role) { 'user' } - - context 'when he updates dossier informations' do - let(:action) { 'update' } - - it { is_expected.to eq('brouillon') } - end - - context 'when he posts a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('brouillon') } - end - - context 'when he initiate a dossier' do - let(:action) { 'initiate' } - - it { is_expected.to eq('en_construction') } - end - end - end - - context 'when dossier is at state en_construction' do - before do - dossier.en_construction! - end - - context 'when user is connect' do - let(:role) { 'user' } - - context 'when is update dossier informations' do - let(:action) { 'update' } - - it { is_expected.to eq('en_construction') } - end - - context 'when is post a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('en_construction') } - end - end - - context 'when gestionnaire is connect' do - let(:role) { 'gestionnaire' } - - context 'when is post a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('en_construction') } - end - - context 'when is follow' do - let(:action) { 'follow' } - - it { is_expected.to eq 'en_construction' } - end - end - end - - context 'when dossier is at state en_instruction' do - before do - dossier.en_instruction! - end - - context 'when user is connected' do - let(:role) { 'user' } - - context 'when he posts a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('en_instruction') } - end - end - - context 'when gestionnaire is connect' do - let(:role) { 'gestionnaire' } - - context 'when he posts a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('en_instruction') } - end - - context 'when he closes the dossier' do - let(:action) { 'close' } - - it { is_expected.to eq('accepte') } - end - end - end - - context 'when dossier is at state refuse' do - before do - dossier.refuse! - end - - context 'when user is connected' do - let(:role) { 'user' } - - context 'when he posts a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('refuse') } - end - end - - context 'when gestionnaire is connect' do - let(:role) { 'gestionnaire' } - - context 'when he posts a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('refuse') } - end - end - end - - context 'when dossier is at state sans_suite' do - before do - dossier.sans_suite! - end - - context 'when user is connected' do - let(:role) { 'user' } - - context 'when he posts a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('sans_suite') } - end - end - - context 'when gestionnaire is connect' do - let(:role) { 'gestionnaire' } - - context 'when he posts a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('sans_suite') } - end - end - end - - context 'when dossier is at state accepte' do - before do - dossier.accepte! - end - - context 'when user is connect' do - let(:role) { 'user' } - - context 'when is post a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('accepte') } - end - end - - context 'when gestionnaire is connect' do - let(:role) { 'gestionnaire' } - - context 'when is post a comment' do - let(:action) { 'comment' } - - it { is_expected.to eq('accepte') } - end - end - end - end end describe '#cerfa_available?' do @@ -818,7 +624,7 @@ describe Dossier do let(:procedure) { create(:procedure, attestation_template: attestation_template) } before :each do - dossier.next_step!('gestionnaire', 'close') + dossier.attestation = dossier.build_attestation dossier.reload end