From 9627ba43c0ef99e8fbb33e7808e51064ccb4c67f Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Thu, 8 Feb 2018 17:13:15 +0100 Subject: [PATCH] [Fix #1409] Identite can be updated --- .../new_user/dossiers_controller.rb | 28 +++++++++++++ app/models/dossier.rb | 4 +- app/models/individual.rb | 13 +++++++ config/locales/models/dossier/fr.yml | 1 + config/locales/models/individual/fr.yml | 2 + config/routes.rb | 1 + .../new_user/dossiers_controller_spec.rb | 39 +++++++++++++++++++ .../users/dossiers_controller_spec.rb | 2 + spec/factories/dossier.rb | 1 + 9 files changed, 90 insertions(+), 1 deletion(-) diff --git a/app/controllers/new_user/dossiers_controller.rb b/app/controllers/new_user/dossiers_controller.rb index 345ea666a..0cb5f24d0 100644 --- a/app/controllers/new_user/dossiers_controller.rb +++ b/app/controllers/new_user/dossiers_controller.rb @@ -11,6 +11,26 @@ module NewUser @user = current_user end + def update_identite + @dossier = dossier + + individual_updated = @dossier.individual.update(individual_params) + dossier_updated = @dossier.update(dossier_params) + + if individual_updated && dossier_updated + flash.notice = "Identité enregistrée" + + if @dossier.procedure.module_api_carto.use_api_carto + redirect_to users_dossier_carte_path(@dossier.id) + else + redirect_to identite_dossier_path(@dossier) # Simon should replace this with dossier_path when done + end + else + flash.now.alert = @dossier.errors.full_messages + render :identite + end + end + private def dossier @@ -23,5 +43,13 @@ module NewUser redirect_to root_path end end + + def individual_params + params.require(:individual).permit(:gender, :nom, :prenom, :birthdate) + end + + def dossier_params + params.require(:dossier).permit(:autorisation_donnees) + end end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 5fede6a93..e4f2be5ab 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -37,6 +37,8 @@ class Dossier < ActiveRecord::Base accepts_nested_attributes_for :champs accepts_nested_attributes_for :champs_private + validates :autorisation_donnees, acceptance: { message: 'doit être coché' }, allow_nil: false, on: :update + default_scope { where(hidden_at: nil) } scope :state_brouillon, -> { where(state: 'brouillon') } scope :state_not_brouillon, -> { where.not(state: 'brouillon') } @@ -177,7 +179,7 @@ class Dossier < ActiveRecord::Base etablissement.destroy entreprise.destroy - update_attributes(autorisation_donnees: false) + update_columns(autorisation_donnees: false) end def total_follow diff --git a/app/models/individual.rb b/app/models/individual.rb index c35af2e4d..f108933dd 100644 --- a/app/models/individual.rb +++ b/app/models/individual.rb @@ -2,5 +2,18 @@ class Individual < ActiveRecord::Base belongs_to :dossier validates_uniqueness_of :dossier_id + validates :gender, presence: true, allow_nil: false, on: :update + validates :nom, presence: true, allow_blank: false, allow_nil: false, on: :update + validates :prenom, presence: true, allow_blank: false, allow_nil: false, on: :update validates :birthdate, format: { with: /\A\d{4}\-\d{2}\-\d{2}\z/, message: "La date n'est pas au format AAAA-MM-JJ" }, allow_nil: true + + before_validation :set_iso_date, if: -> { birthdate_changed? } + + private + + def set_iso_date + if birthdate.present? + self.birthdate = Date.parse(birthdate).iso8601 + end + end end diff --git a/config/locales/models/dossier/fr.yml b/config/locales/models/dossier/fr.yml index a9b9452f4..b66d5eff4 100644 --- a/config/locales/models/dossier/fr.yml +++ b/config/locales/models/dossier/fr.yml @@ -14,6 +14,7 @@ fr: accepte: "Accepté" refuse: "Refusé" sans_suite: "Sans suite" + autorisation_donnees: Acceptation des CGU errors: models: diff --git a/config/locales/models/individual/fr.yml b/config/locales/models/individual/fr.yml index 186e27a65..fe5bda687 100644 --- a/config/locales/models/individual/fr.yml +++ b/config/locales/models/individual/fr.yml @@ -10,6 +10,8 @@ fr: models: individual: attributes: + gender: + blank: 'doit être rempli' nom: blank: 'doit être rempli' prenom: diff --git a/config/routes.rb b/config/routes.rb index 694215ddd..8a2155b5b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -204,6 +204,7 @@ Rails.application.routes.draw do resources :dossiers, only: [] do member do get 'identite' + patch 'update_identite' end get 'attestation' end diff --git a/spec/controllers/new_user/dossiers_controller_spec.rb b/spec/controllers/new_user/dossiers_controller_spec.rb index 358a893c2..52795cd5c 100644 --- a/spec/controllers/new_user/dossiers_controller_spec.rb +++ b/spec/controllers/new_user/dossiers_controller_spec.rb @@ -64,4 +64,43 @@ describe NewUser::DossiersController, type: :controller do end end end + + describe 'update_identite' do + let(:procedure) { create(:procedure, :for_individual) } + let(:dossier) { create(:dossier, user: user, procedure: procedure) } + + subject { post :update_identite, params: { id: dossier.id, individual: individual_params, dossier: dossier_params } } + + before do + sign_in(user) + subject + end + + context 'with correct individual and dossier params' do + let(:individual_params) { { gender: 'M', nom: 'Mouse', prenom: 'Mickey' } } + let(:dossier_params) { { autorisation_donnees: true } } + + it do + expect(response).to redirect_to(users_dossier_description_path(dossier)) + end + + context 'on a procedure with carto' do + let(:procedure) { create(:procedure, :for_individual, :with_api_carto) } + + it do + expect(response).to redirect_to(users_dossier_carte_path(dossier)) + end + end + end + + context 'with incorrect individual and dossier params' do + let(:individual_params) { { gender: '', nom: '', prenom: '' } } + let(:dossier_params) { { autorisation_donnees: nil } } + + it do + expect(response).not_to have_http_status(:redirect) + expect(flash[:alert]).to include("Civilité doit être rempli", "Nom doit être rempli", "Prénom doit être rempli", "Acceptation des CGU doit être coché") + end + end + end end diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 6635d49d0..0525b1ec7 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -365,6 +365,8 @@ describe Users::DossiersController, type: :controller do before do sign_in dossier.user + dossier.update_columns(autorisation_donnees: nil) + dossier.reload subject end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 4fa1978de..1fda4bbda 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory :dossier do + autorisation_donnees true state 'brouillon' association :user, factory: [:user]