diff --git a/app/models/export.rb b/app/models/export.rb index caef9ce48..05d8f04a7 100644 --- a/app/models/export.rb +++ b/app/models/export.rb @@ -2,12 +2,14 @@ # # Table name: exports # -# id :bigint not null, primary key -# format :string not null -# key :text not null -# time_span_type :string default("everything"), not null -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint not null, primary key +# format :string not null +# key :text not null +# statut :string default("tous") +# time_span_type :string default("everything"), not null +# created_at :datetime not null +# updated_at :datetime not null +# procedure_presentation_id :bigint # class Export < ApplicationRecord MAX_DUREE_CONSERVATION_EXPORT = 3.hours @@ -23,7 +25,18 @@ class Export < ApplicationRecord monthly: 'monthly' } + enum statut: { + 'a-suivre': 'a-suivre', + suivis: 'suivis', + traites: 'traites', + tous: 'tous', + supprimes_recemment: 'supprimes_recemment', + archives: 'archives', + expirant: 'expirant' + } + has_and_belongs_to_many :groupe_instructeurs + belongs_to :procedure_presentation, optional: true has_one_attached :file diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 1c39669d5..5eaf64138 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -25,6 +25,8 @@ class ProcedurePresentation < ApplicationRecord FILTERS_VALUE_MAX_LENGTH = 100 belongs_to :assign_to, optional: false + has_many :exports, dependent: :destroy + delegate :procedure, :instructeur, to: :assign_to validate :check_allowed_displayed_fields diff --git a/db/migrate/20220323143325_add_procedure_presentation_and_state_to_exports.rb b/db/migrate/20220323143325_add_procedure_presentation_and_state_to_exports.rb new file mode 100644 index 000000000..01921efb4 --- /dev/null +++ b/db/migrate/20220323143325_add_procedure_presentation_and_state_to_exports.rb @@ -0,0 +1,10 @@ +class AddProcedurePresentationAndStateToExports < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + def change + add_reference :exports, :procedure_presentation, null: true, index: { algorithm: :concurrently } + add_column :exports, :statut, :string, default: 'tous' + remove_index :exports, [:format, :time_span_type, :key] + add_index :exports, [:format, :time_span_type, :statut, :key], unique: true, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index 11feb6627..36b33644d 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: 2022_04_05_100354) do +ActiveRecord::Schema.define(version: 2022_04_05_110354) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -431,9 +431,12 @@ ActiveRecord::Schema.define(version: 2022_04_05_100354) do t.datetime "created_at", null: false t.string "format", null: false t.text "key", null: false + t.bigint "procedure_presentation_id" + t.string "statut", default: "tous" t.string "time_span_type", default: "everything", null: false t.datetime "updated_at", null: false - t.index ["format", "time_span_type", "key"], name: "index_exports_on_format_and_time_span_type_and_key", unique: true + t.index ["format", "time_span_type", "statut", "key"], name: "index_exports_on_format_and_time_span_type_and_statut_and_key", unique: true + t.index ["procedure_presentation_id"], name: "index_exports_on_procedure_presentation_id" end create_table "exports_groupe_instructeurs", force: :cascade do |t| diff --git a/spec/factories/export.rb b/spec/factories/export.rb index 3069ca238..c7ebda9b4 100644 --- a/spec/factories/export.rb +++ b/spec/factories/export.rb @@ -1,11 +1,12 @@ FactoryBot.define do factory :export do - format { :csv } + format { Export.formats.fetch(:csv) } + statut { Export.statuts.fetch(:tous) } time_span_type { Export.time_span_types.fetch(:everything) } groupe_instructeurs { [association(:groupe_instructeur)] } after(:build) do |export, _evaluator| - export.key = Export.generate_cache_key(export.groupe_instructeurs.map(&:id)) + export.key = Export.generate_cache_key(export.groupe_instructeurs.map(&:id), export.procedure_presentation&.id) end end end