refactor(export/archives): holds user profile asking for the archive or export

This commit is contained in:
Martin 2024-02-07 17:19:50 +01:00
parent 4dfb3b86ef
commit 72a3f6e89c
18 changed files with 51 additions and 17 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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