diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index f900a5bf0..3a826e5af 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -8,17 +8,19 @@ module Instructeurs def index @procedures = current_instructeur.visible_procedures.order(archived_at: :desc, published_at: :desc, created_at: :desc) - dossiers = current_instructeur.dossiers - @dossiers_count_per_procedure = dossiers.all_state.group(:procedure_id).reorder(nil).count - @dossiers_a_suivre_count_per_procedure = dossiers.without_followers.en_cours.group(:procedure_id).reorder(nil).count - @dossiers_archived_count_per_procedure = dossiers.archived.group(:procedure_id).count - @dossiers_termines_count_per_procedure = dossiers.termine.group(:procedure_id).reorder(nil).count + groupe_instructeurs = current_instructeur.groupe_instructeurs.where(procedure: @procedures) - @followed_dossiers_count_per_procedure = current_instructeur + dossiers = current_instructeur.dossiers + @dossiers_count_per_groupe_instructeur = dossiers.all_state.group(:groupe_instructeur_id).reorder(nil).count + @dossiers_a_suivre_count_per_groupe_instructeur = dossiers.without_followers.en_cours.group(:groupe_instructeur_id).reorder(nil).count + @dossiers_archived_count_per_groupe_instructeur = dossiers.archived.group(:groupe_instructeur_id).count + @dossiers_termines_count_per_groupe_instructeur = dossiers.termine.group(:groupe_instructeur_id).reorder(nil).count + + @followed_dossiers_count_per_groupe_instructeur = current_instructeur .followed_dossiers .en_cours - .where(procedure: @procedures) - .group(:procedure_id) + .where(groupe_instructeur: groupe_instructeurs) + .group(:groupe_instructeur_id) .reorder(nil) .count end @@ -34,6 +36,7 @@ module Instructeurs @displayed_fields_values = displayed_fields_values @a_suivre_dossiers = procedure + .defaut_groupe_instructeur .dossiers .includes(:user) .without_followers @@ -42,19 +45,19 @@ module Instructeurs @followed_dossiers = current_instructeur .followed_dossiers .includes(:user) - .where(procedure: @procedure) + .where(groupe_instructeur: procedure.defaut_groupe_instructeur) .en_cours @followed_dossiers_id = current_instructeur .followed_dossiers - .where(procedure: @procedure) + .where(groupe_instructeur: procedure.defaut_groupe_instructeur) .pluck(:id) - @termines_dossiers = procedure.dossiers.includes(:user).termine + @termines_dossiers = procedure.defaut_groupe_instructeur.dossiers.includes(:user).termine - @all_state_dossiers = procedure.dossiers.includes(:user).all_state + @all_state_dossiers = procedure.defaut_groupe_instructeur.dossiers.includes(:user).all_state - @archived_dossiers = procedure.dossiers.includes(:user).archived + @archived_dossiers = procedure.defaut_groupe_instructeur.dossiers.includes(:user).archived @dossiers = case statut when 'a-suivre' @@ -209,7 +212,7 @@ module Instructeurs end def assign_to - current_instructeur.assign_to.find_by(procedure: procedure) + current_instructeur.assign_to.joins(:groupe_instructeur).find_by(groupe_instructeurs: { procedure: procedure }) end def statut diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 571e6d79f..edcda5802 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -253,7 +253,7 @@ class StatsController < ApplicationController processed_dossiers = dossiers .where(:processed_at => min_date..max_date) - .pluck(:procedure_id, :en_construction_at, :processed_at) + .pluck(:groupe_instructeur_id, :en_construction_at, :processed_at) # Group dossiers by month processed_dossiers_by_month = processed_dossiers @@ -263,10 +263,10 @@ class StatsController < ApplicationController processed_dossiers_by_month.map do |month, value| # Group the dossiers for this month by procedure - dossiers_grouped_by_procedure = value.group_by { |dossier| dossier[0] } + dossiers_grouped_by_groupe_instructeur = value.group_by { |dossier| dossier[0] } # Compute the mean time for this procedure - procedure_processing_times = dossiers_grouped_by_procedure.map do |_procedure_id, procedure_dossiers| + procedure_processing_times = dossiers_grouped_by_groupe_instructeur.map do |_procedure_id, procedure_dossiers| procedure_dossiers_processing_time = procedure_dossiers.map do |dossier| (dossier[2] - dossier[1]).to_f / (3600 * 24) end @@ -295,7 +295,7 @@ class StatsController < ApplicationController processed_dossiers = dossiers .where(:processed_at => min_date..max_date) .pluck( - :procedure_id, + :groupe_instructeur_id, Arel.sql('EXTRACT(EPOCH FROM (en_construction_at - created_at)) / 60 AS processing_time'), :processed_at ) @@ -306,18 +306,26 @@ class StatsController < ApplicationController processed_at.beginning_of_month.to_s end + groupe_instructeur_ids = processed_dossiers.map { |gid, _, _| gid }.uniq + groupe_instructeurs = GroupeInstructeur.where(id: groupe_instructeur_ids).pluck(:id, :procedure_id) + procedure_id_type_de_champs_count = TypeDeChamp .where(private: false) .group(:procedure_id) .count + groupe_instructeur_id_type_de_champs_count = groupe_instructeurs.reduce({}) do |acc, (gi_id, procedure_id)| + acc[gi_id] = procedure_id_type_de_champs_count[procedure_id] + acc + end + processed_dossiers_by_month.map do |month, dossier_plucks| # Group the dossiers for this month by procedure - dossiers_grouped_by_procedure = dossier_plucks.group_by { |(procedure_id, *_)| procedure_id } + dossiers_grouped_by_groupe_instructeur = dossier_plucks.group_by { |(groupe_instructeur_id, *_)| groupe_instructeur_id } # Compute the mean time for this procedure - procedure_processing_times = dossiers_grouped_by_procedure.map do |procedure_id, procedure_dossiers| - procedure_fields_count = procedure_id_type_de_champs_count[procedure_id] + procedure_processing_times = dossiers_grouped_by_groupe_instructeur.map do |groupe_instructeur_id, procedure_dossiers| + procedure_fields_count = groupe_instructeur_id_type_de_champs_count[groupe_instructeur_id] if (procedure_fields_count == 0 || procedure_fields_count.nil?) next diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index c28a7a53a..b95c514ab 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -224,7 +224,7 @@ module Users procedure = Procedure.publiees.find(params[:procedure_id]) end - dossier = Dossier.create!(procedure: procedure, user: current_user, state: Dossier.states.fetch(:brouillon)) + dossier = Dossier.create!(groupe_instructeur: procedure.defaut_groupe_instructeur, user: current_user, state: Dossier.states.fetch(:brouillon)) if dossier.procedure.for_individual if current_user.france_connect_information.present? diff --git a/app/models/assign_to.rb b/app/models/assign_to.rb index 33425d306..96bb9713b 100644 --- a/app/models/assign_to.rb +++ b/app/models/assign_to.rb @@ -1,8 +1,8 @@ class AssignTo < ApplicationRecord - belongs_to :procedure belongs_to :instructeur belongs_to :groupe_instructeur has_one :procedure_presentation, dependent: :destroy + has_one :procedure, through: :groupe_instructeur scope :with_email_notifications, -> { where(email_notifications_enabled: true) } 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..bf849e4ba 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 @@ -86,7 +86,7 @@ class Instructeur < ApplicationRecord end def procedure_presentation_and_errors_for_procedure_id(procedure_id) - assign_to.find_by(procedure_id: procedure_id).procedure_presentation_or_default_and_errors + assign_to.joins(:groupe_instructeur).find_by(groupe_instructeurs: { procedure_id: procedure_id }).procedure_presentation_or_default_and_errors end def notifications_for_dossier(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..120d96ef7 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 @@ -154,7 +155,12 @@ class Procedure < ApplicationRecord # Warning: dossier after_save build_default_champs must be removed # to save a dossier created from this method def new_dossier - Dossier.new(procedure: self, champs: build_champs, champs_private: build_champs_private) + Dossier.new( + procedure: self, + champs: build_champs, + champs_private: build_champs_private, + groupe_instructeur: defaut_groupe_instructeur + ) end def build_champs 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/app/views/instructeurs/procedures/index.html.haml b/app/views/instructeurs/procedures/index.html.haml index d1566afb0..1c35a6e94 100644 --- a/app/views/instructeurs/procedures/index.html.haml +++ b/app/views/instructeurs/procedures/index.html.haml @@ -19,7 +19,7 @@ %li %object = link_to(instructeur_procedure_path(p, statut: 'a-suivre')) do - - a_suivre_count = @dossiers_a_suivre_count_per_procedure[p.id] || 0 + - a_suivre_count = @dossiers_a_suivre_count_per_groupe_instructeur[p.defaut_groupe_instructeur.id] || 0 .stats-number = a_suivre_count .stats-legend @@ -27,9 +27,9 @@ %li %object = link_to(instructeur_procedure_path(p, statut: 'suivis')) do - - if current_instructeur.notifications_per_procedure[p.id].present? + - if current_instructeur.notifications_per_procedure[p.defaut_groupe_instructeur.id].present? %span.notifications{ 'aria-label': "notifications" } - - followed_count = @followed_dossiers_count_per_procedure[p.id] || 0 + - followed_count = @followed_dossiers_count_per_groupe_instructeur[p.defaut_groupe_instructeur.id] || 0 .stats-number = followed_count .stats-legend @@ -37,9 +37,9 @@ %li %object = link_to(instructeur_procedure_path(p, statut: 'traites')) do - - if current_instructeur.notifications_per_procedure(:termine)[p.id].present? + - if current_instructeur.notifications_per_procedure(:termine)[p.defaut_groupe_instructeur.id].present? %span.notifications{ 'aria-label': "notifications" } - - termines_count = @dossiers_termines_count_per_procedure[p.id] || 0 + - termines_count = @dossiers_termines_count_per_groupe_instructeur[p.defaut_groupe_instructeur.id] || 0 .stats-number = termines_count .stats-legend @@ -47,7 +47,7 @@ %li %object = link_to(instructeur_procedure_path(p, statut: 'tous')) do - - dossier_count = @dossiers_count_per_procedure[p.id] || 0 + - dossier_count = @dossiers_count_per_groupe_instructeur[p.defaut_groupe_instructeur.id] || 0 .stats-number = dossier_count .stats-legend @@ -55,7 +55,7 @@ %li %object = link_to(instructeur_procedure_path(p, statut: 'archives')) do - - archived_count = @dossiers_archived_count_per_procedure[p.id] || 0 + - archived_count = @dossiers_archived_count_per_groupe_instructeur[p.defaut_groupe_instructeur.id] || 0 .stats-number = archived_count .stats-legend diff --git a/app/views/layouts/_new_header.haml b/app/views/layouts/_new_header.haml index 0ffb8eda5..eb3c7eb97 100644 --- a/app/views/layouts/_new_header.haml +++ b/app/views/layouts/_new_header.haml @@ -7,7 +7,10 @@ .header-inner-content .flex.align-center - = link_to '', root_path_for_profile(nav_bar_profile), class: "header-logo", title: "Revenir à l’accueil" + - if params[:controller] == 'users/commencer' + = link_to 'Revenir en arrière', url_for(:back), class: "button", title: "Revenir sur le site de mon administration" + - else + = link_to '', root_path_for_profile(nav_bar_profile), class: "header-logo", title: "Revenir à l’accueil" - if nav_bar_profile == :instructeur && instructeur_signed_in? - current_url = request.path_info 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/2018_05_14_add_annotation_privee_to_procedure.rake b/lib/tasks/2018_05_14_add_annotation_privee_to_procedure.rake deleted file mode 100644 index 22d496dfa..000000000 --- a/lib/tasks/2018_05_14_add_annotation_privee_to_procedure.rake +++ /dev/null @@ -1,54 +0,0 @@ -namespace :'2018_05_14_add_annotation_privee_to_procedure' do - task add: :environment do - procedure_id = ENV['PROCEDURE_ID'] || 3723 - add_an_annotation_privee(procedure_id) - update_description(procedure_id) - end - - def add_an_annotation_privee(procedure_id) - new_tdc_order_place = 7 - - TypeDeChamp - .where(procedure_id: procedure_id, private: true) - .where('order_place >= ?', new_tdc_order_place) - .each do |tdc| - tdc.update_attribute(:order_place, tdc.order_place + 1) - end - - new_tdc = TypeDeChamp.create( - procedure_id: procedure_id, - private: true, - libelle: 'URL Espace de consultation', - order_place: new_tdc_order_place, - type_champ: 'text', - description: 'L’instructeur renseigne l’URL du site de dépôt des observations ou la page web de la préfecture où est mentionné l’email pour déposer les commentaires' - ) - - Dossier.includes(champs: :type_de_champ).where(procedure_id: procedure_id).all.each do |dossier| - Champs::TextChamp.create( - dossier: dossier, - type_de_champ: new_tdc, - private: true - ) - end - end - - def update_description(procedure_id) - TypeDeChamp.find_by( - private: true, - description: "L'instructeur en préfecture saisie la date de publication de l'avis à consultation du public ", - procedure_id: procedure_id - )&.update( - description: "L'instructeur en préfecture saisie la première date de publication de l'avis à consultation du public" - ) - - TypeDeChamp.find_by( - private: false, - libelle: "Fichier(s) Etude d'impact", - procedure_id: procedure_id - )&.update( - libelle: "Fichier Etude d'impact", - description: "Vous devez télé-charger votre fichier constituant le document dit \"étude d'impact\" de votre projet.\r\nObligatoire." - ) - end -end 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/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake b/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake deleted file mode 100644 index 2e7db554d..000000000 --- a/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake +++ /dev/null @@ -1,25 +0,0 @@ -require Rails.root.join("lib", "tasks", "task_helper") - -namespace :after_party do - desc 'Deployment task: delete_dossiers_without_procedure' - task delete_dossiers_without_procedure: :environment do - rake_puts "Running deploy task 'delete_dossiers_without_procedure'" - - dossiers_without_procedure = Dossier.left_outer_joins(:procedure).where(procedures: { id: nil }) - total = dossiers_without_procedure.count - expected_dossiers_count = 60 - - if total > expected_dossiers_count - raise "Error: #{expected_dossiers_count} dossiers expected, but found #{total}. Aborting." - end - - dossiers_without_procedure.each do |dossier| - rake_puts "Destroy dossier #{dossier.id}" - dossier.destroy! - end - - rake_puts "#{total} dossiers without procedure were destroyed." - - AfterParty::TaskRecord.create version: '20190117154829' - end # task :delete_dossiers_without_procedure -end # namespace :after_party diff --git a/lib/tasks/deployment/20190826153115_link_dossier_and_groupe_instructeur.rake b/lib/tasks/deployment/20190826153115_link_dossier_and_groupe_instructeur.rake new file mode 100644 index 000000000..623e617d0 --- /dev/null +++ b/lib/tasks/deployment/20190826153115_link_dossier_and_groupe_instructeur.rake @@ -0,0 +1,14 @@ +namespace :after_party do + desc 'Deployment task: link_dossier_and_groupe_instructeur' + task link_dossier_and_groupe_instructeur: :environment do + sql = <<~SQL + UPDATE dossiers SET groupe_instructeur_id = groupe_instructeurs.id + FROM dossiers AS d1 INNER JOIN groupe_instructeurs ON groupe_instructeurs.procedure_id = d1.procedure_id + WHERE dossiers.id = d1.id; + SQL + + ActiveRecord::Base.connection.execute(sql) + + AfterParty::TaskRecord.create version: '20190826153115' + end +end diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index 48a19ccef..ef66acd25 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -125,11 +125,11 @@ describe Instructeurs::ProceduresController, type: :controller do let(:state) { Dossier.states.fetch(:brouillon) } before { subject } - it { expect(assigns(:dossiers_count_per_procedure)[procedure.id]).to eq(nil) } - it { expect(assigns(:dossiers_a_suivre_count_per_procedure)[procedure.id]).to eq(nil) } - it { expect(assigns(:dossiers_archived_count_per_procedure)[procedure.id]).to eq(nil) } - it { expect(assigns(:followed_dossiers_count_per_procedure)[procedure.id]).to eq(nil) } - it { expect(assigns(:dossiers_termines_count_per_procedure)[procedure.id]).to eq(nil) } + it { expect(assigns(:dossiers_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(nil) } + it { expect(assigns(:dossiers_a_suivre_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(nil) } + it { expect(assigns(:dossiers_archived_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(nil) } + it { expect(assigns(:followed_dossiers_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(nil) } + it { expect(assigns(:dossiers_termines_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(nil) } end context "with not draft state on multiple procedures" do @@ -149,17 +149,17 @@ describe Instructeurs::ProceduresController, type: :controller do subject end - it { expect(assigns(:dossiers_count_per_procedure)[procedure.id]).to eq(3) } - it { expect(assigns(:dossiers_a_suivre_count_per_procedure)[procedure.id]).to eq(3) } - it { expect(assigns(:followed_dossiers_count_per_procedure)[procedure.id]).to eq(nil) } - it { expect(assigns(:dossiers_archived_count_per_procedure)[procedure.id]).to eq(1) } - it { expect(assigns(:dossiers_termines_count_per_procedure)[procedure.id]).to eq(nil) } + it { expect(assigns(:dossiers_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(3) } + it { expect(assigns(:dossiers_a_suivre_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(3) } + it { expect(assigns(:followed_dossiers_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(nil) } + it { expect(assigns(:dossiers_archived_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(1) } + it { expect(assigns(:dossiers_termines_count_per_groupe_instructeur)[procedure.defaut_groupe_instructeur.id]).to eq(nil) } - it { expect(assigns(:dossiers_count_per_procedure)[procedure2.id]).to eq(3) } - it { expect(assigns(:dossiers_a_suivre_count_per_procedure)[procedure2.id]).to eq(nil) } - it { expect(assigns(:followed_dossiers_count_per_procedure)[procedure2.id]).to eq(1) } - it { expect(assigns(:dossiers_archived_count_per_procedure)[procedure2.id]).to eq(nil) } - it { expect(assigns(:dossiers_termines_count_per_procedure)[procedure2.id]).to eq(1) } + it { expect(assigns(:dossiers_count_per_groupe_instructeur)[procedure2.defaut_groupe_instructeur.id]).to eq(3) } + it { expect(assigns(:dossiers_a_suivre_count_per_groupe_instructeur)[procedure2.defaut_groupe_instructeur.id]).to eq(nil) } + it { expect(assigns(:followed_dossiers_count_per_groupe_instructeur)[procedure2.defaut_groupe_instructeur.id]).to eq(1) } + it { expect(assigns(:dossiers_archived_count_per_groupe_instructeur)[procedure2.defaut_groupe_instructeur.id]).to eq(nil) } + it { expect(assigns(:dossiers_termines_count_per_groupe_instructeur)[procedure2.defaut_groupe_instructeur.id]).to eq(1) } end end end @@ -344,16 +344,16 @@ 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) } + let(:assign_to) { instructeur.assign_to.joins(:groupe_instructeur).find_by(groupe_instructeurs: { procedure: procedure }) } before 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/factories/assign_to.rb b/spec/factories/assign_to.rb index 7a5110f36..759c32d52 100644 --- a/spec/factories/assign_to.rb +++ b/spec/factories/assign_to.rb @@ -1,4 +1,7 @@ FactoryBot.define do factory :assign_to do + after(:build) do |assign_to, _evaluator| + assign_to.groupe_instructeur = assign_to.procedure.defaut_groupe_instructeur + end end end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 7f90b5689..f828ff27f 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -4,11 +4,17 @@ FactoryBot.define do state { Dossier.states.fetch(:brouillon) } association :user, factory: [:user] - before(:create) do |dossier, _evaluator| - if !dossier.procedure + transient do + procedure { nil } + end + + after(:build) do |dossier, evaluator| + if evaluator.procedure.present? + procedure = evaluator.procedure + else procedure = create(:procedure, :published, :with_type_de_champ, :with_type_de_champ_private) - dossier.procedure = procedure end + dossier.groupe_instructeur = procedure.defaut_groupe_instructeur end trait :with_entreprise do diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 6136d005b..b652d1cc0 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -34,8 +34,13 @@ FactoryBot.define do dossiers_count { 1 } end - after(:build) do |procedure, evaluator| - procedure.dossiers << create_list(:dossier, evaluator.dossiers_count, procedure: procedure) + after(:create) do |procedure, evaluator| + user = create(:user) + evaluator.dossiers_count.times do + dossier = procedure.new_dossier + dossier.user = user + dossier.save! + end end end diff --git a/spec/lib/tasks/2018_05_14_add_annotation_privee_to_procedure_spec.rb b/spec/lib/tasks/2018_05_14_add_annotation_privee_to_procedure_spec.rb deleted file mode 100644 index e14e54012..000000000 --- a/spec/lib/tasks/2018_05_14_add_annotation_privee_to_procedure_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -describe '2018_05_14_add_annotation_privee_to_procedure' do - let!(:user) { create(:user) } - let!(:procedure) do - procedure = create(:procedure) - 10.times do |i| - TypeDeChamp.create( - procedure: procedure, - private: false, - libelle: 'libelle', - order_place: i, - type_champ: 'number' - ) - - TypeDeChamp.create( - procedure: procedure, - private: true, - libelle: 'libelle', - order_place: i, - type_champ: 'number' - ) - end - procedure - end - let!(:dossier) { Dossier.create(procedure: procedure, user: user, state: 'brouillon') } - let(:rake_task) { Rake::Task['2018_05_14_add_annotation_privee_to_procedure:add'] } - - before do - ENV['PROCEDURE_ID'] = procedure.id.to_s - rake_task.invoke - procedure.reload - end - - after { rake_task.reenable } - - it { expect(procedure.types_de_champ.count).to eq(10) } - it { expect(procedure.types_de_champ_private.count).to eq(11) } - it { expect(dossier.champs_private.includes(:type_de_champ).map(&:order_place).sort).to match((0..10).to_a) } - it { expect(procedure.types_de_champ_private.find_by(order_place: 7).type_champ).to eq('text') } -end diff --git a/spec/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake_spec.rb b/spec/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake_spec.rb deleted file mode 100644 index fd530c1e1..000000000 --- a/spec/lib/tasks/deployment/20190117154829_delete_dossiers_without_procedure.rake_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -describe '20190117154829_delete_dossiers_without_procedure.rake' do - let(:rake_task) { Rake::Task['after_party:delete_dossiers_without_procedure'] } - - subject do - rake_task.invoke - end - - after do - rake_task.reenable - end - - context 'when the procedure of some dossiers has been deleted' do - let!(:procedure1) { create(:procedure_with_dossiers, dossiers_count: 2) } - let!(:procedure2) { create(:procedure_with_dossiers, :published, dossiers_count: 2) } - let!(:procedure3) { create(:procedure_with_dossiers, :published, dossiers_count: 2) } - let!(:procedure4) { create(:procedure_with_dossiers, :archived, dossiers_count: 2) } - - let(:procedure_2_dossier_ids) { procedure2.dossiers.pluck(:id) } - - before do - procedure_2_dossier_ids - procedure2.defaut_groupe_instructeur.delete - procedure2.delete - expect(procedure_2_dossier_ids.count).to eq(2) - expect(Dossier.find_by(id: procedure_2_dossier_ids.first).procedure).to be nil - expect(Dossier.find_by(id: procedure_2_dossier_ids.second).procedure).to be nil - end - - it 'destroy dossiers without an existing procedure' do - subject - expect(Dossier.unscoped.find_by(id: procedure_2_dossier_ids.first)).to be nil - expect(Dossier.unscoped.find_by(id: procedure_2_dossier_ids.last)).to be nil - end - - it 'doesn’t destroy other dossiers' do - subject - expect(Dossier.all.count).to eq(6) - expect(procedure1.reload.dossiers.count).to eq(2) - expect(procedure3.reload.dossiers.count).to eq(2) - expect(procedure4.reload.dossiers.count).to eq(2) - end - end -end diff --git a/spec/lib/tasks/deployment/20190826153115_link_dossier_and_groupe_instructeur.rake_spec.rb b/spec/lib/tasks/deployment/20190826153115_link_dossier_and_groupe_instructeur.rake_spec.rb new file mode 100644 index 000000000..381e6aa14 --- /dev/null +++ b/spec/lib/tasks/deployment/20190826153115_link_dossier_and_groupe_instructeur.rake_spec.rb @@ -0,0 +1,36 @@ +describe '20190826153115_link_dossier_and_groupe_instructeur.rake' do + let(:rake_task) { Rake::Task['after_party:link_dossier_and_groupe_instructeur'] } + + subject { rake_task.invoke } + after { rake_task.reenable } + + context 'with 3 dossiers' do + let!(:procedure) { create(:procedure) } + let!(:procedure2) { create(:procedure) } + let!(:other_procedure_needed_to_create_dossier) { create(:procedure) } + let!(:other_gi) { other_procedure_needed_to_create_dossier.defaut_groupe_instructeur } + let!(:dossier) { Dossier.create(user: create(:user), procedure_id: procedure.id, groupe_instructeur: other_gi) } + let!(:dossier2) { Dossier.create(user: create(:user), procedure_id: procedure2.id, groupe_instructeur: other_gi) } + let!(:dossier3) { Dossier.create(user: create(:user), procedure_id: procedure2.id, groupe_instructeur: other_gi) } + + before do + [dossier, dossier2, dossier3].each do |d| + d.update_column('groupe_instructeur_id', nil) + end + + other_procedure_needed_to_create_dossier.groupe_instructeurs.destroy_all + other_procedure_needed_to_create_dossier.destroy + end + + it do + expect(dossier.reload.groupe_instructeur_id).to be_nil + subject + expect(Dossier.count).to eq(3) + expect(Procedure.count).to eq(2) + expect(GroupeInstructeur.count).to eq(2) + expect(dossier.reload.groupe_instructeur_id).to eq(procedure.defaut_groupe_instructeur.id) + expect(dossier2.reload.groupe_instructeur_id).to eq(procedure2.defaut_groupe_instructeur.id) + expect(dossier3.reload.groupe_instructeur_id).to eq(procedure2.defaut_groupe_instructeur.id) + end + end +end diff --git a/spec/models/assign_to_spec.rb b/spec/models/assign_to_spec.rb index 5e1f0e25f..adec8da2b 100644 --- a/spec/models/assign_to_spec.rb +++ b/spec/models/assign_to_spec.rb @@ -1,7 +1,7 @@ describe AssignTo, type: :model do describe '#procedure_presentation_or_default_and_errors' do let(:procedure) { create(:procedure) } - let(:assign_to) { AssignTo.create(procedure: procedure) } + let(:assign_to) { create(:assign_to, procedure: procedure) } let(:procedure_presentation_and_errors) { assign_to.procedure_presentation_or_default_and_errors } let(:procedure_presentation_or_default) { procedure_presentation_and_errors.first } 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 diff --git a/spec/serializers/individual_serializer_spec.rb b/spec/serializers/individual_serializer_spec.rb index 0617957f3..24ed39302 100644 --- a/spec/serializers/individual_serializer_spec.rb +++ b/spec/serializers/individual_serializer_spec.rb @@ -1,8 +1,8 @@ describe IndividualSerializer do describe '#attributes' do - let(:procedure) { build(:procedure) } - let(:dossier) { build(:dossier, procedure: procedure) } - let(:individual) { build(:individual, gender: 'M.', nom: 'nom', prenom: 'prenom', birthdate: Date.new(2001, 8, 27), dossier: dossier) } + let(:procedure) { create(:procedure) } + let(:dossier) { create(:dossier, procedure: procedure) } + let(:individual) { create(:individual, gender: 'M.', nom: 'nom', prenom: 'prenom', birthdate: Date.new(2001, 8, 27), dossier: dossier) } subject { IndividualSerializer.new(individual).serializable_hash } @@ -12,7 +12,7 @@ describe IndividualSerializer do it { is_expected.not_to have_key(:date_naissance) } context 'when the procedure asks for a birthdate' do - let(:procedure) { build(:procedure, ask_birthday: true) } + before { procedure.update(ask_birthday: true) } it { is_expected.to include(date_naissance: Date.new(2001, 8, 27)) } end diff --git a/spec/services/administrateur_usage_statistics_service_spec.rb b/spec/services/administrateur_usage_statistics_service_spec.rb index 2aaa44ff5..60eb299f0 100644 --- a/spec/services/administrateur_usage_statistics_service_spec.rb +++ b/spec/services/administrateur_usage_statistics_service_spec.rb @@ -90,13 +90,13 @@ describe AdministrateurUsageStatisticsService do end context 'with a procedure archivee' do - let(:procedure) { create(:procedure, aasm_state: 'archivee', dossiers: dossiers) } - let(:dossiers) do + let(:procedure) { create(:procedure, aasm_state: 'archivee') } + let!(:dossiers) do (1..7).flat_map do [ - create(:dossier, :en_construction), - create(:dossier, :en_instruction), - create(:dossier, :accepte) + create(:dossier, :en_construction, procedure: procedure), + create(:dossier, :en_instruction, procedure: procedure), + create(:dossier, :accepte, procedure: procedure) ] end end @@ -141,13 +141,13 @@ describe AdministrateurUsageStatisticsService do end context 'with a procedure en test' do - let(:procedure) { create(:procedure, dossiers: dossiers) } - let(:dossiers) do + let(:procedure) { create(:procedure) } + let!(:dossiers) do (1..7).flat_map do [ - create(:dossier, :en_construction), - create(:dossier, :en_instruction), - create(:dossier, :accepte) + create(:dossier, :en_construction, procedure: procedure), + create(:dossier, :en_instruction, procedure: procedure), + create(:dossier, :accepte, procedure: procedure) ] end end @@ -171,12 +171,12 @@ describe AdministrateurUsageStatisticsService do end context 'with a procedure en prod' do - let(:procedure) { create(:procedure, aasm_state: 'publiee', dossiers: dossiers) } - let(:dossiers) do + let(:procedure) { create(:procedure, aasm_state: 'publiee') } + let!(:dossiers) do [ - create(:dossier, :en_construction), - create(:dossier, :en_instruction), - create(:dossier, :accepte) + create(:dossier, :en_construction, procedure: procedure), + create(:dossier, :en_instruction, procedure: procedure), + create(:dossier, :accepte, procedure: procedure) ] end @@ -199,13 +199,13 @@ describe AdministrateurUsageStatisticsService do end context 'with a procedure en prod and more than 20 dossiers' do - let(:procedure) { create(:procedure, aasm_state: 'publiee', dossiers: dossiers) } - let(:dossiers) do + let(:procedure) { create(:procedure, aasm_state: 'publiee') } + let!(:dossiers) do (1..7).flat_map do [ - create(:dossier, :en_construction), - create(:dossier, :en_instruction), - create(:dossier, :accepte) + create(:dossier, :en_construction, procedure: procedure), + create(:dossier, :en_instruction, procedure: procedure), + create(:dossier, :accepte, procedure: procedure) ] end end