diff --git a/app/models/dossier.rb b/app/models/dossier.rb index bbfc893fe..105d19bc4 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -35,7 +35,8 @@ class Dossier < ApplicationRecord has_many :dossier_operation_logs, dependent: :destroy - belongs_to :procedure + belongs_to :groupe_instructeur + has_one :procedure, through: :groupe_instructeur belongs_to :user accepts_nested_attributes_for :champs @@ -167,7 +168,7 @@ class Dossier < ApplicationRecord before_validation :update_state_dates, if: -> { state_changed? } - before_save :build_default_champs, if: Proc.new { procedure_id_changed? } + before_save :build_default_champs, if: Proc.new { groupe_instructeur_id_changed? } before_save :build_default_individual, if: Proc.new { procedure.for_individual? } before_save :update_search_terms diff --git a/app/models/groupe_instructeur.rb b/app/models/groupe_instructeur.rb index dcb8a14de..af5c5eaab 100644 --- a/app/models/groupe_instructeur.rb +++ b/app/models/groupe_instructeur.rb @@ -3,4 +3,5 @@ class GroupeInstructeur < ApplicationRecord belongs_to :procedure has_many :assign_tos has_many :instructeurs, through: :assign_tos + has_many :dossiers end diff --git a/app/models/instructeur.rb b/app/models/instructeur.rb index df7cde3c2..bf2d29590 100644 --- a/app/models/instructeur.rb +++ b/app/models/instructeur.rb @@ -10,9 +10,9 @@ class Instructeur < ApplicationRecord has_many :procedures, through: :groupe_instructeurs has_many :assign_to_with_email_notifications, -> { with_email_notifications }, class_name: 'AssignTo', inverse_of: :instructeur - has_many :procedures_with_email_notifications, through: :assign_to_with_email_notifications, source: :procedure + has_many :groupe_instructeur_with_email_notifications, through: :assign_to_with_email_notifications, source: :groupe_instructeur - has_many :dossiers, -> { state_not_brouillon }, through: :procedures + has_many :dossiers, -> { state_not_brouillon }, through: :groupe_instructeurs has_many :follows, -> { active }, inverse_of: :instructeur has_many :previous_follows, -> { inactive }, class_name: 'Follow', inverse_of: :instructeur has_many :followed_dossiers, through: :follows, source: :dossier @@ -115,13 +115,13 @@ class Instructeur < ApplicationRecord def notifications_for_procedure(procedure, state = :en_cours) dossiers = case state when :termine - procedure.dossiers.termine + procedure.defaut_groupe_instructeur.dossiers.termine when :not_archived - procedure.dossiers.not_archived + procedure.defaut_groupe_instructeur.dossiers.not_archived when :all - procedure.dossiers + procedure.defaut_groupe_instructeur.dossiers else - procedure.dossiers.en_cours + procedure.defaut_groupe_instructeur.dossiers.en_cours end dossiers_id_with_notifications(dossiers) @@ -137,7 +137,7 @@ class Instructeur < ApplicationRecord Dossier.en_cours end - Dossier.where(id: dossiers_id_with_notifications(dossiers)).group(:procedure_id).count + Dossier.joins(:groupe_instructeur).where(id: dossiers_id_with_notifications(dossiers)).group('groupe_instructeurs.procedure_id').count end def create_trusted_device_token @@ -203,11 +203,13 @@ class Instructeur < ApplicationRecord end def email_notification_data - procedures_with_email_notifications - .reduce([]) do |acc, procedure| + groupe_instructeur_with_email_notifications + .reduce([]) do |acc, groupe| + + procedure = groupe.procedure h = { - nb_en_construction: procedure.dossiers.en_construction.count, + nb_en_construction: groupe.dossiers.en_construction.count, nb_notification: notifications_for_procedure(procedure, :all).count } diff --git a/app/models/procedure.rb b/app/models/procedure.rb index a340bf53a..b721afe24 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -5,7 +5,6 @@ class Procedure < ApplicationRecord has_many :types_de_champ, -> { root.public_only.ordered }, inverse_of: :procedure, dependent: :destroy has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', inverse_of: :procedure, dependent: :destroy - has_many :dossiers, dependent: :restrict_with_exception has_many :deleted_dossiers, dependent: :destroy has_one :module_api_carto, dependent: :destroy @@ -18,6 +17,8 @@ class Procedure < ApplicationRecord has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! } has_many :groupe_instructeurs, dependent: :destroy + has_many :dossiers, through: :groupe_instructeurs, dependent: :restrict_with_exception + has_one :initiated_mail, class_name: "Mails::InitiatedMail", dependent: :destroy has_one :received_mail, class_name: "Mails::ReceivedMail", dependent: :destroy has_one :closed_mail, class_name: "Mails::ClosedMail", dependent: :destroy @@ -118,7 +119,7 @@ class Procedure < ApplicationRecord if locked? raise "Can not reset a locked procedure." else - dossiers.destroy_all + groupe_instructeurs.each { |gi| gi.dossiers.destroy_all } end end diff --git a/app/serializers/individual_serializer.rb b/app/serializers/individual_serializer.rb index b7b3032a5..0ef0f4207 100644 --- a/app/serializers/individual_serializer.rb +++ b/app/serializers/individual_serializer.rb @@ -4,6 +4,6 @@ class IndividualSerializer < ActiveModel::Serializer attribute :birthdate, key: :date_naissance, if: :include_birthdate? def include_birthdate? - object&.dossier&.procedure&.ask_birthday + object&.dossier&.groupe_instructeur&.procedure&.ask_birthday end end diff --git a/app/services/administrateur_usage_statistics_service.rb b/app/services/administrateur_usage_statistics_service.rb index 6fc01339b..44d5cba6a 100644 --- a/app/services/administrateur_usage_statistics_service.rb +++ b/app/services/administrateur_usage_statistics_service.rb @@ -131,10 +131,10 @@ class AdministrateurUsageStatisticsService result = {} Dossier - .joins(procedure: [:administrateurs]) + .joins(groupe_instructeur: { procedure: [:administrateurs] }) .group( 'administrateurs.id', - :procedure_id, + 'groupe_instructeurs.procedure_id', <<~EOSQL CASE WHEN state IN('accepte', 'refuse', 'sans_suite') THEN 'termine' diff --git a/app/views/commencer/show.html.haml b/app/views/commencer/show.html.haml index 1f584df63..ee3564f92 100644 --- a/app/views/commencer/show.html.haml +++ b/app/views/commencer/show.html.haml @@ -11,7 +11,7 @@ = render partial: 'shared/france_connect_login', locals: { url: commencer_france_connect_path(path: @procedure.path) } - else - - dossiers = current_user.dossiers.where(procedure: @procedure) + - dossiers = current_user.dossiers.where(groupe_instructeur: @procedure.groupe_instructeurs) - drafts = dossiers.merge(Dossier.state_brouillon) - not_drafts = dossiers.merge(Dossier.state_not_brouillon) diff --git a/db/migrate/20190822143413_add_groupe_instructeur_id_column_to_dossier.rb b/db/migrate/20190822143413_add_groupe_instructeur_id_column_to_dossier.rb new file mode 100644 index 000000000..2c1240bb7 --- /dev/null +++ b/db/migrate/20190822143413_add_groupe_instructeur_id_column_to_dossier.rb @@ -0,0 +1,5 @@ +class AddGroupeInstructeurIdColumnToDossier < ActiveRecord::Migration[5.2] + def change + add_reference :dossiers, :groupe_instructeur, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index a2ee405e7..39061aa04 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: 2019_08_19_145355) do +ActiveRecord::Schema.define(version: 2019_08_22_143413) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -275,8 +275,10 @@ ActiveRecord::Schema.define(version: 2019_08_19_145355) do t.datetime "hidden_at" t.text "search_terms" t.text "private_search_terms" + t.bigint "groupe_instructeur_id" t.index "to_tsvector('french'::regconfig, (search_terms || private_search_terms))", name: "index_dossiers_on_search_terms_private_search_terms", using: :gin t.index "to_tsvector('french'::regconfig, search_terms)", name: "index_dossiers_on_search_terms", using: :gin + t.index ["groupe_instructeur_id"], name: "index_dossiers_on_groupe_instructeur_id" t.index ["hidden_at"], name: "index_dossiers_on_hidden_at" t.index ["procedure_id"], name: "index_dossiers_on_procedure_id" t.index ["user_id"], name: "index_dossiers_on_user_id" @@ -648,6 +650,7 @@ ActiveRecord::Schema.define(version: 2019_08_19_145355) do add_foreign_key "dossier_operation_logs", "bill_signatures" add_foreign_key "dossier_operation_logs", "dossiers" add_foreign_key "dossier_operation_logs", "instructeurs" + add_foreign_key "dossiers", "groupe_instructeurs" add_foreign_key "dossiers", "users" add_foreign_key "feedbacks", "users" add_foreign_key "geo_areas", "champs" diff --git a/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake b/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake index 42688fa49..b563540e6 100644 --- a/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake +++ b/lib/tasks/2019_06_06_fix_timestamps_of_migrated_dossiers.rake @@ -4,7 +4,7 @@ namespace :fix_timestamps_of_migrated_dossiers do desc 'Fix the timestamps of dossiers affected by the faulty PJ migration' task run: :environment do affected_time_range = Time.utc(2019, 6, 4, 8, 0)..Time.utc(2019, 6, 4, 18, 0) - dossiers = Dossier.unscoped.where(procedure_id: 0..1000).where(updated_at: affected_time_range) + dossiers = Dossier.unscoped.includes(:groupe_instructeur).where(groupe_instructeurs: { procedure_id: 0..1000 }).where(updated_at: affected_time_range) progress = ProgressReport.new(dossiers.count) diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index 48a19ccef..7a2f63bf7 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -344,7 +344,7 @@ describe Instructeurs::ProceduresController, type: :controller do context "when logged in" do before { sign_in(instructeur.user) } - it { expect(instructeur.procedures_with_email_notifications).to be_empty } + it { expect(instructeur.groupe_instructeur_with_email_notifications).to be_empty } context 'when the instructeur update its preferences' do let(:assign_to) { instructeur.assign_to.find_by(procedure: procedure) } @@ -353,7 +353,7 @@ describe Instructeurs::ProceduresController, type: :controller do patch :update_email_notifications, params: { procedure_id: procedure.id, assign_to: { id: assign_to.id, email_notifications_enabled: true } } end - it { expect(instructeur.procedures_with_email_notifications).to eq([procedure]) } + it { expect(instructeur.groupe_instructeur_with_email_notifications).to eq([procedure.defaut_groupe_instructeur]) } end end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index eceea782a..1222e3c22 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -14,7 +14,7 @@ describe Dossier do describe 'with_champs' do let(:procedure) { create(:procedure) } - let(:dossier) { Dossier.create(user: create(:user), procedure: procedure) } + let(:dossier) { Dossier.create(user: create(:user), groupe_instructeur: procedure.defaut_groupe_instructeur) } before do create(:type_de_champ, libelle: 'l1', order_place: 1, procedure: procedure) @@ -189,7 +189,7 @@ describe Dossier do describe '#champs' do let(:procedure) { create(:procedure) } - let(:dossier) { Dossier.create(user: create(:user), procedure: procedure) } + let(:dossier) { Dossier.create(user: create(:user), groupe_instructeur: procedure.defaut_groupe_instructeur) } before do create(:type_de_champ, libelle: 'l1', order_place: 1, procedure: procedure) @@ -202,7 +202,7 @@ describe Dossier do describe '#champs_private' do let(:procedure) { create :procedure } - let(:dossier) { Dossier.create(user: create(:user), procedure: procedure) } + let(:dossier) { Dossier.create(user: create(:user), groupe_instructeur: procedure.defaut_groupe_instructeur) } before do create :type_de_champ, :private, libelle: 'l1', order_place: 1, procedure: procedure @@ -400,7 +400,7 @@ describe Dossier do dossier = nil expect do perform_enqueued_jobs do - dossier = Dossier.create(procedure: procedure, state: Dossier.states.fetch(:brouillon), user: user) + dossier = Dossier.create(groupe_instructeur: procedure.defaut_groupe_instructeur, state: Dossier.states.fetch(:brouillon), user: user) end end.to change(ActionMailer::Base.deliveries, :size).from(0).to(1) @@ -410,11 +410,11 @@ describe Dossier do end it "does not send an email when the dossier is created with a non brouillon state" do - expect { Dossier.create(procedure: procedure, state: Dossier.states.fetch(:en_construction), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) - expect { Dossier.create(procedure: procedure, state: Dossier.states.fetch(:en_instruction), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) - expect { Dossier.create(procedure: procedure, state: Dossier.states.fetch(:accepte), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) - expect { Dossier.create(procedure: procedure, state: Dossier.states.fetch(:refuse), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) - expect { Dossier.create(procedure: procedure, state: Dossier.states.fetch(:sans_suite), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) + expect { Dossier.create(groupe_instructeur: procedure.defaut_groupe_instructeur, state: Dossier.states.fetch(:en_construction), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) + expect { Dossier.create(groupe_instructeur: procedure.defaut_groupe_instructeur, state: Dossier.states.fetch(:en_instruction), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) + expect { Dossier.create(groupe_instructeur: procedure.defaut_groupe_instructeur, state: Dossier.states.fetch(:accepte), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) + expect { Dossier.create(groupe_instructeur: procedure.defaut_groupe_instructeur, state: Dossier.states.fetch(:refuse), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) + expect { Dossier.create(groupe_instructeur: procedure.defaut_groupe_instructeur, state: Dossier.states.fetch(:sans_suite), user: user) }.not_to change(ActionMailer::Base.deliveries, :size) end end