Add state dates to dossier

This commit is contained in:
Mathieu Magnin 2017-03-01 09:51:55 +01:00
parent 67daaa033c
commit d9f5603f05
4 changed files with 90 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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