Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ETQ tech je souhaite que les champs RNA ne puissent pas contenir d'espace sans quoi nos appels d'API remontent un URI::InvalidURIError #10708

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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-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
12 changes: 12 additions & 0 deletions app/javascript/controllers/format_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, '')
Expand Down
19 changes: 19 additions & 0 deletions app/tasks/maintenance/normalize_rna_values_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Maintenance
class NormalizeRNAValuesTask < MaintenanceTasks::Task
def collection
Champs::RNAChamp.where.not(value: nil)

Check warning on line 6 in app/tasks/maintenance/normalize_rna_values_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/normalize_rna_values_task.rb#L6

Added line #L6 was not covered by tests
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
4 changes: 3 additions & 1 deletion app/tasks/maintenance/populate_rna_json_value_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def process(champ)
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
Expand Down
19 changes: 19 additions & 0 deletions spec/tasks/maintenance/normalize_rna_values_task_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading