From 0399bb7dcf87b87ba2113263c6144d31026a49e4 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 11:33:45 +0100 Subject: [PATCH 01/17] cosmetic move --- app/controllers/instructeurs/procedures_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index aca2ddbe9..ae0232381 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -44,14 +44,14 @@ module Instructeurs def show @procedure = procedure - - @current_filters = current_filters @available_fields_to_filters = available_fields_to_filters # Technically, procedure_presentation already sets the attribute. # Setting it here to make clear that it is used by the view @procedure_presentation = procedure_presentation @displayed_fields_values = displayed_fields_values + @current_filters = current_filters + @a_suivre_dossiers = current_instructeur .dossiers .for_procedure(procedure) From 55998b128486197c437e0c7a5e79fb0ec412c784 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 15:06:51 +0100 Subject: [PATCH 02/17] remove unused dossier_field service --- app/models/procedure_presentation.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index da3ede8ba..c5e6e493c 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -309,10 +309,6 @@ class ProcedurePresentation < ApplicationRecord .join('.') end - def dossier_field_service - @dossier_field_service ||= DossierFieldService.new - end - def assert_supported_column(table, column) if table == 'followers_instructeurs' && column != 'email' raise ArgumentError, 'Table `followers_instructeurs` only supports the `email` column.' From cbccdea8e0ffd63c1829edddb094570e84b0c12e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 11:38:58 +0100 Subject: [PATCH 03/17] move displayed_fields for select to procedure_controller --- .../instructeurs/procedures_controller.rb | 15 +-------------- app/models/procedure_presentation.rb | 15 ++++++++++----- app/views/instructeurs/procedures/show.html.haml | 7 +++---- spec/models/procedure_presentation_spec.rb | 8 ++++---- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index ae0232381..108be8994 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -44,13 +44,12 @@ module Instructeurs def show @procedure = procedure - @available_fields_to_filters = available_fields_to_filters # Technically, procedure_presentation already sets the attribute. # Setting it here to make clear that it is used by the view @procedure_presentation = procedure_presentation - @displayed_fields_values = displayed_fields_values @current_filters = current_filters + @displayed_fields_options, @displayed_fields_selected = procedure_presentation.displayed_fields_for_select @a_suivre_dossiers = current_instructeur .dossiers @@ -275,10 +274,6 @@ module Instructeurs procedure_presentation.fields.find { |c| c['table'] == table && c['column'] == column } end - def field_id(field) - field.values_at('table', 'column').join('/') - end - def assign_to current_instructeur.assign_to.joins(:groupe_instructeur).find_by(groupe_instructeurs: { procedure: procedure }) end @@ -316,18 +311,10 @@ module Instructeurs procedure_presentation end - def displayed_fields_values - procedure_presentation.displayed_fields.map { |field| field_id(field) } - end - def current_filters @current_filters ||= procedure_presentation.filters[statut] end - def available_fields_to_filters - procedure_presentation.fields_for_select - end - def kaminarize(current_page, total) @dossiers.instance_eval <<-EVAL def current_page diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index c5e6e493c..e03014552 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -75,13 +75,14 @@ class ProcedurePresentation < ApplicationRecord fields end - def fields_for_select - fields.map do |field| - [field['label'], "#{field['table']}/#{field['column']}"] - end + def displayed_fields_for_select + [ + fields.map { |field| [field['label'], field_id(field)] }, + displayed_fields.map { |field| field_id(field) } + ] end - def displayed_field_values(dossier) + def displayed_fields_values(dossier) displayed_fields.map { |field| get_value(dossier, field['table'], field['column']) } end @@ -219,6 +220,10 @@ class ProcedurePresentation < ApplicationRecord private + def field_id(field) + field.values_at('table', 'column').join('/') + end + def find_field(table, column) fields.find { |c| c['table'] == table && c['column'] == column } end diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index f8f4b395f..412fadc0c 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -83,7 +83,7 @@ #filter-menu.dropdown-content.left-aligned.fade-in-down = form_tag add_filter_instructeur_procedure_path(@procedure), method: :post, class: 'dropdown-form large' do = label_tag :field, "Colonne" - = select_tag :field, options_for_select(@available_fields_to_filters) + = select_tag :field, options_for_select(@displayed_fields_options) %br = label_tag :value, "Valeur" = text_field_tag :value @@ -123,8 +123,7 @@ #custom-menu.dropdown-content.fade-in-down = form_tag update_displayed_fields_instructeur_procedure_path(@procedure), method: :patch, class: 'dropdown-form columns-form' do = select_tag :values, - options_for_select(@procedure_presentation.fields_for_select, - selected: @displayed_fields_values), + options_for_select(@displayed_fields_options, selected: @displayed_fields_selected), multiple: true, class: 'select2-limited' = submit_tag "Enregistrer", class: 'button' @@ -142,7 +141,7 @@ = link_to(instructeur_dossier_path(@procedure, dossier), class: 'cell-link') do = dossier.id - - @procedure_presentation.displayed_field_values(dossier).each do |value| + - @procedure_presentation.displayed_fields_values(dossier).each do |value| %td / FIXME: value should automatically fallback to `""` instead of nil / #get_value should call to_s on the champ diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 1c118c7ad..0f237d925 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -104,7 +104,7 @@ describe ProcedurePresentation do end end - describe "#fields_for_select" do + describe "#displayed_fields_for_select" do subject { create(:procedure_presentation, assign_to: assign_to) } before do @@ -122,13 +122,13 @@ describe ProcedurePresentation do ]) end - it { expect(subject.fields_for_select).to eq([["label1", "table1/column1"], ["label2", "table2/column2"]]) } + it { expect(subject.displayed_fields_for_select).to eq([[["label1", "table1/column1"], ["label2", "table2/column2"]], ["user/email"]]) } end describe '#get_value' do let(:procedure_presentation) { create(:procedure_presentation, procedure: procedure, assign_to: assign_to, displayed_fields: [{ 'table' => table, 'column' => column }]) } - subject { procedure_presentation.displayed_field_values(dossier).first } + subject { procedure_presentation.displayed_fields_values(dossier).first } context 'for self table' do let(:table) { 'self' } @@ -217,7 +217,7 @@ describe ProcedurePresentation do let!(:follow2) { create(:follow, dossier: dossier, instructeur: create(:instructeur, email: 'user2@host')) } it "return emails of followers instructeurs" do - emails_to_display = procedure_presentation.displayed_field_values(dossier).first.split(', ').sort + emails_to_display = procedure_presentation.displayed_fields_values(dossier).first.split(', ').sort expect(emails_to_display).to eq ["user1@host", "user2@host"] end end From b7a43f3f44c1a05bf8448e10a3b9609be793c763 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 13:33:42 +0100 Subject: [PATCH 04/17] move update_displayed_fields to procedure_presentation --- .../instructeurs/procedures_controller.rb | 21 +------------------ app/models/procedure_presentation.rb | 14 +++++++++++++ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 108be8994..bde4cfaa4 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -138,22 +138,7 @@ module Instructeurs end def update_displayed_fields - values = params[:values] - - if values.nil? - values = [] - end - - fields = values.map do |value| - find_field(*value.split('/')) - end - - procedure_presentation.update(displayed_fields: fields) - - current_sort = procedure_presentation.sort - if !values.include?(field_id(current_sort)) - procedure_presentation.update(sort: Procedure.default_sort) - end + procedure_presentation.update_displayed_fields(params[:values]) redirect_back(fallback_location: instructeur_procedure_url(procedure)) end @@ -270,10 +255,6 @@ module Instructeurs @ods_export = Export.find_for_format_and_groupe_instructeurs(:ods, groupe_instructeurs_for_procedure) end - def find_field(table, column) - procedure_presentation.fields.find { |c| c['table'] == table && c['column'] == column } - end - def assign_to current_instructeur.assign_to.joins(:groupe_instructeur).find_by(groupe_instructeurs: { procedure: procedure }) end diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index e03014552..d4da7f239 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -218,6 +218,20 @@ class ProcedurePresentation < ApplicationRecord end end + + def update_displayed_fields(values) + if values.nil? + values = [] + end + + fields = values.map { |value| find_field(*value.split('/')) } + + update!(displayed_fields: fields) + + if !values.include?(field_id(sort)) + update!(sort: Procedure.default_sort) + end + end private def field_id(field) From e3ee756da491ea06d246bff3695760779a1e59d2 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 14:31:24 +0100 Subject: [PATCH 05/17] move update_sort --- .../instructeurs/procedures_controller.rb | 18 +----------------- app/models/procedure_presentation.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index bde4cfaa4..533987cf9 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -144,23 +144,7 @@ module Instructeurs end def update_sort - current_sort = procedure_presentation.sort - table = params[:table] - column = params[:column] - - if table == current_sort['table'] && column == current_sort['column'] - order = current_sort['order'] == 'asc' ? 'desc' : 'asc' - else - order = 'asc' - end - - sort = { - 'table' => table, - 'column' => column, - 'order' => order - } - - procedure_presentation.update(sort: sort) + procedure_presentation.update_sort(params[:table], params[:column]) redirect_back(fallback_location: instructeur_procedure_url(procedure)) end diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index d4da7f239..18aa50bf0 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -232,6 +232,21 @@ class ProcedurePresentation < ApplicationRecord update!(sort: Procedure.default_sort) end end + + def update_sort(table, column) + order = if sort.values_at('table', 'column') == [table, column] + sort['order'] == 'asc' ? 'desc' : 'asc' + else + 'asc' + end + + update!(sort: { + 'table' => table, + 'column' => column, + 'order' => order + }) + end + private def field_id(field) From 4241ea05f371d62cba70490629a6c472485fe69a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 14:32:53 +0100 Subject: [PATCH 06/17] move add_filter --- app/controllers/instructeurs/procedures_controller.rb | 4 +--- app/models/procedure_presentation.rb | 7 +++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 533987cf9..f22d44d38 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -150,9 +150,7 @@ module Instructeurs end def add_filter - if params[:value].present? - procedure_presentation.add_filter(statut, params[:field], params[:value]) - end + procedure_presentation.add_filter(statut, params[:field], params[:value]) redirect_back(fallback_location: instructeur_procedure_url(procedure)) end diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 18aa50bf0..d0eb61ec6 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -197,16 +197,15 @@ class ProcedurePresentation < ApplicationRecord def add_filter(statut, field, value) if value.present? - updated_filters = self.filters table, column = field.split('/') label = find_field(table, column)['label'] case table when 'type_de_champ', 'type_de_champ_private' - type_de_champ = TypeDeChamp.find_by(id: column) - value = type_de_champ.dynamic_type.human_to_filter(value) + value = find_type_de_champ(column).dynamic_type.human_to_filter(value) end + updated_filters = filters.dup updated_filters[statut] << { 'label' => label, 'table' => table, @@ -214,7 +213,7 @@ class ProcedurePresentation < ApplicationRecord 'value' => value } - update(filters: updated_filters) + update!(filters: updated_filters) end end From e5da1d7111ca718b4e12ed971866a53f4ea594ea Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 14:34:00 +0100 Subject: [PATCH 07/17] move remove_filter --- app/controllers/instructeurs/procedures_controller.rb | 7 +------ app/models/procedure_presentation.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index f22d44d38..e389ab07d 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -156,12 +156,7 @@ module Instructeurs end def remove_filter - filters = procedure_presentation.filters - - to_remove = params.values_at(:table, :column, :value) - filters[statut].reject! { |filter| filter.values_at('table', 'column', 'value') == to_remove } - - procedure_presentation.update(filters: filters) + procedure_presentation.remove_filter(statut, params[:field], params[:value]) redirect_back(fallback_location: instructeur_procedure_url(procedure)) end diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index d0eb61ec6..7ee650812 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -217,6 +217,16 @@ class ProcedurePresentation < ApplicationRecord end end + def remove_filter(statut, field, value) + table, column = field.split('/') + + updated_filters = filters.dup + updated_filters[statut] = filters[statut].reject do |filter| + filter.values_at('table', 'column', 'value') == [table, column, value] + end + + update!(filters: updated_filters) + end def update_displayed_fields(values) if values.nil? From f37c27a243c526b4d75cb0534650126e5ee2d6ae Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 14:44:42 +0100 Subject: [PATCH 08/17] nicer find_field --- app/models/procedure_presentation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 7ee650812..2955db5ba 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -263,7 +263,7 @@ class ProcedurePresentation < ApplicationRecord end def find_field(table, column) - fields.find { |c| c['table'] == table && c['column'] == column } + fields.find { |field| field.values_at('table', 'column') == [table, column] } end def check_allowed_displayed_fields From f07b395d95b56eb99373bc8d9ac41309a6198439 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 14:51:19 +0100 Subject: [PATCH 09/17] sort : remove superfluous return --- app/models/procedure_presentation.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 2955db5ba..2a7d84549 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -93,10 +93,10 @@ class ProcedurePresentation < ApplicationRecord when 'notifications' dossiers_id_with_notification = dossiers.merge(instructeur.followed_dossiers).with_notifications.ids if order == 'desc' - return dossiers_id_with_notification + + dossiers_id_with_notification + (dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification) else - return (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + + (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + dossiers_id_with_notification end when 'type_de_champ', 'type_de_champ_private' @@ -108,15 +108,15 @@ class ProcedurePresentation < ApplicationRecord when 'followers_instructeurs' assert_supported_column(table, column) # LEFT OUTER JOIN allows to keep dossiers without assignated instructeurs yet - return dossiers - .includes(:followers_instructeurs) - .joins('LEFT OUTER JOIN users instructeurs_users ON instructeurs_users.instructeur_id = instructeurs.id') - .order("instructeurs_users.email #{order}") - .pluck(:id) + dossiers + .includes(:followers_instructeurs) + .joins('LEFT OUTER JOIN users instructeurs_users ON instructeurs_users.instructeur_id = instructeurs.id') + .order("instructeurs_users.email #{order}") + .pluck(:id) when 'self', 'user', 'individual', 'etablissement', 'groupe_instructeur' - return (table == 'self' ? dossiers : dossiers.includes(table)) - .order("#{self.class.sanitized_column(table, column)} #{order}") - .pluck(:id) + (table == 'self' ? dossiers : dossiers.includes(table)) + .order("#{self.class.sanitized_column(table, column)} #{order}") + .pluck(:id) end end From 4960eda5cb3c0d34abb1e64ceadd02cd319fc4ae Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 15:01:13 +0100 Subject: [PATCH 10/17] use stable id --- app/models/dossier.rb | 10 ++++++ app/models/procedure_presentation.rb | 36 ++++++++++++---------- spec/models/procedure_presentation_spec.rb | 36 +++++++++++----------- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index e9bc7da9b..165209d1d 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -152,6 +152,16 @@ class Dossier < ApplicationRecord scope :updated_since, -> (since) { where('dossiers.updated_at >= ?', since) } scope :created_since, -> (since) { where('dossiers.en_construction_at >= ?', since) } + scope :with_type_de_champ, -> (stable_id) { + joins('INNER JOIN champs ON champs.dossier_id = dossiers.id INNER JOIN types_de_champ ON types_de_champ.id = champs.type_de_champ_id') + .where('types_de_champ.private = FALSE AND types_de_champ.stable_id = ?', stable_id) + } + + scope :with_type_de_champ_private, -> (stable_id) { + joins('INNER JOIN champs ON champs.dossier_id = dossiers.id INNER JOIN types_de_champ ON types_de_champ.id = champs.type_de_champ_id') + .where('types_de_champ.private = TRUE AND types_de_champ.stable_id = ?', stable_id) + } + scope :all_state, -> { not_archived.state_not_brouillon } scope :en_construction, -> { not_archived.state_en_construction } scope :en_instruction, -> { not_archived.state_en_instruction } diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 2a7d84549..0a80c44d9 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -65,12 +65,12 @@ class ProcedurePresentation < ApplicationRecord fields.concat procedure.types_de_champ .where.not(type_champ: explanatory_types_de_champ) .order(:id) - .map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.id.to_s) } + .map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ', type_de_champ.stable_id.to_s) } fields.concat procedure.types_de_champ_private .where.not(type_champ: explanatory_types_de_champ) .order(:id) - .map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.id.to_s) } + .map { |type_de_champ| field_hash(type_de_champ.libelle, 'type_de_champ_private', type_de_champ.stable_id.to_s) } fields end @@ -99,12 +99,16 @@ class ProcedurePresentation < ApplicationRecord (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + dossiers_id_with_notification end - when 'type_de_champ', 'type_de_champ_private' - return dossiers - .includes(table == 'type_de_champ' ? :champs : :champs_private) - .where("champs.type_de_champ_id = #{column.to_i}") - .order("champs.value #{order}") - .pluck(:id) + when 'type_de_champ' + dossiers + .with_type_de_champ(column) + .order("champs.value #{order}") + .pluck(:id) + when 'type_de_champ_private' + dossiers + .with_type_de_champ_private(column) + .order("champs.value #{order}") + .pluck(:id) when 'followers_instructeurs' assert_supported_column(table, column) # LEFT OUTER JOIN allows to keep dossiers without assignated instructeurs yet @@ -129,12 +133,12 @@ class ProcedurePresentation < ApplicationRecord .map { |v| Time.zone.parse(v).beginning_of_day rescue nil } .compact dossiers.filter_by_datetimes(column, dates) - when 'type_de_champ', 'type_de_champ_private' - relation = table == 'type_de_champ' ? :champs : :champs_private - dossiers - .includes(relation) - .where("champs.type_de_champ_id = ?", column.to_i) - .filter_ilike(relation, :value, values) + when 'type_de_champ' + dossiers.with_type_de_champ(column) + .filter_ilike(:champs, :value, values) + when 'type_de_champ_private' + dossiers.with_type_de_champ_private(column) + .filter_ilike(:champs_private, :value, values) when 'etablissement' if column == 'entreprise_date_creation' dates = values @@ -307,9 +311,9 @@ class ProcedurePresentation < ApplicationRecord when 'followers_instructeurs' dossier.send(table)&.map { |g| g.send(column) }&.join(', ') when 'type_de_champ' - dossier.champs.find { |c| c.type_de_champ_id == column.to_i }.value + dossier.champs.find { |c| c.stable_id == column.to_i }.to_s when 'type_de_champ_private' - dossier.champs_private.find { |c| c.type_de_champ_id == column.to_i }.value + dossier.champs_private.find { |c| c.stable_id == column.to_i }.to_s when 'groupe_instructeur' dossier.groupe_instructeur.label end diff --git a/spec/models/procedure_presentation_spec.rb b/spec/models/procedure_presentation_spec.rb index 0f237d925..3a0c62924 100644 --- a/spec/models/procedure_presentation_spec.rb +++ b/spec/models/procedure_presentation_spec.rb @@ -3,7 +3,7 @@ describe ProcedurePresentation do let(:instructeur) { create(:instructeur) } let(:assign_to) { create(:assign_to, procedure: procedure, instructeur: instructeur) } let(:first_type_de_champ) { assign_to.procedure.types_de_champ.first } - let(:first_type_de_champ_id) { first_type_de_champ.id.to_s } + let(:first_type_de_champ_id) { first_type_de_champ.stable_id.to_s } let(:procedure_presentation) { create(:procedure_presentation, assign_to: assign_to, @@ -72,10 +72,10 @@ describe ProcedurePresentation do { "label" => 'SIRET', "table" => 'etablissement', "column" => 'siret' }, { "label" => 'Libellé NAF', "table" => 'etablissement', "column" => 'libelle_naf' }, { "label" => 'Code postal', "table" => 'etablissement', "column" => 'code_postal' }, - { "label" => tdc_1.libelle, "table" => 'type_de_champ', "column" => tdc_1.id.to_s }, - { "label" => tdc_2.libelle, "table" => 'type_de_champ', "column" => tdc_2.id.to_s }, - { "label" => tdc_private_1.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_1.id.to_s }, - { "label" => tdc_private_2.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_2.id.to_s } + { "label" => tdc_1.libelle, "table" => 'type_de_champ', "column" => tdc_1.stable_id.to_s }, + { "label" => tdc_2.libelle, "table" => 'type_de_champ', "column" => tdc_2.stable_id.to_s }, + { "label" => tdc_private_1.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_1.stable_id.to_s }, + { "label" => tdc_private_2.libelle, "table" => 'type_de_champ_private', "column" => tdc_private_2.stable_id.to_s } ] } @@ -224,7 +224,7 @@ describe ProcedurePresentation do context 'for type_de_champ table' do let(:table) { 'type_de_champ' } - let(:column) { procedure.types_de_champ.first.id.to_s } + let(:column) { procedure.types_de_champ.first.stable_id.to_s } let(:dossier) { create(:dossier, procedure: procedure) } @@ -235,7 +235,7 @@ describe ProcedurePresentation do context 'for type_de_champ_private table' do let(:table) { 'type_de_champ_private' } - let(:column) { procedure.types_de_champ_private.first.id.to_s } + let(:column) { procedure.types_de_champ_private.first.stable_id.to_s } let(:dossier) { create(:dossier, procedure: procedure) } @@ -325,7 +325,7 @@ describe ProcedurePresentation do context 'for type_de_champ table' do let(:table) { 'type_de_champ' } - let(:column) { procedure.types_de_champ.first.id.to_s } + let(:column) { procedure.types_de_champ.first.stable_id.to_s } let(:order) { 'desc' } # Asc works the same, no extra test required let(:beurre_dossier) { create(:dossier, procedure: procedure) } @@ -341,7 +341,7 @@ describe ProcedurePresentation do context 'for type_de_champ_private table' do let(:table) { 'type_de_champ_private' } - let(:column) { procedure.types_de_champ_private.first.id.to_s } + let(:column) { procedure.types_de_champ_private.first.stable_id.to_s } let(:order) { 'asc' } # Desc works the same, no extra test required let(:biere_dossier) { create(:dossier, procedure: procedure) } @@ -496,7 +496,7 @@ describe ProcedurePresentation do end context 'for type_de_champ table' do - let(:filter) { [{ 'table' => 'type_de_champ', 'column' => type_de_champ.id.to_s, 'value' => 'keep' }] } + let(:filter) { [{ 'table' => 'type_de_champ', 'column' => type_de_champ.stable_id.to_s, 'value' => 'keep' }] } let(:kept_dossier) { create(:dossier, procedure: procedure) } let(:discarded_dossier) { create(:dossier, procedure: procedure) } @@ -514,8 +514,8 @@ describe ProcedurePresentation do context 'with multiple search values' do let(:filter) do [ - { 'table' => 'type_de_champ', 'column' => type_de_champ.id.to_s, 'value' => 'keep' }, - { 'table' => 'type_de_champ', 'column' => type_de_champ.id.to_s, 'value' => 'and' } + { 'table' => 'type_de_champ', 'column' => type_de_champ.stable_id.to_s, 'value' => 'keep' }, + { 'table' => 'type_de_champ', 'column' => type_de_champ.stable_id.to_s, 'value' => 'and' } ] end @@ -533,7 +533,7 @@ describe ProcedurePresentation do end context 'with yes_no type_de_champ' do - let(:filter) { [{ 'table' => 'type_de_champ', 'column' => type_de_champ.id.to_s, 'value' => 'true' }] } + let(:filter) { [{ 'table' => 'type_de_champ', 'column' => type_de_champ.stable_id.to_s, 'value' => 'true' }] } let(:procedure) { create(:procedure, :with_yes_no) } before do @@ -546,7 +546,7 @@ describe ProcedurePresentation do end context 'for type_de_champ_private table' do - let(:filter) { [{ 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.id.to_s, 'value' => 'keep' }] } + let(:filter) { [{ 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.stable_id.to_s, 'value' => 'keep' }] } let(:kept_dossier) { create(:dossier, procedure: procedure) } let(:discarded_dossier) { create(:dossier, procedure: procedure) } @@ -562,8 +562,8 @@ describe ProcedurePresentation do context 'with multiple search values' do let(:filter) do [ - { 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.id.to_s, 'value' => 'keep' }, - { 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.id.to_s, 'value' => 'and' } + { 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.stable_id.to_s, 'value' => 'keep' }, + { 'table' => 'type_de_champ_private', 'column' => type_de_champ_private.stable_id.to_s, 'value' => 'and' } ] end @@ -762,7 +762,7 @@ describe ProcedurePresentation do context 'for type de champ' do let(:table) { 'type_de_champ' } - let(:column) { procedure.types_de_champ.first.id.to_s } + let(:column) { procedure.types_de_champ.first.stable_id.to_s } it 'preloads the champs relation' do # Ideally, we would only preload the champs for the matching column @@ -779,7 +779,7 @@ describe ProcedurePresentation do context 'for type de champ private' do let(:table) { 'type_de_champ_private' } - let(:column) { procedure.types_de_champ_private.first.id.to_s } + let(:column) { procedure.types_de_champ_private.first.stable_id.to_s } it 'preloads the champs relation' do # Ideally, we would only preload the champs for the matching column From 024cc706677eddb0c5045ae23f6874abc4abe951 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 15:07:24 +0100 Subject: [PATCH 11/17] human_value use stable_id ? --- app/models/procedure_presentation.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 0a80c44d9..2ca25f739 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -192,8 +192,7 @@ class ProcedurePresentation < ApplicationRecord def human_value_for_filter(filter) case filter['table'] when 'type_de_champ', 'type_de_champ_private' - type_de_champ = TypeDeChamp.find_by(id: filter['column']) - type_de_champ.dynamic_type.filter_to_human(filter['value']) + find_type_de_champ(filter['column']).dynamic_type.filter_to_human(filter['value']) else filter['value'] end @@ -270,6 +269,10 @@ class ProcedurePresentation < ApplicationRecord fields.find { |field| field.values_at('table', 'column') == [table, column] } end + def find_type_de_champ(column) + TypeDeChamp.order(:revision_id).find_by(stable_id: column) + end + def check_allowed_displayed_fields displayed_fields.each do |field| check_allowed_field(:displayed_fields, field) From 116b3c818945f8e91beec676bcb52126f660b714 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 15:07:44 +0100 Subject: [PATCH 12/17] update link ? --- app/views/instructeurs/procedures/show.html.haml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index 412fadc0c..6f59875bf 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -98,7 +98,7 @@ - if i > 0 ou %span.filter - = link_to remove_filter_instructeur_procedure_path(@procedure, { statut: @statut, table: filter['table'], column: filter['column'], value: filter['value'] }) do + = link_to remove_filter_instructeur_procedure_path(@procedure, { statut: @statut, field: "#{filter['table']}/#{filter['column']}", value: filter['value'] }) do %img.close-icon{ src: image_url("close.svg") } = "#{filter['label'].truncate(50)} : #{@procedure_presentation.human_value_for_filter(filter)}" %table.table.dossiers-table.hoverable @@ -143,9 +143,7 @@ - @procedure_presentation.displayed_fields_values(dossier).each do |value| %td - / FIXME: value should automatically fallback to `""` instead of nil - / #get_value should call to_s on the champ - = link_to(value || "", instructeur_dossier_path(@procedure, dossier), class: 'cell-link') + = link_to(value, instructeur_dossier_path(@procedure, dossier), class: 'cell-link') %td.status-col = link_to(instructeur_dossier_path(@procedure, dossier), class: 'cell-link') do From 9848dc2295e01f09af42faf0f00ef1a89809d3f7 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Fri, 30 Oct 2020 15:07:58 +0100 Subject: [PATCH 13/17] update brakeman --- config/brakeman.ignore | 86 +++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/config/brakeman.ignore b/config/brakeman.ignore index e8efe1367..73af45bf8 100644 --- a/config/brakeman.ignore +++ b/config/brakeman.ignore @@ -1,5 +1,25 @@ { "ignored_warnings": [ + { + "warning_type": "SQL Injection", + "warning_code": 0, + "fingerprint": "25d6ed4f7f9120faf69596aa97d9e0558fd86817583b99b9b7879aff43ec2751", + "check_name": "SQL", + "message": "Possible SQL injection", + "file": "app/models/procedure_presentation.rb", + "line": 111, + "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", + "code": "dossiers.with_type_de_champ_private(column).order(\"champs.value #{order}\")", + "render_path": null, + "location": { + "type": "method", + "class": "ProcedurePresentation", + "method": "sorted_ids" + }, + "user_input": "order", + "confidence": "Weak", + "note": "`table`, `column` and `order` come from the model, which is validated to prevent injection attacks. Furthermore, `table` and `column` are escaped." + }, { "warning_type": "Cross-Site Scripting", "warning_code": 2, @@ -10,7 +30,19 @@ "line": 28, "link": "https://brakemanscanner.org/docs/warning_types/cross_site_scripting", "code": "current_user.dossiers.includes(:procedure).find(params[:id]).procedure.monavis_embed", - "render_path": [{"type":"controller","class":"Users::DossiersController","method":"merci","line":181,"file":"app/controllers/users/dossiers_controller.rb"}], + "render_path": [ + { + "type": "controller", + "class": "Users::DossiersController", + "method": "merci", + "line": 195, + "file": "app/controllers/users/dossiers_controller.rb", + "rendered": { + "name": "users/dossiers/merci", + "file": "app/views/users/dossiers/merci.html.haml" + } + } + ], "location": { "type": "template", "template": "users/dossiers/merci" @@ -26,7 +58,7 @@ "check_name": "Redirect", "message": "Possible unprotected redirect", "file": "app/controllers/instructeurs/procedures_controller.rb", - "line": 198, + "line": 176, "link": "https://brakemanscanner.org/docs/warning_types/redirect/", "code": "redirect_to(Export.find_or_create_export(params[:export_format], current_instructeur.groupe_instructeurs.where(:procedure => procedure)).file.service_url)", "render_path": null, @@ -59,6 +91,26 @@ "confidence": "Medium", "note": "The table and column are escaped, which should make this safe" }, + { + "warning_type": "SQL Injection", + "warning_code": 0, + "fingerprint": "d6031dd493ff36d62af2d75d0b1e4606c665413a62ef26a847902af4ad97d81f", + "check_name": "SQL", + "message": "Possible SQL injection", + "file": "app/models/procedure_presentation.rb", + "line": 106, + "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", + "code": "dossiers.with_type_de_champ(column).order(\"champs.value #{order}\")", + "render_path": null, + "location": { + "type": "method", + "class": "ProcedurePresentation", + "method": "sorted_ids" + }, + "user_input": "order", + "confidence": "Weak", + "note": "`table`, `column` and `order` come from the model, which is validated to prevent injection attacks. Furthermore, `table` and `column` are escaped." + }, { "warning_type": "SQL Injection", "warning_code": 0, @@ -66,7 +118,7 @@ "check_name": "SQL", "message": "Possible SQL injection", "file": "app/models/procedure_presentation.rb", - "line": 107, + "line": 123, "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", "code": "((\"self\" == \"self\") ? (dossiers) : (dossiers.includes(\"self\"))).order(\"#{self.class.sanitized_column(\"self\", column)} #{order}\")", "render_path": null, @@ -79,26 +131,6 @@ "confidence": "Weak", "note": "`table`, `column` and `order` come from the model, which is validated to prevent injection attacks. Furthermore, `table` and `column` are escaped." }, - { - "warning_type": "SQL Injection", - "warning_code": 0, - "fingerprint": "f85ed20c14a223884f624d744ff99070f6fc0697d918f54a08e7786ad70bb243", - "check_name": "SQL", - "message": "Possible SQL injection", - "file": "app/models/procedure_presentation.rb", - "line": 95, - "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", - "code": "dossiers.includes(((\"type_de_champ\" == \"type_de_champ\") ? (:champs) : (:champs_private))).where(\"champs.type_de_champ_id = #{column.to_i}\").order(\"champs.value #{order}\")", - "render_path": null, - "location": { - "type": "method", - "class": "ProcedurePresentation", - "method": "sorted_ids" - }, - "user_input": "order", - "confidence": "Weak", - "note": "`column` and `order` come from the model, which is validated to prevent injection attacks. Furthermore, the sql injection attack on `column` would need to survive the `to_i`" - }, { "warning_type": "SQL Injection", "warning_code": 0, @@ -106,7 +138,7 @@ "check_name": "SQL", "message": "Possible SQL injection", "file": "app/models/procedure_presentation.rb", - "line": 103, + "line": 119, "link": "https://brakemanscanner.org/docs/warning_types/sql_injection/", "code": "dossiers.includes(:followers_instructeurs).joins(\"LEFT OUTER JOIN users instructeurs_users ON instructeurs_users.instructeur_id = instructeurs.id\").order(\"instructeurs_users.email #{order}\")", "render_path": null, @@ -117,9 +149,9 @@ }, "user_input": "order", "confidence": "Weak", - "note": "" + "note": "`table`, `column` and `order` come from the model, which is validated to prevent injection attacks. Furthermore, `table` and `column` are escaped." } ], - "updated": "2019-12-12 16:36:32 +0100", - "brakeman_version": "4.3.1" + "updated": "2020-09-17 13:28:51 +0200", + "brakeman_version": "4.8.1" } From a9a4f6e2a801b19b127aae8eaec0d1f384b1a53a Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 1 Oct 2020 18:46:39 +0200 Subject: [PATCH 14/17] Add migration task to use stable_id in filters --- app/models/procedure_presentation.rb | 3 +- ...1931_migrate_filters_to_use_stable_id.rake | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 2ca25f739..297b2ffd4 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -291,7 +291,8 @@ class ProcedurePresentation < ApplicationRecord end def check_allowed_filter_columns - filters.each do |_, columns| + filters.each do |key, columns| + return true if key == 'migrated' columns.each do |column| check_allowed_field(:filters, column) end diff --git a/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake b/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake new file mode 100644 index 000000000..8494eabe1 --- /dev/null +++ b/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake @@ -0,0 +1,44 @@ +namespace :after_party do + desc 'Deployment task: migrate_filters_to_use_stable_id' + task migrate_filters_to_use_stable_id: :environment do + puts "Running deploy task 'migrate_filters_to_use_stable_id'" + + procedure_presentations = ProcedurePresentation.where("filters -> 'migrated' IS NULL") + progress = ProgressReport.new(procedure_presentations.count) + procedure_presentations.find_each do |procedure_presentation| + filters = procedure_presentation.filters + sort = procedure_presentation.sort + + ['tous', 'suivis', 'traites', 'a-suivre', 'archives'].each do |statut| + filters[statut] = filters[statut].map do |filter| + table, column, value = filter.values_at('table', 'column', 'value') + if table && (table == 'type_de_champ' || table == 'type_de_champ_private') + type_de_champ = TypeDeChamp.find_by(id: column) + if type_de_champ + column = type_de_champ.stable_id + end + end + [table, column, value] + end + end + + table, column = sort.values_at('table', 'column') + if table && (table == 'type_de_champ' || table == 'type_de_champ_private') + type_de_champ = TypeDeChamp.find_by(id: column) + if type_de_champ + sort['column'] = type_de_champ.stable_id + end + end + + filters['migrated'] = true + procedure_presentation.update_columns(filters: filters, sort: sort) + progress.inc + end + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end From 1348b883e3d191e4e51ef38f8e3033727c56089e Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Thu, 17 Dec 2020 10:54:12 +0100 Subject: [PATCH 15/17] Add cron to complete missing analysis --- app/jobs/cron/fix_missing_antivirus_analysis_job.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 app/jobs/cron/fix_missing_antivirus_analysis_job.rb diff --git a/app/jobs/cron/fix_missing_antivirus_analysis_job.rb b/app/jobs/cron/fix_missing_antivirus_analysis_job.rb new file mode 100644 index 000000000..a9d097cfd --- /dev/null +++ b/app/jobs/cron/fix_missing_antivirus_analysis_job.rb @@ -0,0 +1,12 @@ +class Cron::FixMissingAntivirusAnalysis < Cron::CronJob + self.schedule_expression = "every day at 2 am" + + def perform + ActiveStorage::Blob.where("metadata like '%\"virus_scan_result\":\"pending%'").each do |b| + begin + VirusScannerJob.perform_now(b) + rescue ActiveStorage::IntegrityError + end + end + end +end From d2bcbaa180e1ef623c1bf1d29f61f185901ed146 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 17 Dec 2020 12:19:42 +0100 Subject: [PATCH 16/17] Fix stable filters migration --- ...1931_migrate_filters_to_use_stable_id.rake | 22 ++++--- ...1_migrate_filters_to_use_stable_id_spec.rb | 60 +++++++++++++++++++ 2 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 spec/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id_spec.rb diff --git a/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake b/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake index 8494eabe1..d4df6b6e6 100644 --- a/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake +++ b/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id.rake @@ -8,30 +8,36 @@ namespace :after_party do procedure_presentations.find_each do |procedure_presentation| filters = procedure_presentation.filters sort = procedure_presentation.sort + displayed_fields = procedure_presentation.displayed_fields ['tous', 'suivis', 'traites', 'a-suivre', 'archives'].each do |statut| filters[statut] = filters[statut].map do |filter| - table, column, value = filter.values_at('table', 'column', 'value') + table, column = filter.values_at('table', 'column') if table && (table == 'type_de_champ' || table == 'type_de_champ_private') type_de_champ = TypeDeChamp.find_by(id: column) - if type_de_champ - column = type_de_champ.stable_id - end + filter['column'] = type_de_champ&.stable_id&.to_s end - [table, column, value] + filter end end table, column = sort.values_at('table', 'column') if table && (table == 'type_de_champ' || table == 'type_de_champ_private') type_de_champ = TypeDeChamp.find_by(id: column) - if type_de_champ - sort['column'] = type_de_champ.stable_id + sort['column'] = type_de_champ&.stable_id&.to_s + end + + displayed_fields = displayed_fields.map do |displayed_field| + table, column = displayed_field.values_at('table', 'column') + if table && (table == 'type_de_champ' || table == 'type_de_champ_private') + type_de_champ = TypeDeChamp.find_by(id: column) + displayed_field['column'] = type_de_champ&.stable_id&.to_s end + displayed_field end filters['migrated'] = true - procedure_presentation.update_columns(filters: filters, sort: sort) + procedure_presentation.update_columns(filters: filters, sort: sort, displayed_fields: displayed_fields) progress.inc end progress.finish diff --git a/spec/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id_spec.rb b/spec/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id_spec.rb new file mode 100644 index 000000000..02c51be49 --- /dev/null +++ b/spec/lib/tasks/deployment/20201001161931_migrate_filters_to_use_stable_id_spec.rb @@ -0,0 +1,60 @@ +describe '20201001161931_migrate_filters_to_use_stable_id' do + let(:rake_task) { Rake::Task['after_party:migrate_filters_to_use_stable_id'] } + + let(:procedure) { create(:procedure, :with_instructeur, :with_type_de_champ) } + let(:type_de_champ) { procedure.types_de_champ.first } + let(:sort) do + { + "table" => "type_de_champ", + "column" => type_de_champ.id.to_s, + "order" => "asc" + } + end + let(:filters) do + { + 'tous' => [ + { + "label" => "test", + "table" => "type_de_champ", + "column" => type_de_champ.id.to_s, + "value" => "test" + } + ], + 'suivis' => [], + 'traites' => [], + 'a-suivre' => [], + 'archives' => [] + } + end + let(:displayed_fields) do + [ + { + "label" => "test", + "table" => "type_de_champ", + "column" => type_de_champ.id.to_s + } + ] + end + let!(:procedure_presentation) do + type_de_champ.update_column(:stable_id, 13) + procedure_presentation = create(:procedure_presentation, procedure: procedure, assign_to: procedure.groupe_instructeurs.first.assign_tos.first) + procedure_presentation.update_columns(sort: sort, filters: filters, displayed_fields: displayed_fields) + procedure_presentation + end + + before do + rake_task.invoke + procedure_presentation.reload + end + + after { rake_task.reenable } + + context "should migrate procedure_presentation" do + it "columns are updated" do + expect(procedure_presentation.sort['column']).to eq(type_de_champ.stable_id.to_s) + expect(procedure_presentation.filters['tous'][0]['column']).to eq(type_de_champ.stable_id.to_s) + expect(procedure_presentation.displayed_fields[0]['column']).to eq(type_de_champ.stable_id.to_s) + expect(procedure_presentation.filters['migrated']).to eq(true) + end + end +end From 09128cb85e27aff5cc974c7d6360dde43d4f8710 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Thu, 24 Sep 2020 12:02:14 +0200 Subject: [PATCH 17/17] Add titre identite champ to GraphQL --- app/graphql/api/v2/schema.rb | 1 + app/graphql/schema.graphql | 27 +++++++++++++++++++ app/graphql/types/champ_type.rb | 2 ++ .../types/champs/titre_identite_champ_type.rb | 16 +++++++++++ .../titre_identite_type_de_champ.rb | 2 ++ 5 files changed, 48 insertions(+) create mode 100644 app/graphql/types/champs/titre_identite_champ_type.rb diff --git a/app/graphql/api/v2/schema.rb b/app/graphql/api/v2/schema.rb index fe527c03c..d734c00d7 100644 --- a/app/graphql/api/v2/schema.rb +++ b/app/graphql/api/v2/schema.rb @@ -50,6 +50,7 @@ class Api::V2::Schema < GraphQL::Schema Types::Champs::RepetitionChampType, Types::Champs::SiretChampType, Types::Champs::TextChampType, + Types::Champs::TitreIdentiteChampType, Types::GeoAreas::ParcelleCadastraleType, Types::GeoAreas::SelectionUtilisateurType, Types::PersonneMoraleType, diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index 81b5acf78..859d157ca 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -1197,6 +1197,33 @@ type TextChamp implements Champ { value: String } +type TitreIdentiteChamp implements Champ { + grantType: TitreIdentiteGrantType! + id: ID! + + """ + Libellé du champ. + """ + label: String! + + """ + La valeur du champ sous forme texte. + """ + stringValue: String +} + +enum TitreIdentiteGrantType { + """ + Françe Connect + """ + france_connect + + """ + Pièce justificative + """ + piece_justificative +} + enum TypeDeChamp { """ Adresse diff --git a/app/graphql/types/champ_type.rb b/app/graphql/types/champ_type.rb index 928e5f582..e0cee2ea6 100644 --- a/app/graphql/types/champ_type.rb +++ b/app/graphql/types/champ_type.rb @@ -33,6 +33,8 @@ module Types Types::Champs::LinkedDropDownListChampType when ::Champs::CiviliteChamp Types::Champs::CiviliteChampType + when ::Champs::TitreIdentiteChamp + Types::Champs::TitreIdentiteChampType else Types::Champs::TextChampType end diff --git a/app/graphql/types/champs/titre_identite_champ_type.rb b/app/graphql/types/champs/titre_identite_champ_type.rb new file mode 100644 index 000000000..df137607b --- /dev/null +++ b/app/graphql/types/champs/titre_identite_champ_type.rb @@ -0,0 +1,16 @@ +module Types::Champs + class TitreIdentiteChampType < Types::BaseObject + implements Types::ChampType + + class TitreIdentiteGrantTypeType < Types::BaseEnum + value(TypesDeChamp::TitreIdentiteTypeDeChamp::FRANCE_CONNECT, "Françe Connect") + value(TypesDeChamp::TitreIdentiteTypeDeChamp::PIECE_JUSTIFICATIVE, "Pièce justificative") + end + + field :grant_type, TitreIdentiteGrantTypeType, null: false + + def grant_type + TypesDeChamp::TitreIdentiteTypeDeChamp::PIECE_JUSTIFICATIVE + end + end +end diff --git a/app/models/types_de_champ/titre_identite_type_de_champ.rb b/app/models/types_de_champ/titre_identite_type_de_champ.rb index 7118fed08..febd8e065 100644 --- a/app/models/types_de_champ/titre_identite_type_de_champ.rb +++ b/app/models/types_de_champ/titre_identite_type_de_champ.rb @@ -1,2 +1,4 @@ class TypesDeChamp::TitreIdentiteTypeDeChamp < TypesDeChamp::TypeDeChampBase + FRANCE_CONNECT = 'france_connect' + PIECE_JUSTIFICATIVE = 'piece_justificative' end