diff --git a/app/models/gestionnaire.rb b/app/models/gestionnaire.rb index c944d93a6..6836c06c3 100644 --- a/app/models/gestionnaire.rb +++ b/app/models/gestionnaire.rb @@ -20,6 +20,12 @@ class Gestionnaire < ActiveRecord::Base @dossiers_follow ||= dossiers.joins(:follows).where("follows.gestionnaire_id = #{id}") end + def procedure_filter + return nil unless assign_to.pluck(:procedure_id).include?(self[:procedure_filter]) + + self[:procedure_filter] + end + def toggle_follow_dossier dossier_id dossier = dossier_id dossier = Dossier.find(dossier_id) unless dossier_id.class == Dossier @@ -64,7 +70,7 @@ class Gestionnaire < ActiveRecord::Base end def notifications - Notification.where(already_read: false, dossier_id: follows.pluck(:dossier_id) ).order("updated_at DESC") + Notification.where(already_read: false, dossier_id: follows.pluck(:dossier_id)).order("updated_at DESC") end def notifications_for procedure diff --git a/spec/controllers/backoffice/dossiers_controller_spec.rb b/spec/controllers/backoffice/dossiers_controller_spec.rb index c1529378d..1835b572c 100644 --- a/spec/controllers/backoffice/dossiers_controller_spec.rb +++ b/spec/controllers/backoffice/dossiers_controller_spec.rb @@ -26,6 +26,43 @@ describe Backoffice::DossiersController, type: :controller do context 'when gestionnaire is assign to a procedure' do it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: procedure.id) } + + context 'when gestionnaire is assign to many proceudure' do + before do + create :assign_to, procedure: create(:procedure), gestionnaire: gestionnaire + create :assign_to, procedure: create(:procedure), gestionnaire: gestionnaire + end + + it { expect(gestionnaire.procedures.count).to eq 3 } + + context 'when gestionnaire procedure_filter is nil' do + it { expect(gestionnaire.procedure_filter).to be_nil } + it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: gestionnaire.procedures.order('libelle ASC').first.id) } + end + + context 'when gestionnaire procedure_filter is not nil' do + let(:procedure_filter_id) { gestionnaire.procedures.order('libelle ASC').last.id } + + before do + gestionnaire.update_column :procedure_filter, procedure_filter_id + end + + context 'when gestionnaire is assign_to the procedure filter id' do + it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: procedure_filter_id) } + end + + context 'when gestionnaire not any more assign_to the procedure filter id' do + before do + AssignTo.where(procedure: procedure_filter_id, gestionnaire: gestionnaire).delete_all + end + + it { expect(gestionnaire.procedure_filter).to be_nil } + it { expect(AssignTo.where(procedure: procedure_filter_id, gestionnaire: gestionnaire).count).to eq 0 } + + it { is_expected.to redirect_to backoffice_dossiers_procedure_path(id: gestionnaire.procedures.order('libelle ASC').first.id) } + end + end + end end context 'when gestionnaire is not assign to a procedure' do diff --git a/spec/models/gestionnaire_spec.rb b/spec/models/gestionnaire_spec.rb index e61990c7b..1d1ed1320 100644 --- a/spec/models/gestionnaire_spec.rb +++ b/spec/models/gestionnaire_spec.rb @@ -4,11 +4,12 @@ describe Gestionnaire, type: :model do let(:admin) { create :administrateur } let!(:procedure) { create :procedure, administrateur: admin } let!(:procedure_2) { create :procedure, administrateur: admin } + let!(:procedure_3) { create :procedure, administrateur: admin } let(:gestionnaire) { create :gestionnaire, procedure_filter: procedure_filter, administrateurs: [admin] } - let(:procedure_filter) { [] } + let(:procedure_filter) { nil } + let!(:procedure_assign) { create :assign_to, gestionnaire: gestionnaire, procedure: procedure } before do - create :assign_to, gestionnaire: gestionnaire, procedure: procedure create :assign_to, gestionnaire: gestionnaire, procedure: procedure_2 end @@ -244,4 +245,33 @@ describe Gestionnaire, type: :model do subject } end end + + describe '#procedure_filter' do + subject { gestionnaire.procedure_filter } + + context 'when procedure_filter_id is nil' do + it { is_expected.to eq nil } + end + + context 'when procedure_filter is not nil' do + context 'when gestionnaire is assign_to the procedure filter id' do + before do + gestionnaire.update_column :procedure_filter, procedure.id + end + + it { expect(AssignTo.where(gestionnaire: gestionnaire, procedure: procedure).count).to eq 1 } + it { is_expected.to eq procedure_assign.procedure.id } + end + + context 'when gestionnaire is not any more assign to the procedure filter id' do + before do + gestionnaire.update_column :procedure_filter, procedure_3.id + end + + it { expect(AssignTo.where(gestionnaire: gestionnaire, procedure: procedure_3).count).to eq 0 } + it { is_expected.to be_nil } + end + end + + end end