From 72a3f6e89ce371df4cd025186def7392336ae98a Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 7 Feb 2024 17:19:50 +0100 Subject: [PATCH] refactor(export/archives): holds user profile asking for the archive or export --- app/controllers/administrateurs/archives_controller.rb | 2 +- app/controllers/administrateurs/exports_controller.rb | 2 +- app/controllers/instructeurs/archives_controller.rb | 2 +- app/models/administrateur.rb | 3 ++- app/models/archive.rb | 5 +++-- app/models/export.rb | 7 ++++--- app/models/instructeur.rb | 3 ++- app/services/procedure_archive_service.rb | 2 +- app/services/procedure_export_service.rb | 3 ++- ...53_add_user_profile_id_user_profile_type_to_exports.rb | 6 ++++++ .../20240202133417_add_administrateur_id_to_archives.rb | 6 ++++++ db/schema.rb | 4 ++++ .../instructeurs/procedures_controller_spec.rb | 2 +- spec/jobs/archive_creation_job_spec.rb | 2 +- spec/models/administrateur_spec.rb | 2 ++ spec/models/export_spec.rb | 8 ++++++++ spec/models/instructeur_spec.rb | 2 ++ spec/services/procedure_export_service_spec.rb | 7 ++++--- 18 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 db/migrate/20240202133353_add_user_profile_id_user_profile_type_to_exports.rb create mode 100644 db/migrate/20240202133417_add_administrateur_id_to_archives.rb diff --git a/app/controllers/administrateurs/archives_controller.rb b/app/controllers/administrateurs/archives_controller.rb index 2bd184f24..530c2f608 100644 --- a/app/controllers/administrateurs/archives_controller.rb +++ b/app/controllers/administrateurs/archives_controller.rb @@ -14,7 +14,7 @@ module Administrateurs def create type = params[:type] - archive = Archive.find_or_create_archive(type, year_month, all_groupe_instructeurs) + archive = Archive.find_or_create_archive(type, year_month, all_groupe_instructeurs, current_administrateur) if archive.pending? ArchiveCreationJob.perform_later(@procedure, archive, current_administrateur) flash[:notice] = "Votre demande a été prise en compte. Selon le nombre de dossiers, cela peut prendre de quelques minutes à plusieurs heures. Vous recevrez un courriel lorsque le fichier sera disponible." diff --git a/app/controllers/administrateurs/exports_controller.rb b/app/controllers/administrateurs/exports_controller.rb index 1d9b0938a..6e1d55305 100644 --- a/app/controllers/administrateurs/exports_controller.rb +++ b/app/controllers/administrateurs/exports_controller.rb @@ -4,7 +4,7 @@ module Administrateurs before_action :ensure_not_super_admin! def download - export = Export.find_or_create_fresh_export(export_format, all_groupe_instructeurs, current_administrateur.instructeur, **export_options) + export = Export.find_or_create_fresh_export(export_format, all_groupe_instructeurs, current_administrateur, **export_options) @dossiers_count = export.count if export.available? diff --git a/app/controllers/instructeurs/archives_controller.rb b/app/controllers/instructeurs/archives_controller.rb index 00bd989da..fd37a9d60 100644 --- a/app/controllers/instructeurs/archives_controller.rb +++ b/app/controllers/instructeurs/archives_controller.rb @@ -13,7 +13,7 @@ module Instructeurs def create type = params[:type] - archive = Archive.find_or_create_archive(type, year_month, groupe_instructeurs) + archive = Archive.find_or_create_archive(type, year_month, groupe_instructeurs, current_instructeur) if archive.pending? ArchiveCreationJob.perform_later(@procedure, archive, current_instructeur) flash[:notice] = "Votre demande a été prise en compte. Selon le nombre de dossiers, cela peut prendre de quelques minutes à plusieurs heures. Vous recevrez un courriel lorsque le fichier sera disponible." diff --git a/app/models/administrateur.rb b/app/models/administrateur.rb index 48234f03a..aec396bf4 100644 --- a/app/models/administrateur.rb +++ b/app/models/administrateur.rb @@ -9,7 +9,8 @@ class Administrateur < ApplicationRecord has_many :api_tokens, inverse_of: :administrateur, dependent: :destroy has_many :commentaire_groupe_gestionnaires, as: :sender has_and_belongs_to_many :default_zones, class_name: 'Zone', join_table: 'default_zones_administrateurs' - + has_many :archives, as: :user_profile + has_many :exports, as: :user_profile belongs_to :user belongs_to :groupe_gestionnaire, optional: true diff --git a/app/models/archive.rb b/app/models/archive.rb index 87eb4e461..e64c96a0b 100644 --- a/app/models/archive.rb +++ b/app/models/archive.rb @@ -9,6 +9,7 @@ class Archive < ApplicationRecord has_and_belongs_to_many :groupe_instructeurs has_one_attached :file + belongs_to :user_profile, polymorphic: true, optional: true scope :for_groupe_instructeur, -> (groupe_instructeur) { joins(:archives_groupe_instructeurs) @@ -30,9 +31,9 @@ class Archive < ApplicationRecord end end - def self.find_or_create_archive(time_span_type, month, groupe_instructeurs) + def self.find_or_create_archive(time_span_type, month, groupe_instructeurs, user_profile) create_with(groupe_instructeurs: groupe_instructeurs) - .create_or_find_by(time_span_type: time_span_type, month: month, key: generate_cache_key(groupe_instructeurs)) + .create_or_find_by(time_span_type:, month:, user_profile:, key: generate_cache_key(groupe_instructeurs)) end private diff --git a/app/models/export.rb b/app/models/export.rb index b8c72020a..3a7a1ac34 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -30,6 +30,7 @@ class Export < ApplicationRecord has_and_belongs_to_many :groupe_instructeurs belongs_to :procedure_presentation, optional: true belongs_to :instructeur, optional: true + belongs_to :user_profile, polymorphic: true, optional: true has_one_attached :file @@ -65,7 +66,7 @@ class Export < ApplicationRecord procedure_presentation_id.present? end - def self.find_or_create_fresh_export(format, groupe_instructeurs, instructeur, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil) + def self.find_or_create_fresh_export(format, groupe_instructeurs, user_profile, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil) attributes = { format:, time_span_type:, @@ -81,7 +82,7 @@ class Export < ApplicationRecord return recent_export if recent_export.present? create!(**attributes, groupe_instructeurs:, - instructeur:, + user_profile:, procedure_presentation:, procedure_presentation_snapshot: procedure_presentation&.snapshot) end @@ -146,7 +147,7 @@ class Export < ApplicationRecord end def blob - service = ProcedureExportService.new(procedure, dossiers_for_export) + service = ProcedureExportService.new(procedure, dossiers_for_export, user_profile) case format.to_sym when :csv diff --git a/app/models/instructeur.rb b/app/models/instructeur.rb index 86f7aa4e4..f22289b01 100644 --- a/app/models/instructeur.rb +++ b/app/models/instructeur.rb @@ -18,8 +18,9 @@ class Instructeur < ApplicationRecord has_many :followed_dossiers, through: :follows, source: :dossier has_many :previously_followed_dossiers, -> { distinct }, through: :previous_follows, source: :dossier has_many :trusted_device_tokens, dependent: :destroy - has_many :archives has_many :bulk_messages, dependent: :destroy + has_many :exports, as: :user_profile + has_many :archives, as: :user_profile belongs_to :user diff --git a/app/services/procedure_archive_service.rb b/app/services/procedure_archive_service.rb index a2d86a51b..1025b1241 100644 --- a/app/services/procedure_archive_service.rb +++ b/app/services/procedure_archive_service.rb @@ -15,7 +15,7 @@ class ProcedureArchiveService dossiers.processed_in_month(archive.month) end - attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers, with_bills: true, include_infos_administration: true, with_champs_private: true) + attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers:, user_profile: archive.user_profile) DownloadableFileService.download_and_zip(@procedure, attachments, zip_root_folder(archive)) do |zip_filepath| ArchiveUploader.new(procedure: @procedure, filename: archive.filename(@procedure), filepath: zip_filepath) diff --git a/app/services/procedure_export_service.rb b/app/services/procedure_export_service.rb index 4769f2a8d..eb2b1dd93 100644 --- a/app/services/procedure_export_service.rb +++ b/app/services/procedure_export_service.rb @@ -1,9 +1,10 @@ class ProcedureExportService attr_reader :procedure, :dossiers - def initialize(procedure, dossiers) + def initialize(procedure, dossiers, user_profile) @procedure = procedure @dossiers = dossiers + @user_profile = user_profile end def to_csv diff --git a/db/migrate/20240202133353_add_user_profile_id_user_profile_type_to_exports.rb b/db/migrate/20240202133353_add_user_profile_id_user_profile_type_to_exports.rb new file mode 100644 index 000000000..f444f4742 --- /dev/null +++ b/db/migrate/20240202133353_add_user_profile_id_user_profile_type_to_exports.rb @@ -0,0 +1,6 @@ +class AddUserProfileIdUserProfileTypeToExports < ActiveRecord::Migration[7.0] + def change + add_column :exports, :user_profile_id, :bigint + add_column :exports, :user_profile_type, :string + end +end diff --git a/db/migrate/20240202133417_add_administrateur_id_to_archives.rb b/db/migrate/20240202133417_add_administrateur_id_to_archives.rb new file mode 100644 index 000000000..4b6e3c674 --- /dev/null +++ b/db/migrate/20240202133417_add_administrateur_id_to_archives.rb @@ -0,0 +1,6 @@ +class AddAdministrateurIdToArchives < ActiveRecord::Migration[7.0] + def change + add_column :archives, :user_profile_id, :bigint + add_column :archives, :user_profile_type, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index da44dc650..ad3dbb338 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -116,6 +116,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_15_164247) do t.date "month" t.string "time_span_type", null: false t.datetime "updated_at", null: false + t.bigint "user_profile_id" + t.string "user_profile_type" t.index ["key", "time_span_type", "month"], name: "index_archives_on_key_and_time_span_type_and_month", unique: true end @@ -586,6 +588,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_15_164247) do t.string "statut", default: "tous" t.string "time_span_type", default: "everything", null: false t.datetime "updated_at", precision: nil, null: false + t.bigint "user_profile_id" + t.string "user_profile_type" t.index ["instructeur_id"], name: "index_exports_on_instructeur_id" t.index ["key"], name: "index_exports_on_key" t.index ["procedure_presentation_id"], name: "index_exports_on_procedure_presentation_id" diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index 022c44aa6..7c9ad6af7 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -735,7 +735,7 @@ describe Instructeurs::ProceduresController, type: :controller do expect(flash.notice).to be_present end - it { expect { subject }.to change(Export, :count).by(1) } + it { expect { subject }.to change { Export.where(user_profile: instructeur).count }.by(1) } end context 'when the export is not ready' do diff --git a/spec/jobs/archive_creation_job_spec.rb b/spec/jobs/archive_creation_job_spec.rb index f21f985b6..66e4cf62e 100644 --- a/spec/jobs/archive_creation_job_spec.rb +++ b/spec/jobs/archive_creation_job_spec.rb @@ -1,6 +1,6 @@ describe ArchiveCreationJob, type: :job do describe 'perform' do - let(:archive) { create(:archive, job_status: status, groupe_instructeurs: [procedure.groupe_instructeurs.first]) } + let(:archive) { create(:archive, user_profile: instructeur, job_status: status, groupe_instructeurs: [procedure.groupe_instructeurs.first]) } let(:instructeur) { create(:instructeur) } let(:procedure) { create(:procedure, instructeurs: [instructeur]) } let(:job) { ArchiveCreationJob.new(procedure, archive, instructeur) } diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index cd2e82184..7f6dc04b3 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -3,6 +3,8 @@ describe Administrateur, type: :model do describe 'associations' do it { is_expected.to have_many(:commentaire_groupe_gestionnaires) } + it { is_expected.to have_many(:archives) } + it { is_expected.to have_many(:exports) } it { is_expected.to have_and_belong_to_many(:instructeurs) } it { is_expected.to belong_to(:groupe_gestionnaire).optional } end diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb index 4c508f244..5e7e1eda3 100644 --- a/spec/models/export_spec.rb +++ b/spec/models/export_spec.rb @@ -1,4 +1,12 @@ RSpec.describe Export, type: :model do + describe 'associations' do + let(:instructeur) { create(:instructeur) } + let(:export) { create(:export, user_profile_type: 'Instructeur', user_profile_id: instructeur.id) } + it 'find polymorphic association' do + expect(export.user_profile).to eq(instructeur) + end + end + describe 'validations' do let(:groupe_instructeur) { create(:groupe_instructeur) } diff --git a/spec/models/instructeur_spec.rb b/spec/models/instructeur_spec.rb index 249554774..d426fada2 100644 --- a/spec/models/instructeur_spec.rb +++ b/spec/models/instructeur_spec.rb @@ -13,6 +13,8 @@ describe Instructeur, type: :model do end describe 'associations' do + it { is_expected.to have_many(:archives) } + it { is_expected.to have_many(:exports) } it { is_expected.to have_and_belong_to_many(:administrateurs) } it { is_expected.to have_many(:batch_operations) } end diff --git a/spec/services/procedure_export_service_spec.rb b/spec/services/procedure_export_service_spec.rb index cdf42b2c8..e7196047b 100644 --- a/spec/services/procedure_export_service_spec.rb +++ b/spec/services/procedure_export_service_spec.rb @@ -1,8 +1,9 @@ require 'csv' describe ProcedureExportService do - let(:procedure) { create(:procedure, :published, :for_individual, :with_all_champs) } - let(:service) { ProcedureExportService.new(procedure, procedure.dossiers) } + let(:instructeur) { create(:instructeur) } + let(:procedure) { create(:procedure, :published, :for_individual, :with_all_champs, instructeurs: [instructeur]) } + let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur) } describe 'to_xlsx' do subject do @@ -235,7 +236,7 @@ describe ProcedureExportService do context 'as csv' do subject do - ProcedureExportService.new(procedure, procedure.dossiers) + ProcedureExportService.new(procedure, procedure.dossiers, instructeur) .to_csv .open { |f| CSV.read(f.path) } end