Skip to content

Commit

Permalink
[#11103] Add include_archived flag on export
Browse files Browse the repository at this point in the history
  • Loading branch information
mmagn committed Dec 3, 2024
1 parent 0e3c799 commit 83ffb2f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

.fr-pb-2w
= render Dsfr::ToggleComponent.new(form: f,
target: :include_archived_dossiers,
target: :include_archived,
title: "Include les dossiers archivés",
label_side: :right)

Expand Down
6 changes: 3 additions & 3 deletions app/models/dossier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon

scope :with_type_de_champ, -> (stable_id) { joins(:champs).where(champs: { stream: 'main', stable_id: }) }

scope :all_state, -> { not_archived.state_not_brouillon }
scope :all_state, -> (include_archived: false) { include_archived ? state_not_brouillon : not_archived.state_not_brouillon }
scope :en_construction, -> { not_archived.state_en_construction }
scope :en_instruction, -> { not_archived.state_en_instruction }
scope :termine, -> { not_archived.state_termine }
Expand Down Expand Up @@ -384,7 +384,7 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon
.distinct
end

scope :by_statut, -> (statut, instructeur = nil) do
scope :by_statut, -> (statut, instructeur: nil, include_archived: false) do
case statut
when 'a-suivre'
visible_by_administration
Expand All @@ -398,7 +398,7 @@ def classer_sans_suite(motivation: nil, instructeur: nil, processed_at: Time.zon
when 'traites'
visible_by_administration.termine
when 'tous'
visible_by_administration.all_state
visible_by_administration.all_state(include_archived)
when 'supprimes'
hidden_by_administration.state_termine.or(hidden_by_expired)
when 'archives'
Expand Down
5 changes: 3 additions & 2 deletions app/models/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def since
time_span_type == Export.time_span_types.fetch(:monthly) ? 30.days.ago : nil
end

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, export_template: 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, export_template: nil, include_archived: false)
filtered_columns = Array.wrap(procedure_presentation&.filters_for(statut))
sorted_column = procedure_presentation&.sorted_column

Expand All @@ -78,6 +78,7 @@ def self.find_or_create_fresh_export(format, groupe_instructeurs, user_profile,
export_template:,
time_span_type:,
statut:,
include_archived:,
key: generate_cache_key(groupe_instructeurs.map(&:id), filtered_columns, sorted_column)
}

Expand Down Expand Up @@ -136,7 +137,7 @@ def dossiers_for_export
dossiers.visible_by_administration.where('dossiers.depose_at > ?', since)
elsif filtered_columns.present? || sorted_column.present?
instructeur = instructeur_from(user_profile)
filtered_sorted_ids = DossierFilterService.filtered_sorted_ids(dossiers, statut, filtered_columns, sorted_column, instructeur)
filtered_sorted_ids = DossierFilterService.filtered_sorted_ids(dossiers, statut, filtered_columns, sorted_column, instructeur, include_archived: include_archived)

dossiers.where(id: filtered_sorted_ids)
else
Expand Down
4 changes: 2 additions & 2 deletions app/services/dossier_filter_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class DossierFilterService
TYPE_DE_CHAMP = 'type_de_champ'

def self.filtered_sorted_ids(dossiers, statut, filters, sorted_column, instructeur, count: nil)
dossiers_by_statut = dossiers.by_statut(statut, instructeur)
def self.filtered_sorted_ids(dossiers, statut, filters, sorted_column, instructeur, count: nil, include_archived: false)
dossiers_by_statut = dossiers.by_statut(statut, instructeur, include_archived:)
dossiers_sorted_ids = self.sorted_ids(dossiers_by_statut, sorted_column, instructeur, count || dossiers_by_statut.size)

if filters.present?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddIncludeArchivedDossiersInExport < ActiveRecord::Migration[7.0]
def change
add_column :exports, :include_archived, :boolean, default: false, null: false
end
end
40 changes: 38 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_11_26_145420) do
ActiveRecord::Schema[7.0].define(version: 2024_12_03_154714) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_buffercache"
enable_extension "pg_stat_statements"
Expand Down Expand Up @@ -642,6 +642,7 @@
t.bigint "export_template_id"
t.jsonb "filtered_columns", default: [], null: false, array: true
t.string "format", null: false
t.boolean "include_archived", default: false, null: false
t.bigint "instructeur_id"
t.string "job_status", default: "pending", null: false
t.text "key", null: false
Expand Down Expand Up @@ -890,6 +891,15 @@
t.index ["from"], name: "index_path_rewrites_on_from", unique: true
end

create_table "procedure_paths", force: :cascade do |t|
t.datetime "created_at", null: false
t.string "path"
t.bigint "procedure_id", null: false
t.datetime "updated_at", null: false
t.index ["path"], name: "index_procedure_paths_on_path", unique: true
t.index ["procedure_id"], name: "index_procedure_paths_on_procedure_id"
end

create_table "procedure_presentations", id: :serial, force: :cascade do |t|
t.jsonb "a_suivre_filters", default: [], null: false, array: true
t.jsonb "archives_filters", default: [], null: false, array: true
Expand Down Expand Up @@ -937,7 +947,6 @@

create_table "procedure_tags", force: :cascade do |t|
t.datetime "created_at", null: false
t.text "description"
t.string "name", null: false
t.datetime "updated_at", null: false
t.index ["name"], name: "index_procedure_tags_on_name", unique: true
Expand Down Expand Up @@ -1008,6 +1017,7 @@
t.boolean "procedure_expires_when_termine_enabled", default: true
t.datetime "published_at", precision: nil
t.bigint "published_revision_id"
t.boolean "rdv_enabled", default: false, null: false
t.bigint "replaced_by_procedure_id"
t.boolean "routing_enabled"
t.bigint "service_id"
Expand Down Expand Up @@ -1044,6 +1054,26 @@
t.index ["zone_id"], name: "index_procedures_zones_on_zone_id"
end

create_table "rdv_connections", force: :cascade do |t|
t.string "access_token"
t.datetime "created_at", null: false
t.datetime "expires_at"
t.bigint "instructeur_id", null: false
t.string "refresh_token"
t.datetime "updated_at", null: false
t.index ["instructeur_id"], name: "index_rdv_connections_on_instructeur_id", unique: true
end

create_table "rdvs", force: :cascade do |t|
t.datetime "created_at", null: false
t.bigint "dossier_id", null: false
t.string "rdv_service_public_id"
t.datetime "starts_at"
t.string "status"
t.datetime "updated_at", null: false
t.index ["dossier_id"], name: "index_rdvs_on_dossier_id"
end

create_table "re_instructed_mails", force: :cascade do |t|
t.text "body"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -1293,6 +1323,7 @@
add_foreign_key "champs", "types_de_champ"
add_foreign_key "closed_mails", "procedures"
add_foreign_key "commentaires", "dossiers"
add_foreign_key "commentaires", "dossiers"
add_foreign_key "commentaires", "experts"
add_foreign_key "commentaires", "instructeurs"
add_foreign_key "contact_forms", "users"
Expand All @@ -1312,6 +1343,7 @@
add_foreign_key "dossiers", "groupe_instructeurs"
add_foreign_key "dossiers", "procedure_revisions", column: "revision_id"
add_foreign_key "dossiers", "users"
add_foreign_key "dossiers", "users"
add_foreign_key "etablissements", "dossiers"
add_foreign_key "experts", "users"
add_foreign_key "experts_procedures", "experts"
Expand All @@ -1326,6 +1358,7 @@
add_foreign_key "instructeurs", "users"
add_foreign_key "labels", "procedures"
add_foreign_key "merge_logs", "users"
add_foreign_key "procedure_paths", "procedures"
add_foreign_key "procedure_presentations", "assign_tos"
add_foreign_key "procedure_revision_types_de_champ", "procedure_revision_types_de_champ", column: "parent_id"
add_foreign_key "procedure_revision_types_de_champ", "procedure_revisions", column: "revision_id"
Expand All @@ -1337,12 +1370,15 @@
add_foreign_key "procedures", "procedure_revisions", column: "published_revision_id"
add_foreign_key "procedures", "services"
add_foreign_key "procedures", "zones"
add_foreign_key "rdv_connections", "instructeurs"
add_foreign_key "rdvs", "dossiers"
add_foreign_key "received_mails", "procedures"
add_foreign_key "refused_mails", "procedures"
add_foreign_key "services", "administrateurs"
add_foreign_key "targeted_user_links", "users"
add_foreign_key "traitements", "dossiers"
add_foreign_key "trusted_device_tokens", "instructeurs"
add_foreign_key "trusted_device_tokens", "instructeurs"
add_foreign_key "users", "users", column: "requested_merge_into_id"
add_foreign_key "without_continuation_mails", "procedures"
add_foreign_key "zone_labels", "zones"
Expand Down
11 changes: 10 additions & 1 deletion spec/services/dossier_filter_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@ def to_filter((label, filter)) = FilteredColumn.new(column: procedure.find_colum
let(:dossiers) { procedure.dossiers }
let(:statut) { 'suivis' }
let(:filters) { [] }
let(:include_archived) { false }
let(:sorted_columns) { procedure.default_sorted_column }

subject { described_class.filtered_sorted_ids(dossiers, statut, filters, sorted_columns, instructeur) }
subject { described_class.filtered_sorted_ids(dossiers, statut, filters, sorted_columns, instructeur, include_archived:) }

context 'with no filters' do
let(:en_construction_dossier) { create(:dossier, :en_construction, procedure:) }
let(:accepte_dossier) { create(:dossier, :accepte, procedure:) }
let(:archived_dossier) { create(:dossier, :archived, procedure:) }

before do
create(:follow, dossier: en_construction_dossier, instructeur:)
create(:follow, dossier: accepte_dossier, instructeur:)
create(:follow, dossier: archived_dossier, instructeur:)
end

it { is_expected.to contain_exactly(en_construction_dossier.id) }

context 'when include_archived is true' do
let(:include_archived) { true }

it { is_expected.to contain_exactly(en_construction_dossier.id, archived_dossier.id) }
end
end

context 'with mocked sorted_ids' do
Expand Down

0 comments on commit 83ffb2f

Please sign in to comment.