diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 88acb1cee..3a366d8e6 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -38,6 +38,8 @@ class Dossier < ActiveRecord::Base delegate :types_de_champ, to: :procedure delegate :france_connect_information, to: :user + before_validation :update_state_dates, if: -> { state_changed? } + after_save :build_default_champs, if: Proc.new { procedure_id_changed? } after_save :build_default_individual, if: Proc.new { procedure.for_individual? } @@ -302,4 +304,16 @@ class Dossier < ActiveRecord::Base def invite_by_user? email (invites_user.pluck :email).include? email end + + private + + def update_state_dates + if initiated? && !self.initiated_at + self.initiated_at = DateTime.now + elsif received? && !self.received_at + self.received_at = DateTime.now + elsif TERMINE.include?(state) + self.processed_at = DateTime.now + end + end end diff --git a/db/migrate/20170228144909_add_state_dates_to_dossiers.rb b/db/migrate/20170228144909_add_state_dates_to_dossiers.rb new file mode 100644 index 000000000..b07f022ea --- /dev/null +++ b/db/migrate/20170228144909_add_state_dates_to_dossiers.rb @@ -0,0 +1,7 @@ +class AddStateDatesToDossiers < ActiveRecord::Migration[5.0] + def change + add_column :dossiers, :initiated_at, :datetime + add_column :dossiers, :received_at, :datetime + add_column :dossiers, :processed_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 7c4a70927..d1426d252 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170223170808) do +ActiveRecord::Schema.define(version: 20170228144909) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -127,6 +127,9 @@ ActiveRecord::Schema.define(version: 20170223170808) do t.boolean "archived", default: false t.boolean "mandataire_social", default: false t.datetime "deposit_datetime" + t.datetime "initiated_at" + t.datetime "received_at" + t.datetime "processed_at" t.index ["procedure_id"], name: "index_dossiers_on_procedure_id", using: :btree t.index ["user_id"], name: "index_dossiers_on_user_id", using: :btree end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index ad9b4c19e..80656c2ff 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -881,6 +881,71 @@ describe Dossier do it { is_expected.to eq "#{gestionnaire.email} #{gestionnaire2.email}" } end + end + + describe '#update_state_dates' do + let(:state) { 'draft' } + let(:dossier) { create(:dossier, state: state) } + let(:now) { Time.now.beginning_of_day } + + before do + Timecop.freeze(now) + end + + context 'when dossier is initiated' do + before do + dossier.initiated! + dossier.reload + end + + it { expect(dossier.state).to eq('initiated') } + it { expect(dossier.initiated_at).to eq(now) } + + # it 'should not change initiated_at if it is already set' do + # dossier.initiated_at = 1.day.ago + # expect(dossier.initiated_at).to eq(1.day.ago) + # end + end + + context 'when dossier is received' do + let(:state) { 'initiated' } + + before do + dossier.received! + dossier.reload + end + + it { expect(dossier.state).to eq('received') } + it { expect(dossier.received_at).to eq(now) } + 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(now) } + end + + context 'when dossier is closed' do + let(:state) { 'received' } + + it_behaves_like 'dossier is processed', 'closed' + end + + context 'when dossier is refused' do + let(:state) { 'received' } + + it_behaves_like 'dossier is processed', 'refused' + end + + context 'when dossier is without_continuation' do + let(:state) { 'received' } + + it_behaves_like 'dossier is processed', 'without_continuation' + end end end