Merge pull request #31 from sgmap/add_dossier_workflow_dates
Add state dates to dossier
This commit is contained in:
commit
4d9732b63a
6 changed files with 124 additions and 6 deletions
|
@ -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? }
|
||||
|
||||
|
@ -308,4 +310,17 @@ class Dossier < ActiveRecord::Base
|
|||
def can_be_initiated?
|
||||
!(procedure.archived && draft?)
|
||||
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
|
||||
|
|
|
@ -4,7 +4,10 @@ class DossierProcedureSerializer < ActiveModel::Serializer
|
|||
:updated_at,
|
||||
:archived,
|
||||
:mandataire_social,
|
||||
:state
|
||||
:state,
|
||||
:initiated_at,
|
||||
:received_at,
|
||||
:processed_at
|
||||
|
||||
attribute :followers_gestionnaires_emails, key: :emails_accompagnateurs
|
||||
end
|
||||
|
|
7
db/migrate/20170228144909_add_state_dates_to_dossiers.rb
Normal file
7
db/migrate/20170228144909_add_state_dates_to_dossiers.rb
Normal 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
|
|
@ -0,0 +1,8 @@
|
|||
class RemoveDepositDatetimeFromDossiers < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
Dossier.where.not(deposit_datetime: nil).each do |dossier|
|
||||
dossier.update(initiated_at: dossier.deposit_datetime)
|
||||
end
|
||||
remove_column :dossiers, :deposit_datetime, :datetime
|
||||
end
|
||||
end
|
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20170228150522) do
|
||||
ActiveRecord::Schema.define(version: 20170302112312) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -126,7 +126,9 @@ ActiveRecord::Schema.define(version: 20170228150522) do
|
|||
t.text "json_latlngs"
|
||||
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
|
||||
|
|
|
@ -597,7 +597,10 @@ describe Dossier do
|
|||
let(:procedure) { create(:procedure, :with_type_de_champ) }
|
||||
let(:gestionnaire) { create(:gestionnaire) }
|
||||
let(:follow) { create(:follow, gestionnaire: gestionnaire) }
|
||||
let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure, follows: [follow]) }
|
||||
let(:date1) { 1.day.ago }
|
||||
let(:date2) { 1.hour.ago }
|
||||
let(:date3) { 1.minute.ago }
|
||||
let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure, follows: [follow], initiated_at: date1, received_at: date2, processed_at: date3) }
|
||||
|
||||
describe '#export_headers' do
|
||||
|
||||
|
@ -608,6 +611,7 @@ describe Dossier do
|
|||
end
|
||||
|
||||
describe '#data_with_champs' do
|
||||
|
||||
subject { dossier.data_with_champs }
|
||||
|
||||
it { expect(subject[0]).to be_a_kind_of(Integer) }
|
||||
|
@ -616,14 +620,17 @@ describe Dossier do
|
|||
it { expect(subject[3]).to be_in([true, false]) }
|
||||
it { expect(subject[4]).to be_in([true, false]) }
|
||||
it { expect(subject[5]).to eq("draft") }
|
||||
it { expect(subject[6]).to eq(dossier.followers_gestionnaires_emails) }
|
||||
it { expect(subject[6]).to eq(date1) }
|
||||
it { expect(subject[7]).to eq(date2) }
|
||||
it { expect(subject[8]).to eq(date3) }
|
||||
it { expect(subject[9]).to eq(dossier.followers_gestionnaires_emails) }
|
||||
it { expect(subject.count).to eq(DossierProcedureSerializer.new(dossier).attributes.count + dossier.procedure.types_de_champ.count + dossier.export_entreprise_data.count) }
|
||||
end
|
||||
end
|
||||
|
||||
describe '#Dossier.to_csv' do
|
||||
let!(:procedure) { create(:procedure) }
|
||||
let!(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) }
|
||||
let!(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure, ) }
|
||||
|
||||
subject do
|
||||
dossier_hash = {}
|
||||
|
@ -881,6 +888,82 @@ 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(:beginning_of_day) { Time.now.beginning_of_day }
|
||||
|
||||
before do
|
||||
Timecop.freeze(beginning_of_day)
|
||||
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(beginning_of_day) }
|
||||
|
||||
it 'should keep first initiated_at date' do
|
||||
Timecop.return
|
||||
dossier.received!
|
||||
dossier.initiated!
|
||||
|
||||
expect(dossier.initiated_at).to eq(beginning_of_day)
|
||||
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(beginning_of_day) }
|
||||
|
||||
it 'should keep first received_at date if dossier is set to initiated again' do
|
||||
Timecop.return
|
||||
dossier.initiated!
|
||||
dossier.received!
|
||||
|
||||
expect(dossier.received_at).to eq(beginning_of_day)
|
||||
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 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
|
||||
|
|
Loading…
Reference in a new issue