allow draft to be saved with invalid cnaf champ
This commit is contained in:
parent
cd7bafaa0d
commit
87de9e38c6
4 changed files with 42 additions and 12 deletions
|
@ -382,7 +382,8 @@ module Users
|
||||||
if @dossier.champs.any?(&:changed_for_autosave?)
|
if @dossier.champs.any?(&:changed_for_autosave?)
|
||||||
@dossier.last_champ_updated_at = Time.zone.now
|
@dossier.last_champ_updated_at = Time.zone.now
|
||||||
end
|
end
|
||||||
if !@dossier.save
|
|
||||||
|
if !@dossier.save(**validation_options)
|
||||||
errors += @dossier.errors.full_messages
|
errors += @dossier.errors.full_messages
|
||||||
elsif change_groupe_instructeur?
|
elsif change_groupe_instructeur?
|
||||||
@dossier.assign_to_groupe_instructeur(groupe_instructeur_from_params)
|
@dossier.assign_to_groupe_instructeur(groupe_instructeur_from_params)
|
||||||
|
@ -453,5 +454,16 @@ module Users
|
||||||
def save_draft?
|
def save_draft?
|
||||||
dossier.brouillon? && !params[:submit_draft]
|
dossier.brouillon? && !params[:submit_draft]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def validation_options
|
||||||
|
if save_draft?
|
||||||
|
{ context: :brouillon }
|
||||||
|
else
|
||||||
|
# rubocop:disable Lint/BooleanSymbol
|
||||||
|
# Force ActiveRecord to re-validate associated records.
|
||||||
|
{ context: :false }
|
||||||
|
# rubocop:enable Lint/BooleanSymbol
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,8 +20,8 @@
|
||||||
#
|
#
|
||||||
class Champs::CnafChamp < Champs::TextChamp
|
class Champs::CnafChamp < Champs::TextChamp
|
||||||
# see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/cnaf-input-validation.middleware.ts
|
# see https://github.com/betagouv/api-particulier/blob/master/src/presentation/middlewares/cnaf-input-validation.middleware.ts
|
||||||
validates :numero_allocataire, format: { with: /\A\d{1,7}\z/ }, if: -> { code_postal.present? }
|
validates :numero_allocataire, format: { with: /\A\d{1,7}\z/ }, if: -> { code_postal.present? && validation_context != :brouillon }
|
||||||
validates :code_postal, format: { with: /\A\w{5}\z/ }, if: -> { numero_allocataire.present? }
|
validates :code_postal, format: { with: /\A\w{5}\z/ }, if: -> { numero_allocataire.present? && validation_context != :brouillon }
|
||||||
|
|
||||||
store_accessor :value_json, :numero_allocataire, :code_postal
|
store_accessor :value_json, :numero_allocataire, :code_postal
|
||||||
|
|
||||||
|
@ -34,12 +34,14 @@ class Champs::CnafChamp < Champs::TextChamp
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_external_data
|
def fetch_external_data
|
||||||
APIParticulier::CnafAdapter.new(
|
if valid?
|
||||||
procedure.api_particulier_token,
|
APIParticulier::CnafAdapter.new(
|
||||||
numero_allocataire,
|
procedure.api_particulier_token,
|
||||||
code_postal,
|
numero_allocataire,
|
||||||
procedure.api_particulier_sources
|
code_postal,
|
||||||
).to_params
|
procedure.api_particulier_sources
|
||||||
|
).to_params
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def external_id
|
def external_id
|
||||||
|
|
|
@ -125,14 +125,23 @@ feature 'fetch API Particulier Data', js: true do
|
||||||
click_button('Continuer')
|
click_button('Continuer')
|
||||||
|
|
||||||
fill_in 'Le numéro d’allocataire CAF', with: numero_allocataire
|
fill_in 'Le numéro d’allocataire CAF', with: numero_allocataire
|
||||||
|
fill_in 'Le code postal', with: 'wrong_code'
|
||||||
|
|
||||||
|
blur
|
||||||
|
expect(page).to have_css('span', text: 'Brouillon enregistré', visible: true)
|
||||||
|
|
||||||
|
dossier = Dossier.last
|
||||||
|
expect(dossier.champs.first.code_postal).to eq('wrong_code')
|
||||||
|
|
||||||
|
click_on 'Déposer le dossier'
|
||||||
|
expect(page).to have_content(/code postal doit posséder 5 caractères/)
|
||||||
|
|
||||||
fill_in 'Le code postal', with: code_postal
|
fill_in 'Le code postal', with: code_postal
|
||||||
|
|
||||||
VCR.use_cassette("api_particulier/success/composition_familiale") do
|
VCR.use_cassette("api_particulier/success/composition_familiale") do
|
||||||
perform_enqueued_jobs { click_on 'Déposer le dossier' }
|
perform_enqueued_jobs { click_on 'Déposer le dossier' }
|
||||||
end
|
end
|
||||||
|
|
||||||
dossier = Dossier.last
|
|
||||||
|
|
||||||
visit demande_dossier_path(dossier)
|
visit demande_dossier_path(dossier)
|
||||||
expect(page).to have_content(/Des données.*ont été reçues depuis la CAF/)
|
expect(page).to have_content(/Des données.*ont été reçues depuis la CAF/)
|
||||||
|
|
||||||
|
|
|
@ -38,8 +38,9 @@ describe Champs::CnafChamp, type: :model do
|
||||||
let(:numero_allocataire) { '1234567' }
|
let(:numero_allocataire) { '1234567' }
|
||||||
let(:code_postal) { '12345' }
|
let(:code_postal) { '12345' }
|
||||||
let(:champ) { described_class.new(dossier: create(:dossier), type_de_champ: create(:type_de_champ_cnaf)) }
|
let(:champ) { described_class.new(dossier: create(:dossier), type_de_champ: create(:type_de_champ_cnaf)) }
|
||||||
|
let(:validation_context) { :create }
|
||||||
|
|
||||||
subject { champ.valid? }
|
subject { champ.valid?(validation_context) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
champ.numero_allocataire = numero_allocataire
|
champ.numero_allocataire = numero_allocataire
|
||||||
|
@ -82,6 +83,12 @@ describe Champs::CnafChamp, type: :model do
|
||||||
is_expected.to be false
|
is_expected.to be false
|
||||||
expect(champ.errors.full_messages).to eq(["Numero allocataire doit être composé au maximum de 7 chiffres"])
|
expect(champ.errors.full_messages).to eq(["Numero allocataire doit être composé au maximum de 7 chiffres"])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'and the validation_context is :brouillon' do
|
||||||
|
let(:validation_context) { :brouillon }
|
||||||
|
|
||||||
|
it { is_expected.to be true }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when code_postal is invalid' do
|
context 'when code_postal is invalid' do
|
||||||
|
|
Loading…
Reference in a new issue