feat(procedure_presentation): pass filters as serialized snapshot

This commit is contained in:
Paul Chavard 2022-04-05 18:11:12 +02:00
parent e82dc9c8b5
commit 9904dabffe
4 changed files with 34 additions and 11 deletions

View file

@ -2,14 +2,15 @@
# #
# Table name: exports # Table name: exports
# #
# id :bigint not null, primary key # id :bigint not null, primary key
# format :string not null # format :string not null
# key :text not null # key :text not null
# statut :string default("tous") # procedure_presentation_snapshot :jsonb
# time_span_type :string default("everything"), not null # statut :string default("tous")
# created_at :datetime not null # time_span_type :string default("everything"), not null
# updated_at :datetime not null # created_at :datetime not null
# procedure_presentation_id :bigint # updated_at :datetime not null
# procedure_presentation_id :bigint
# #
class Export < ApplicationRecord class Export < ApplicationRecord
MAX_DUREE_CONSERVATION_EXPORT = 3.hours MAX_DUREE_CONSERVATION_EXPORT = 3.hours
@ -60,6 +61,8 @@ class Export < ApplicationRecord
end end
def compute def compute
load_snapshot!
file.attach( file.attach(
io: io, io: io,
filename: filename, filename: filename,
@ -78,7 +81,11 @@ class Export < ApplicationRecord
end end
def old? def old?
updated_at < 20.minutes.ago updated_at < 20.minutes.ago || filters_changed?
end
def filters_changed?
procedure_presentation&.snapshot != procedure_presentation_snapshot
end end
def filtered? def filtered?
@ -98,7 +105,7 @@ class Export < ApplicationRecord
end end
def self.find_or_create_export(format, groupe_instructeurs, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil) def self.find_or_create_export(format, groupe_instructeurs, time_span_type: time_span_types.fetch(:everything), statut: statuts.fetch(:tous), procedure_presentation: nil)
create_with(groupe_instructeurs: groupe_instructeurs, procedure_presentation: procedure_presentation) create_with(groupe_instructeurs: groupe_instructeurs, procedure_presentation: procedure_presentation, procedure_presentation_snapshot: procedure_presentation&.snapshot)
.includes(:procedure_presentation) .includes(:procedure_presentation)
.create_or_find_by(format: format, .create_or_find_by(format: format,
time_span_type: time_span_type, time_span_type: time_span_type,
@ -147,6 +154,12 @@ class Export < ApplicationRecord
private private
def load_snapshot!
if procedure_presentation_snapshot.present?
procedure_presentation.attributes = procedure_presentation_snapshot
end
end
def dossiers_for_export def dossiers_for_export
@dossiers_for_export ||= begin @dossiers_for_export ||= begin
dossiers = Dossier.where(groupe_instructeur: groupe_instructeurs) dossiers = Dossier.where(groupe_instructeur: groupe_instructeurs)

View file

@ -263,6 +263,10 @@ class ProcedurePresentation < ApplicationRecord
}) })
end end
def snapshot
slice(:filters, :sort, :displayed_fields)
end
private private
def field_id(field) def field_id(field)

View file

@ -0,0 +1,5 @@
class AddProcedurePresentationSnapshotToExports < ActiveRecord::Migration[6.1]
def change
add_column :exports, :procedure_presentation_snapshot, :jsonb
end
end

View file

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_04_05_110354) do ActiveRecord::Schema.define(version: 2022_04_05_163206) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -432,6 +432,7 @@ ActiveRecord::Schema.define(version: 2022_04_05_110354) do
t.string "format", null: false t.string "format", null: false
t.text "key", null: false t.text "key", null: false
t.bigint "procedure_presentation_id" t.bigint "procedure_presentation_id"
t.jsonb "procedure_presentation_snapshot"
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", null: false t.datetime "updated_at", null: false