From ded5a6a28fc627e1832585bbb194c3c53b43b4aa Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 24 Sep 2015 10:27:14 +0200 Subject: [PATCH] add function "next_step" into dossier --- app/models/dossier.rb | 105 +++++++++++---- spec/models/dossier_spec.rb | 253 ++++++++++++++++++++++++++++++++++++ 2 files changed, 332 insertions(+), 26 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 7c4e1ee1a..94518ee01 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1,11 +1,11 @@ class Dossier < ActiveRecord::Base enum state: { draft: 'draft', - submitted: 'submitted', - reply: 'reply', - updated: 'updated', - confirmed: 'confirmed', - filed: 'filed', - processed: 'processed' } + proposed: 'proposed', + reply: 'reply', + updated: 'updated', + confirmed: 'confirmed', + deposited: 'deposited', + processed: 'processed' } has_one :etablissement has_one :entreprise @@ -29,28 +29,81 @@ class Dossier < ActiveRecord::Base validates :date_previsionnelle, presence: true, allow_blank: false, unless: Proc.new { description.nil? } - def retrieve_piece_justificative_by_type(type) - pieces_justificatives.where(type_de_piece_justificative_id: type).last - end + def retrieve_piece_justificative_by_type(type) + pieces_justificatives.where(type_de_piece_justificative_id: type).last + end - def build_default_pieces_justificatives - procedure.types_de_piece_justificative.each do |type_de_piece_justificative| - PieceJustificative.create(type_de_piece_justificative_id: type_de_piece_justificative.id, dossier_id: id) - end - end + def build_default_pieces_justificatives + procedure.types_de_piece_justificative.each do |type_de_piece_justificative| + PieceJustificative.create(type_de_piece_justificative_id: type_de_piece_justificative.id, dossier_id: id) + end + end - def sous_domaine - if Rails.env.production? - 'tps' - else - 'tps-dev' - end - end + def sous_domaine + if Rails.env.production? + 'tps' + else + 'tps-dev' + end + end - private + def next_step! role, action + unless ['propose', 'reply', 'update', 'comment', 'confirme', 'depose', 'process'].include?(action) + fail 'action is not valid' + end - def build_default_cerfa - build_cerfa - true - end + unless ['user', 'gestionnaire'].include?(role) + fail 'role is not valid' + end + + if role == 'user' + case action + when 'propose' + if draft? + proposed! + end + when 'depose' + if confirmed? + deposited! + end + when 'update' + if reply? + updated! + end + when 'comment' + if reply? + updated! + end + end + elsif role == 'gestionnaire' + case action + when 'comment' + if updated? + reply! + elsif proposed? + reply! + end + when 'confirme' + if updated? + confirmed! + elsif reply? + confirmed! + elsif proposed? + confirmed! + end + when 'process' + if deposited? + processed! + end + end + end + state + end + + private + + def build_default_cerfa + build_cerfa + true + end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 7814c87ff..eda807c07 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -115,5 +115,258 @@ describe Dossier do end end end + + #TODO revoir le nommage + describe '#next_step' do + let(:dossier) { create(:dossier) } + let(:role) { 'user' } + let(:action) { 'propose' } + + 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 draft' do + before do + dossier.draft! + 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('draft') } + end + + context 'when he posts a comment' do + let(:action) {'comment'} + + it { is_expected.to eq('draft') } + end + + context 'when he proposes a dossier' do + let(:action) { 'propose' } + + it { is_expected.to eq('proposed') } + end + end + end + + context 'when dossier is at state proposed' do + before do + dossier.proposed! + 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('proposed')} + end + + context 'when is post a comment' do + let(:action) { 'comment' } + + it {is_expected.to eq('proposed')} + 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('reply')} + end + + context 'when is confirmed the dossier' do + let(:action) { 'confirme' } + + it {is_expected.to eq('confirmed')} + end + end + end + + context 'when dossier is at state reply' do + before do + dossier.reply! + 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('updated') } + end + + context 'when is updated dossier informations' do + let(:action) { 'update' } + + it { + + is_expected.to eq('updated') + } + 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('reply')} + end + + context 'when is confirmed the dossier' do + let(:action) { 'confirme' } + + it {is_expected.to eq('confirmed')} + end + end + end + + context 'when dossier is at state updated' do + before do + dossier.updated! + 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('updated')} + end + + context 'when is updated dossier informations' do + let(:action) { 'update' } + + it { is_expected.to eq('updated')} + 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('reply')} + end + + context 'when is confirmed the dossier' do + let(:action) { 'confirme' } + + it {is_expected.to eq('confirmed')} + end + end + end + + context 'when dossier is at state confirmed' do + before do + dossier.confirmed! + 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('confirmed') } + end + + context 'when is deposed the dossier' do + let(:action) { 'depose' } + + it { is_expected.to eq('deposited') } + 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('confirmed')} + end + end + end + + context 'when dossier is at state deposited' do + before do + dossier.deposited! + 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('deposited') } + 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('deposited')} + end + + context 'when is processed the dossier' do + let(:action) { 'process' } + + it {is_expected.to eq('processed')} + end + end + end + + context 'when dossier is at state processed' do + before do + dossier.processed! + 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('processed')} + 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('processed')} + end + end + end + end end end