refactor(export/archives): holds user profile asking for the archive or export
This commit is contained in:
parent
4dfb3b86ef
commit
72a3f6e89c
18 changed files with 51 additions and 17 deletions
|
@ -14,7 +14,7 @@ module Administrateurs
|
||||||
|
|
||||||
def create
|
def create
|
||||||
type = params[:type]
|
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?
|
if archive.pending?
|
||||||
ArchiveCreationJob.perform_later(@procedure, archive, current_administrateur)
|
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."
|
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."
|
||||||
|
|
|
@ -4,7 +4,7 @@ module Administrateurs
|
||||||
before_action :ensure_not_super_admin!
|
before_action :ensure_not_super_admin!
|
||||||
|
|
||||||
def download
|
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
|
@dossiers_count = export.count
|
||||||
|
|
||||||
if export.available?
|
if export.available?
|
||||||
|
|
|
@ -13,7 +13,7 @@ module Instructeurs
|
||||||
|
|
||||||
def create
|
def create
|
||||||
type = params[:type]
|
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?
|
if archive.pending?
|
||||||
ArchiveCreationJob.perform_later(@procedure, archive, current_instructeur)
|
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."
|
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."
|
||||||
|
|
|
@ -9,7 +9,8 @@ class Administrateur < ApplicationRecord
|
||||||
has_many :api_tokens, inverse_of: :administrateur, dependent: :destroy
|
has_many :api_tokens, inverse_of: :administrateur, dependent: :destroy
|
||||||
has_many :commentaire_groupe_gestionnaires, as: :sender
|
has_many :commentaire_groupe_gestionnaires, as: :sender
|
||||||
has_and_belongs_to_many :default_zones, class_name: 'Zone', join_table: 'default_zones_administrateurs'
|
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 :user
|
||||||
belongs_to :groupe_gestionnaire, optional: true
|
belongs_to :groupe_gestionnaire, optional: true
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ class Archive < ApplicationRecord
|
||||||
has_and_belongs_to_many :groupe_instructeurs
|
has_and_belongs_to_many :groupe_instructeurs
|
||||||
|
|
||||||
has_one_attached :file
|
has_one_attached :file
|
||||||
|
belongs_to :user_profile, polymorphic: true, optional: true
|
||||||
|
|
||||||
scope :for_groupe_instructeur, -> (groupe_instructeur) {
|
scope :for_groupe_instructeur, -> (groupe_instructeur) {
|
||||||
joins(:archives_groupe_instructeurs)
|
joins(:archives_groupe_instructeurs)
|
||||||
|
@ -30,9 +31,9 @@ class Archive < ApplicationRecord
|
||||||
end
|
end
|
||||||
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_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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -30,6 +30,7 @@ class Export < ApplicationRecord
|
||||||
has_and_belongs_to_many :groupe_instructeurs
|
has_and_belongs_to_many :groupe_instructeurs
|
||||||
belongs_to :procedure_presentation, optional: true
|
belongs_to :procedure_presentation, optional: true
|
||||||
belongs_to :instructeur, optional: true
|
belongs_to :instructeur, optional: true
|
||||||
|
belongs_to :user_profile, polymorphic: true, optional: true
|
||||||
|
|
||||||
has_one_attached :file
|
has_one_attached :file
|
||||||
|
|
||||||
|
@ -65,7 +66,7 @@ class Export < ApplicationRecord
|
||||||
procedure_presentation_id.present?
|
procedure_presentation_id.present?
|
||||||
end
|
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 = {
|
attributes = {
|
||||||
format:,
|
format:,
|
||||||
time_span_type:,
|
time_span_type:,
|
||||||
|
@ -81,7 +82,7 @@ class Export < ApplicationRecord
|
||||||
return recent_export if recent_export.present?
|
return recent_export if recent_export.present?
|
||||||
|
|
||||||
create!(**attributes, groupe_instructeurs:,
|
create!(**attributes, groupe_instructeurs:,
|
||||||
instructeur:,
|
user_profile:,
|
||||||
procedure_presentation:,
|
procedure_presentation:,
|
||||||
procedure_presentation_snapshot: procedure_presentation&.snapshot)
|
procedure_presentation_snapshot: procedure_presentation&.snapshot)
|
||||||
end
|
end
|
||||||
|
@ -146,7 +147,7 @@ class Export < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def blob
|
def blob
|
||||||
service = ProcedureExportService.new(procedure, dossiers_for_export)
|
service = ProcedureExportService.new(procedure, dossiers_for_export, user_profile)
|
||||||
|
|
||||||
case format.to_sym
|
case format.to_sym
|
||||||
when :csv
|
when :csv
|
||||||
|
|
|
@ -18,8 +18,9 @@ class Instructeur < ApplicationRecord
|
||||||
has_many :followed_dossiers, through: :follows, source: :dossier
|
has_many :followed_dossiers, through: :follows, source: :dossier
|
||||||
has_many :previously_followed_dossiers, -> { distinct }, through: :previous_follows, source: :dossier
|
has_many :previously_followed_dossiers, -> { distinct }, through: :previous_follows, source: :dossier
|
||||||
has_many :trusted_device_tokens, dependent: :destroy
|
has_many :trusted_device_tokens, dependent: :destroy
|
||||||
has_many :archives
|
|
||||||
has_many :bulk_messages, dependent: :destroy
|
has_many :bulk_messages, dependent: :destroy
|
||||||
|
has_many :exports, as: :user_profile
|
||||||
|
has_many :archives, as: :user_profile
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ class ProcedureArchiveService
|
||||||
dossiers.processed_in_month(archive.month)
|
dossiers.processed_in_month(archive.month)
|
||||||
end
|
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|
|
DownloadableFileService.download_and_zip(@procedure, attachments, zip_root_folder(archive)) do |zip_filepath|
|
||||||
ArchiveUploader.new(procedure: @procedure, filename: archive.filename(@procedure), filepath: zip_filepath)
|
ArchiveUploader.new(procedure: @procedure, filename: archive.filename(@procedure), filepath: zip_filepath)
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
class ProcedureExportService
|
class ProcedureExportService
|
||||||
attr_reader :procedure, :dossiers
|
attr_reader :procedure, :dossiers
|
||||||
|
|
||||||
def initialize(procedure, dossiers)
|
def initialize(procedure, dossiers, user_profile)
|
||||||
@procedure = procedure
|
@procedure = procedure
|
||||||
@dossiers = dossiers
|
@dossiers = dossiers
|
||||||
|
@user_profile = user_profile
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_csv
|
def to_csv
|
||||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -116,6 +116,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_15_164247) do
|
||||||
t.date "month"
|
t.date "month"
|
||||||
t.string "time_span_type", null: false
|
t.string "time_span_type", null: false
|
||||||
t.datetime "updated_at", 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
|
t.index ["key", "time_span_type", "month"], name: "index_archives_on_key_and_time_span_type_and_month", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -586,6 +588,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_02_15_164247) do
|
||||||
t.string "statut", default: "tous"
|
t.string "statut", default: "tous"
|
||||||
t.string "time_span_type", default: "everything", null: false
|
t.string "time_span_type", default: "everything", null: false
|
||||||
t.datetime "updated_at", precision: nil, 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 ["instructeur_id"], name: "index_exports_on_instructeur_id"
|
||||||
t.index ["key"], name: "index_exports_on_key"
|
t.index ["key"], name: "index_exports_on_key"
|
||||||
t.index ["procedure_presentation_id"], name: "index_exports_on_procedure_presentation_id"
|
t.index ["procedure_presentation_id"], name: "index_exports_on_procedure_presentation_id"
|
||||||
|
|
|
@ -735,7 +735,7 @@ describe Instructeurs::ProceduresController, type: :controller do
|
||||||
expect(flash.notice).to be_present
|
expect(flash.notice).to be_present
|
||||||
end
|
end
|
||||||
|
|
||||||
it { expect { subject }.to change(Export, :count).by(1) }
|
it { expect { subject }.to change { Export.where(user_profile: instructeur).count }.by(1) }
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the export is not ready' do
|
context 'when the export is not ready' do
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
describe ArchiveCreationJob, type: :job do
|
describe ArchiveCreationJob, type: :job do
|
||||||
describe 'perform' 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(:instructeur) { create(:instructeur) }
|
||||||
let(:procedure) { create(:procedure, instructeurs: [instructeur]) }
|
let(:procedure) { create(:procedure, instructeurs: [instructeur]) }
|
||||||
let(:job) { ArchiveCreationJob.new(procedure, archive, instructeur) }
|
let(:job) { ArchiveCreationJob.new(procedure, archive, instructeur) }
|
||||||
|
|
|
@ -3,6 +3,8 @@ describe Administrateur, type: :model do
|
||||||
|
|
||||||
describe 'associations' do
|
describe 'associations' do
|
||||||
it { is_expected.to have_many(:commentaire_groupe_gestionnaires) }
|
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 have_and_belong_to_many(:instructeurs) }
|
||||||
it { is_expected.to belong_to(:groupe_gestionnaire).optional }
|
it { is_expected.to belong_to(:groupe_gestionnaire).optional }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
RSpec.describe Export, type: :model do
|
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
|
describe 'validations' do
|
||||||
let(:groupe_instructeur) { create(:groupe_instructeur) }
|
let(:groupe_instructeur) { create(:groupe_instructeur) }
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,8 @@ describe Instructeur, type: :model do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'associations' do
|
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_and_belong_to_many(:administrateurs) }
|
||||||
it { is_expected.to have_many(:batch_operations) }
|
it { is_expected.to have_many(:batch_operations) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
describe ProcedureExportService do
|
describe ProcedureExportService do
|
||||||
let(:procedure) { create(:procedure, :published, :for_individual, :with_all_champs) }
|
let(:instructeur) { create(:instructeur) }
|
||||||
let(:service) { ProcedureExportService.new(procedure, procedure.dossiers) }
|
let(:procedure) { create(:procedure, :published, :for_individual, :with_all_champs, instructeurs: [instructeur]) }
|
||||||
|
let(:service) { ProcedureExportService.new(procedure, procedure.dossiers, instructeur) }
|
||||||
|
|
||||||
describe 'to_xlsx' do
|
describe 'to_xlsx' do
|
||||||
subject do
|
subject do
|
||||||
|
@ -235,7 +236,7 @@ describe ProcedureExportService do
|
||||||
|
|
||||||
context 'as csv' do
|
context 'as csv' do
|
||||||
subject do
|
subject do
|
||||||
ProcedureExportService.new(procedure, procedure.dossiers)
|
ProcedureExportService.new(procedure, procedure.dossiers, instructeur)
|
||||||
.to_csv
|
.to_csv
|
||||||
.open { |f| CSV.read(f.path) }
|
.open { |f| CSV.read(f.path) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue