From abb2fa159c1a9b2666e70d25d8de1ea964e99593 Mon Sep 17 00:00:00 2001 From: mfo Date: Wed, 21 Aug 2024 17:59:30 +0200 Subject: [PATCH 1/4] feat(Champ.rna): avoid spaces that breaks API calls --- .../rna_component/rna_component.html.haml | 2 +- app/javascript/controllers/format_controller.ts | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/components/editable_champ/rna_component/rna_component.html.haml b/app/components/editable_champ/rna_component/rna_component.html.haml index 4d7240518..03bcd30d5 100644 --- a/app/components/editable_champ/rna_component/rna_component.html.haml +++ b/app/components/editable_champ/rna_component/rna_component.html.haml @@ -1,4 +1,4 @@ -= @form.text_field(:value, input_opts( id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'turbo-input', turbo_input_load_on_connect_value: @champ.prefilled? && @champ.value.present? && @champ.data.blank?, turbo_input_url_value: update_path }, required: @champ.required?, pattern: "W[0-9]{9}", class: "width-33-desktop", maxlength: 10)) += @form.text_field(:value, input_opts( id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'turbo-input format', format: 'deleteSpace', turbo_input_load_on_connect_value: @champ.prefilled? && @champ.value.present? && @champ.data.blank?, turbo_input_url_value: update_path }, required: @champ.required?, pattern: "W[0-9]{9}", class: "width-33-desktop", maxlength: 10)) .rna-info{ id: dom_id(@champ, :rna_info) } = render 'shared/champs/rna/association', champ: @champ, error: nil diff --git a/app/javascript/controllers/format_controller.ts b/app/javascript/controllers/format_controller.ts index 9eacd690e..1e76d4219 100644 --- a/app/javascript/controllers/format_controller.ts +++ b/app/javascript/controllers/format_controller.ts @@ -4,6 +4,13 @@ export class FormatController extends ApplicationController { connect() { const format = this.element.getAttribute('data-format'); switch (format) { + case 'deleteSpace': + this.on('change', (event) => { + const target = event.target as HTMLInputElement; + const value = this.deleteSpace(target.value); + replaceValue(target, value); + }); + break; case 'list': this.on('change', (event) => { const target = event.target as HTMLInputElement; @@ -41,16 +48,21 @@ export class FormatController extends ApplicationController { break; } } + private deleteSpace(value: string) { + return value.replace(/\s*/g, ''); + } private formatList(value: string) { return value.replace(/;/g, ','); } + private formatSIRET(value: string) { return value .replace(/[^\d]/gi, '') .replace(/^\s*(\d{3})\s*(\d{3})\s*(\d{3})\s*(\d{5})\s*$/gi, '$1 $2 $3 $4') .trim(); } + private formatIBAN(value: string) { return value .replace(/[^\dA-Z]/gi, '') From 22e50d4c9e3680a1e0e656ab92ce16e8a6317442 Mon Sep 17 00:00:00 2001 From: mfo Date: Wed, 21 Aug 2024 18:00:01 +0200 Subject: [PATCH 2/4] fix(data): normalize invalid Champs::RNAChamp.values in db --- .../maintenance/normalize_rna_values_task.rb | 19 +++++++++++++++++++ .../normalize_rna_values_task_spec.rb | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 app/tasks/maintenance/normalize_rna_values_task.rb create mode 100644 spec/tasks/maintenance/normalize_rna_values_task_spec.rb diff --git a/app/tasks/maintenance/normalize_rna_values_task.rb b/app/tasks/maintenance/normalize_rna_values_task.rb new file mode 100644 index 000000000..3837d7bf7 --- /dev/null +++ b/app/tasks/maintenance/normalize_rna_values_task.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Maintenance + class NormalizeRNAValuesTask < MaintenanceTasks::Task + def collection + Champs::RNAChamp.where.not(value: nil) + end + + def process(element) + if /\s/.match?(element.value) + element.update_column(:value, element.value.gsub(/\s+/, '')) + end + end + + def count + # to costly + end + end +end diff --git a/spec/tasks/maintenance/normalize_rna_values_task_spec.rb b/spec/tasks/maintenance/normalize_rna_values_task_spec.rb new file mode 100644 index 000000000..bed039e3e --- /dev/null +++ b/spec/tasks/maintenance/normalize_rna_values_task_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require "rails_helper" + +module Maintenance + RSpec.describe NormalizeRNAValuesTask do + describe "#process" do + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :rna }]) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:element) { dossier.champs.first } + subject(:process) { described_class.process(element) } + let(:error_value) { "999 0 999" } + it "removes extra spaces" do + element.update_column(:value, error_value) + expect { subject }.to change { element.reload.value }.from(error_value).to("9990999") + end + end + end +end From 53cbb1cace3d71344ab3228512686b38b1fbc165 Mon Sep 17 00:00:00 2001 From: mfo Date: Wed, 21 Aug 2024 18:02:01 +0200 Subject: [PATCH 3/4] fix(PopulateRnaJsonValue): some procedure had been deleted, can not find them --- app/tasks/maintenance/populate_rna_json_value_task.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/tasks/maintenance/populate_rna_json_value_task.rb b/app/tasks/maintenance/populate_rna_json_value_task.rb index 71680f3fd..1998dea15 100644 --- a/app/tasks/maintenance/populate_rna_json_value_task.rb +++ b/app/tasks/maintenance/populate_rna_json_value_task.rb @@ -16,7 +16,9 @@ module Maintenance return if data.blank? champ.update(value_json: APIGeoService.parse_rna_address(data['adresse'])) rescue URI::InvalidURIError - # some data raise this error + # some Champs::RNAChamp contain spaces which raise this error + rescue ActiveRecord::RecordNotFound + # some Champs::RNAChamp procedure had been soft deleted end def count From 76f9373af55ecbc2e29b2a8ddf084c29644752dc Mon Sep 17 00:00:00 2001 From: mfo Date: Thu, 22 Aug 2024 09:50:53 +0200 Subject: [PATCH 4/4] fix(RNA.pattern): allows RNA with letters too, some territories can have letters too --- .../editable_champ/rna_component/rna_component.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/editable_champ/rna_component/rna_component.html.haml b/app/components/editable_champ/rna_component/rna_component.html.haml index 03bcd30d5..3a7672892 100644 --- a/app/components/editable_champ/rna_component/rna_component.html.haml +++ b/app/components/editable_champ/rna_component/rna_component.html.haml @@ -1,4 +1,4 @@ -= @form.text_field(:value, input_opts( id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'turbo-input format', format: 'deleteSpace', turbo_input_load_on_connect_value: @champ.prefilled? && @champ.value.present? && @champ.data.blank?, turbo_input_url_value: update_path }, required: @champ.required?, pattern: "W[0-9]{9}", class: "width-33-desktop", maxlength: 10)) += @form.text_field(:value, input_opts( id: @champ.input_id, aria: { describedby: @champ.describedby_id }, data: { controller: 'turbo-input format', format: 'deleteSpace', turbo_input_load_on_connect_value: @champ.prefilled? && @champ.value.present? && @champ.data.blank?, turbo_input_url_value: update_path }, required: @champ.required?, pattern: "W[0-9A-Z]{9}", class: "width-33-desktop", maxlength: 10)) .rna-info{ id: dom_id(@champ, :rna_info) } = render 'shared/champs/rna/association', champ: @champ, error: nil