Link Dossier and GroupeInstructeur

This commit is contained in:
simon lehericey 2019-08-22 17:58:31 +02:00
parent 940813a3ab
commit 3cc8f0c792
12 changed files with 44 additions and 31 deletions

View file

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

View file

@ -3,4 +3,5 @@ class GroupeInstructeur < ApplicationRecord
belongs_to :procedure
has_many :assign_tos
has_many :instructeurs, through: :assign_tos
has_many :dossiers
end

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,5 @@
class AddGroupeInstructeurIdColumnToDossier < ActiveRecord::Migration[5.2]
def change
add_reference :dossiers, :groupe_instructeur, foreign_key: true
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: 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"

View file

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

View file

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

View file

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