From 62a0b84048bbbc89a2cc52410c48f713a9fc12f1 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 8 Sep 2016 16:21:54 +0200 Subject: [PATCH] Filter by procedure is activated on route /backoffice/dossiers/procedure/:id --- .../admin/accompagnateurs_controller.rb | 8 +- .../backoffice/dossiers_controller.rb | 8 +- .../preference_list_dossier_controller.rb | 19 +++- app/facades/dossiers_list_facades.rb | 8 ++ app/models/preference_list_dossier.rb | 1 + app/models/procedure.rb | 2 + app/services/accompagnateur_service.rb | 12 +++ .../dossiers_list_gestionnaire_service.rb | 2 +- app/views/backoffice/dossiers/_list.html.haml | 4 +- .../backoffice/dossiers/_pref_list.html.haml | 4 +- .../backoffice/dossiers/_pref_list.js.erb | 4 +- ..._add_has_procedure_to_pref_list_dossier.rb | 5 + ...ild_default_pref_list_dossier_procedure.rb | 44 +++++++++ db/schema.rb | 3 +- .../admin/accompagnateurs_controller_spec.rb | 6 +- ...preference_list_dossier_controller_spec.rb | 1 + spec/facades/dossiers_list_facades_spec.rb | 39 ++++++++ ...st_dossier_by_procedure_backoffice_spec.rb | 92 +++++++++++++++++++ spec/models/preference_list_dossier_spec.rb | 1 + spec/models/procedure_spec.rb | 1 + spec/services/accompagnateur_service_spec.rb | 38 +++++++- 21 files changed, 287 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20160906123255_add_has_procedure_to_pref_list_dossier.rb create mode 100644 db/migrate/20160906134155_build_default_pref_list_dossier_procedure.rb create mode 100644 spec/facades/dossiers_list_facades_spec.rb create mode 100644 spec/features/backoffice/lateral_page_pref_list_dossier_by_procedure_backoffice_spec.rb diff --git a/app/controllers/admin/accompagnateurs_controller.rb b/app/controllers/admin/accompagnateurs_controller.rb index 83f9a6c94..ef47687a8 100644 --- a/app/controllers/admin/accompagnateurs_controller.rb +++ b/app/controllers/admin/accompagnateurs_controller.rb @@ -23,7 +23,13 @@ class Admin::AccompagnateursController < AdminController end def update - AccompagnateurService.change_assignement! Gestionnaire.find(params[:accompagnateur_id]), Procedure.find(params[:procedure_id]), params[:to] + gestionnaire = Gestionnaire.find(params[:accompagnateur_id]) + procedure = Procedure.find(params[:procedure_id]) + to = params[:to] + + + AccompagnateurService.change_assignement! gestionnaire, procedure, to + AccompagnateurService.build_default_column gestionnaire, procedure, to flash.notice = "Assignement effectué" redirect_to admin_procedure_accompagnateurs_path, procedure_id: params[:procedure_id] diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index dde387679..6918c4653 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -123,7 +123,7 @@ class Backoffice::DossiersController < ApplicationController end def create_dossiers_list_facade liste='a_traiter' - @dossiers_list_facade = DossiersListFacades.new current_gestionnaire, liste + @dossiers_list_facade = DossiersListFacades.new current_gestionnaire, liste, retrieve_procedure end def create_dossier_facade dossier_id @@ -133,4 +133,10 @@ class Backoffice::DossiersController < ApplicationController flash.alert = t('errors.messages.dossier_not_found') redirect_to url_for(controller: '/backoffice') end + + + def retrieve_procedure + return if params[:procedure_id].blank? + current_gestionnaire.procedures.find params[:procedure_id] + end end diff --git a/app/controllers/backoffice/preference_list_dossier_controller.rb b/app/controllers/backoffice/preference_list_dossier_controller.rb index 034ff1fb1..0b9e564b3 100644 --- a/app/controllers/backoffice/preference_list_dossier_controller.rb +++ b/app/controllers/backoffice/preference_list_dossier_controller.rb @@ -3,6 +3,7 @@ class Backoffice::PreferenceListDossierController < ApplicationController helper SmartListing::Helper before_action :authenticate_gestionnaire! + before_action :params_procedure_id def add PreferenceListDossier.create( @@ -13,14 +14,17 @@ class Backoffice::PreferenceListDossierController < ApplicationController bootstrap_lg: params[:bootstrap_lg], order: nil, filter: nil, - gestionnaire: current_gestionnaire + gestionnaire: current_gestionnaire, + procedure_id: params_procedure_id ) render partial: 'backoffice/dossiers/pref_list', formats: :js end def reload_pref_list - render partial: 'backoffice/dossiers/pref_list' + @dossiers_list_facade = DossiersListFacades.new current_gestionnaire, '', retrieve_procedure + + render partial: 'backoffice/dossiers/pref_list', id: params_procedure_id end def delete @@ -28,4 +32,15 @@ class Backoffice::PreferenceListDossierController < ApplicationController render partial: 'backoffice/dossiers/pref_list', formats: :js end + + private + + def params_procedure_id + @procedure_id ||= params[:procedure_id] + end + + def retrieve_procedure + return if params[:procedure_id].blank? + current_gestionnaire.procedures.find params_procedure_id + end end diff --git a/app/facades/dossiers_list_facades.rb b/app/facades/dossiers_list_facades.rb index 0ef6a6baf..39ee69c4d 100644 --- a/app/facades/dossiers_list_facades.rb +++ b/app/facades/dossiers_list_facades.rb @@ -19,10 +19,18 @@ class DossiersListFacades @liste end + def procedure_id + @procedure.nil? ? nil : @procedure.id + end + def dossiers_to_display service.dossiers_to_display end + def preference_list_dossiers_filter + @list_table_columns ||= @current_devise_profil.preference_list_dossiers.where(procedure: @procedure).order(:id) + end + def nouveaux_class (@liste == 'nouveaux' ? 'active' : '') end diff --git a/app/models/preference_list_dossier.rb b/app/models/preference_list_dossier.rb index a6a57f985..cc0a00a4f 100644 --- a/app/models/preference_list_dossier.rb +++ b/app/models/preference_list_dossier.rb @@ -1,5 +1,6 @@ class PreferenceListDossier < ActiveRecord::Base belongs_to :gestionnaire + belongs_to :procedure def table_attr return self.attr if table.nil? || table.empty? diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 317f70f44..2489f53d2 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -15,6 +15,8 @@ class Procedure < ActiveRecord::Base has_many :assign_to, dependent: :destroy has_many :gestionnaires, through: :assign_to + has_many :preference_list_dossiers + delegate :use_api_carto, to: :module_api_carto accepts_nested_attributes_for :types_de_champ, :reject_if => proc { |attributes| attributes['libelle'].blank? }, :allow_destroy => true diff --git a/app/services/accompagnateur_service.rb b/app/services/accompagnateur_service.rb index 119560418..8a254e06f 100644 --- a/app/services/accompagnateur_service.rb +++ b/app/services/accompagnateur_service.rb @@ -9,4 +9,16 @@ class AccompagnateurService AssignTo.delete_all(gestionnaire: accompagnateur, procedure: procedure) end end + + def self.build_default_column accompagnateur, procedure, to + return unless to == ASSIGN + return unless PreferenceListDossier.where(gestionnaire: accompagnateur, procedure: procedure).empty? + + accompagnateur.preference_list_dossiers.each do |pref| + clone = pref.dup + + clone.procedure = procedure + clone.save + end + end end \ No newline at end of file diff --git a/app/services/dossiers_list_gestionnaire_service.rb b/app/services/dossiers_list_gestionnaire_service.rb index a4ca81d98..6e3cda519 100644 --- a/app/services/dossiers_list_gestionnaire_service.rb +++ b/app/services/dossiers_list_gestionnaire_service.rb @@ -39,6 +39,6 @@ class DossiersListGestionnaireService end def filter_dossiers - @procedure.nil? ? @current_devise_profil.dossiers : @procedure.dossiers + @filter_dossiers ||= @procedure.nil? ? @current_devise_profil.dossiers : @procedure.dossiers end end \ No newline at end of file diff --git a/app/views/backoffice/dossiers/_list.html.haml b/app/views/backoffice/dossiers/_list.html.haml index 8add886a6..64390423b 100644 --- a/app/views/backoffice/dossiers/_list.html.haml +++ b/app/views/backoffice/dossiers/_list.html.haml @@ -1,14 +1,14 @@ - unless smart_listing.empty? %table.table %thead - - current_gestionnaire.preference_list_dossiers.order(:id).each do |preference| + - @dossiers_list_facade.preference_list_dossiers_filter.each do |preference| %th{class: "col-md-#{preference.bootstrap_lg} col-lg-#{preference.bootstrap_lg}"}= smart_listing.sortable preference.libelle, preference.table_attr %th.col-md-1.col-lg-1.center Actions %th.col-md-1.col-lg-1.center Abonnés - @dossiers.each do |dossier| %tr - - current_gestionnaire.preference_list_dossiers.order(:id).each_with_index do |preference, index| + - @dossiers_list_facade.preference_list_dossiers_filter.each_with_index do |preference, index| %td - if preference.table.nil? || preference.table.empty? - value = dossier.decorate.public_send(preference.attr_decorate) diff --git a/app/views/backoffice/dossiers/_pref_list.html.haml b/app/views/backoffice/dossiers/_pref_list.html.haml index e802bb638..30731efcb 100644 --- a/app/views/backoffice/dossiers/_pref_list.html.haml +++ b/app/views/backoffice/dossiers/_pref_list.html.haml @@ -9,10 +9,11 @@ Actuelles %ul - - current_gestionnaire.preference_list_dossiers.order(:id).each_with_index do |preference, index| + - @dossiers_list_facade.preference_list_dossiers_filter.each_with_index do |preference, index| %li = form_tag backoffice_preference_list_dossier_delete_path, method: :delete, remote: true do = hidden_field_tag :pref_id, preference.id + = hidden_field_tag :procedure_id, preference.procedure_id = preference.libelle %button.btn.btn-default.btn-xs{type: :submit, id: "delete_pref_list_#{preference[:table]}_#{preference[:attr]}"} %i.fa.fa-minus @@ -36,6 +37,7 @@ = hidden_field_tag :attr, columns.second[:attr] = hidden_field_tag :attr_decorate, columns.second[:attr_decorate] = hidden_field_tag :bootstrap_lg, columns.second[:bootstrap_lg] + = hidden_field_tag :procedure_id, @dossiers_list_facade.procedure_id = columns.second[:libelle] %button.btn.btn-default.btn-xs{type: :submit, id: "add_pref_list_#{columns.second[:table]}_#{columns.second[:attr]}"} diff --git a/app/views/backoffice/dossiers/_pref_list.js.erb b/app/views/backoffice/dossiers/_pref_list.js.erb index 2d421e10f..e15e3f068 100644 --- a/app/views/backoffice/dossiers/_pref_list.js.erb +++ b/app/views/backoffice/dossiers/_pref_list.js.erb @@ -1,12 +1,12 @@ $.ajax({ method: 'get', - url: '/backoffice/preference_list_dossier/reload_smartlisting', + url: '/backoffice/preference_list_dossier/reload_smartlisting?procedure_id=<%= @procedure_id %>', async: true }); $.ajax({ methd: 'get', - url: '/backoffice/preference_list_dossier/reload_pref_list', + url: '/backoffice/preference_list_dossier/reload_pref_list?procedure_id=<%= @procedure_id %>', async: true }).done(function (data) { $("#pref_list_menu").html(data); diff --git a/db/migrate/20160906123255_add_has_procedure_to_pref_list_dossier.rb b/db/migrate/20160906123255_add_has_procedure_to_pref_list_dossier.rb new file mode 100644 index 000000000..e80588537 --- /dev/null +++ b/db/migrate/20160906123255_add_has_procedure_to_pref_list_dossier.rb @@ -0,0 +1,5 @@ +class AddHasProcedureToPrefListDossier < ActiveRecord::Migration + def change + add_belongs_to :preference_list_dossiers, :procedure + end +end diff --git a/db/migrate/20160906134155_build_default_pref_list_dossier_procedure.rb b/db/migrate/20160906134155_build_default_pref_list_dossier_procedure.rb new file mode 100644 index 000000000..bb4545d69 --- /dev/null +++ b/db/migrate/20160906134155_build_default_pref_list_dossier_procedure.rb @@ -0,0 +1,44 @@ +class BuildDefaultPrefListDossierProcedure < ActiveRecord::Migration + class Gestionnaire < ActiveRecord::Base + has_many :assign_to, dependent: :destroy + has_many :procedures, through: :assign_to + has_many :preference_list_dossiers + end + + class PreferenceListDossier < ActiveRecord::Base + belongs_to :gestionnaire + belongs_to :procedure + end + + class AssignTo < ActiveRecord::Base + belongs_to :procedure + belongs_to :gestionnaire + end + + class Procedure < ActiveRecord::Base + has_many :gestionnaires, through: :assign_to + has_many :preference_list_dossiers + end + + def up + Gestionnaire.all.each do |gestionnaire| + gestionnaire.procedures.each do |procedure| + gestionnaire.preference_list_dossiers.where(procedure: nil).each do |preference| + clone = preference.dup + + clone.procedure = procedure + clone.save + end + + base_object = gestionnaire.preference_list_dossiers.where(procedure: nil).size + created_object = gestionnaire.preference_list_dossiers.where(procedure: procedure).size + + raise "ERROR nb object (#{base_object} != #{created_object})" unless created_object == base_object + end + end + end + + def down + PreferenceListDossier.where('procedure_id IS NOT NULL') + end +end diff --git a/db/schema.rb b/db/schema.rb index 3b12345df..7533be06e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160901082824) do +ActiveRecord::Schema.define(version: 20160906134155) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -264,6 +264,7 @@ ActiveRecord::Schema.define(version: 20160901082824) do t.string "order" t.string "filter" t.integer "gestionnaire_id" + t.integer "procedure_id" end create_table "procedure_paths", force: :cascade do |t| diff --git a/spec/controllers/admin/accompagnateurs_controller_spec.rb b/spec/controllers/admin/accompagnateurs_controller_spec.rb index 42889eb1f..285b43feb 100644 --- a/spec/controllers/admin/accompagnateurs_controller_spec.rb +++ b/spec/controllers/admin/accompagnateurs_controller_spec.rb @@ -15,7 +15,7 @@ describe Admin::AccompagnateursController, type: :controller do end describe 'PUT #update' do - subject { put :update, accompagnateur_id: gestionnaire.id ,procedure_id: procedure.id } + subject { put :update, accompagnateur_id: gestionnaire.id ,procedure_id: procedure.id, to: 'assign' } it { expect(subject).to redirect_to admin_procedure_accompagnateurs_path(procedure_id: procedure.id) } @@ -25,6 +25,10 @@ describe Admin::AccompagnateursController, type: :controller do end it { expect(flash[:notice]).to be_present } + + it 'default pref list dossier procedure columns are created' do + expect(procedure.preference_list_dossiers.size).to eq gestionnaire.preference_list_dossiers.where('procedure_id IS NULL').size + end end end end \ No newline at end of file diff --git a/spec/controllers/backoffice/preference_list_dossier_controller_spec.rb b/spec/controllers/backoffice/preference_list_dossier_controller_spec.rb index ecf6c25f0..06ccf9b55 100644 --- a/spec/controllers/backoffice/preference_list_dossier_controller_spec.rb +++ b/spec/controllers/backoffice/preference_list_dossier_controller_spec.rb @@ -36,6 +36,7 @@ describe Backoffice::PreferenceListDossierController, type: :controller do it { expect(last.bootstrap_lg).to eq bootstrap_lg } it { expect(last.order).to be_nil } it { expect(last.filter).to be_nil } + it { expect(last.procedure_id).to be_nil } it { expect(last.gestionnaire).to eq gestionnaire } end end diff --git a/spec/facades/dossiers_list_facades_spec.rb b/spec/facades/dossiers_list_facades_spec.rb new file mode 100644 index 000000000..c701092f8 --- /dev/null +++ b/spec/facades/dossiers_list_facades_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe DossiersListFacades do + + let(:gestionnaire) { create :gestionnaire } + let(:procedure) { create :procedure } + + before do + create :preference_list_dossier, + gestionnaire: gestionnaire, + table: '', + attr: 'state', + attr_decorate: 'display_state' + + create :preference_list_dossier, + gestionnaire: gestionnaire, + table: '', + attr: 'state', + attr_decorate: 'display_state', + procedure_id: procedure.id + end + + describe '#preference_list_dossiers_filter' do + + subject { facade.preference_list_dossiers_filter } + + context 'when procedure is not pasted at the facade' do + let(:facade) { described_class.new gestionnaire, 'nouveaux' } + + it { expect(subject.size).to eq 6 } + end + + context 'when procedure is pasted at the facade' do + let(:facade) { described_class.new gestionnaire, 'nouveaux', procedure } + + it { expect(subject.size).to eq 1 } + end + end +end \ No newline at end of file diff --git a/spec/features/backoffice/lateral_page_pref_list_dossier_by_procedure_backoffice_spec.rb b/spec/features/backoffice/lateral_page_pref_list_dossier_by_procedure_backoffice_spec.rb new file mode 100644 index 000000000..2a0c01147 --- /dev/null +++ b/spec/features/backoffice/lateral_page_pref_list_dossier_by_procedure_backoffice_spec.rb @@ -0,0 +1,92 @@ +require 'spec_helper' + +feature 'usage of pref list dossier lateral panel by procedure', js: true do + + let(:administrateur) { create(:administrateur) } + let(:gestionnaire) { create(:gestionnaire, administrateurs: [administrateur]) } + let(:procedure) { create(:procedure, administrateur: administrateur) } + + before do + create(:dossier, :with_entreprise, procedure: procedure, state: 'updated') + create :assign_to, procedure: procedure, gestionnaire: gestionnaire + visit backoffice_path + end + + scenario 'he is redirected to /gestionnaires/sign_id' do + expect(page).to have_css('#gestionnaire_login') + end + + context 'when user enter good credentials' do + before do + page.find_by_id(:gestionnaire_email).set gestionnaire.email + page.find_by_id(:gestionnaire_password).set gestionnaire.password + page.click_on 'Se connecter' + end + + scenario 'he is redirected to /backoffice/dossiers/' do + expect(page).to have_css('#backoffice_index') + end + + describe 'user navigate to dossiers list by procedure' do + before do + visit backoffice_dossiers_procedure_path(procedure.id) + end + + scenario 'lateral panel is masked' do + expect(page).to have_css('#pref_list_menu', visible: false) + end + + context 'when on click on pref list button' do + before do + page.click_on 'pref_list_dossier_open_action' + end + + scenario 'lateral panel is appeared' do + expect(page).to have_css('#pref_list_menu') + end + + context 'when on click on add attribut button' do + before do + page.click_on 'add_pref_list_entreprise_siren' + end + + scenario 'preference list panel is brought up to date' do + wait_for_ajax + expect(page).to have_css('#delete_pref_list_entreprise_siren') + end + + scenario 'dossier is brought up to date' do + wait_for_ajax + expect(page).to have_selector("a.sortable[data-attr='entreprise.siren']") + end + + context 'when on click on delete attribut button' do + before do + page.click_on 'delete_pref_list_entreprise_siren' + end + + scenario 'preference list panel is brought up to date' do + wait_for_ajax + expect(page).not_to have_css('#delete_pref_list_entreprise_siren') + end + + scenario 'dossier is brought up to date' do + wait_for_ajax + expect(page).not_to have_selector("a.sortable[data-attr='entreprise.siren']") + end + + context 'when on click on close pref list button' do + before do + page.click_on 'pref_list_dossier_close_action' + end + + scenario 'lateral panel is masked' do + expect(page).to have_css('#pref_list_menu', visible: false) + end + end + end + end + end + end + end +end \ No newline at end of file diff --git a/spec/models/preference_list_dossier_spec.rb b/spec/models/preference_list_dossier_spec.rb index e65b377fb..f84082084 100644 --- a/spec/models/preference_list_dossier_spec.rb +++ b/spec/models/preference_list_dossier_spec.rb @@ -11,6 +11,7 @@ describe PreferenceListDossier do it { is_expected.to have_db_column(:gestionnaire_id) } it { is_expected.to belong_to(:gestionnaire) } + it { is_expected.to belong_to(:procedure) } describe '.available_columns' do subject { PreferenceListDossier.available_columns } diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index bd720b25a..2b52bcb13 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -9,6 +9,7 @@ describe Procedure do it { is_expected.to have_one(:mail_received) } it { is_expected.to have_one(:module_api_carto) } it { is_expected.to belong_to(:administrateur) } + it { is_expected.to have_many(:preference_list_dossiers) } end describe 'attributes' do diff --git a/spec/services/accompagnateur_service_spec.rb b/spec/services/accompagnateur_service_spec.rb index 1c0dcf9ab..c939ecf27 100644 --- a/spec/services/accompagnateur_service_spec.rb +++ b/spec/services/accompagnateur_service_spec.rb @@ -1,10 +1,11 @@ require 'spec_helper' describe AccompagnateurService do - describe '#change_assignement!' do - let(:procedure) { create :procedure } - let(:accompagnateur) { create :gestionnaire } + let(:procedure) { create :procedure } + let(:accompagnateur) { create :gestionnaire } + + describe '#change_assignement!' do subject { AccompagnateurService.change_assignement! accompagnateur, procedure, to } @@ -29,4 +30,35 @@ describe AccompagnateurService do it { expect(accompagnateur.procedures).not_to include procedure } end end + + describe '#build_default_column' do + + subject { AccompagnateurService.build_default_column accompagnateur, procedure, to } + + context 'when to is not assign' do + let(:to) { AccompagnateurService::NOT_ASSIGN } + + it { is_expected.to be_nil } + end + + context 'when to is assign' do + let(:to) { AccompagnateurService::ASSIGN } + + context 'when gestionnaire has already preference for this procedure' do + before do + create :preference_list_dossier, gestionnaire: accompagnateur, procedure: procedure + end + + it { is_expected.to be_nil } + end + + context 'when gestionnaire has not preference for this procedure' do + before do + subject + end + + it { expect(accompagnateur.preference_list_dossiers.where('procedure_id IS NULL').size).to eq procedure.preference_list_dossiers.size } + end + end + end end \ No newline at end of file