From ae6d338304fa359c41c34777317f5c43d33a4d1a Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Mon, 29 Jul 2024 15:21:39 +0000 Subject: [PATCH 1/3] =?UTF-8?q?Ajout=20des=20cl=C3=A9s=20de=20traduction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/locales/models/individual/en.yml | 6 ++++++ config/locales/models/individual/fr.yml | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/config/locales/models/individual/en.yml b/config/locales/models/individual/en.yml index 46389b7e5d6..36edcc4b1e6 100644 --- a/config/locales/models/individual/en.yml +++ b/config/locales/models/individual/en.yml @@ -1,5 +1,11 @@ en: activerecord: + errors: + models: + individual: + attributes: + email: + must_be_different_from_mandataire: "must be different from the agent's email" attributes: individual: gender: Gender diff --git a/config/locales/models/individual/fr.yml b/config/locales/models/individual/fr.yml index cf0d2f376c9..217b2aa2c42 100644 --- a/config/locales/models/individual/fr.yml +++ b/config/locales/models/individual/fr.yml @@ -1,5 +1,11 @@ fr: activerecord: + errors: + models: + individual: + attributes: + email: + must_be_different_from_mandataire: "doit être différent de celui du mandataire" attributes: individual: gender: Civilité From 61525b6c2b62de1a510215e08eaaec0f76bd073e Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Mon, 29 Jul 2024 15:21:49 +0000 Subject: [PATCH 2/3] test --- spec/models/individual_spec.rb | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/spec/models/individual_spec.rb b/spec/models/individual_spec.rb index 6f8fb7c6f3a..8c693184a2d 100644 --- a/spec/models/individual_spec.rb +++ b/spec/models/individual_spec.rb @@ -44,13 +44,33 @@ end end - context 'when an individual has an invalid notification_method' do - let(:invalid_individual) { build(:individual, notification_method: 'invalid_method') } + describe 'validate_mandant_email' do + let(:user) { create(:user, email: 'mandataire@example.com') } + let(:dossier) { create(:dossier, :for_tiers_with_notification, user: user) } + let(:individual) { dossier.individual } - it 'raises an ArgumentError' do - expect { - invalid_individual.valid? - }.to raise_error(ArgumentError, "'invalid_method' is not a valid notification_method") + context 'when validating email' do + it 'is valid when email is different from the mandataire' do + individual.email = 'different@example.com' + expect(individual).to be_valid + end + + it 'is invalid when email is the same as the mandataire' do + individual.email = 'mandataire@example.com' + expect(individual).not_to be_valid + expect(individual.errors[:email]).to include( + I18n.t('activerecord.errors.models.individual.attributes.email.must_be_different_from_mandataire') + ) + end + + it 'is valid when email is not required (notification_method is not email)' do + dossier_without_notification = create(:dossier, :for_tiers_without_notification, user: user) + individual_without_notification = dossier_without_notification.individual + + expect(individual_without_notification).to be_valid + expect(individual_without_notification.email).to be_nil + expect(individual_without_notification.notification_method).to eq('no_notification') + end end end end From 88cd4a94dd55ddfb0c4c6afa9240068f89c7d9a7 Mon Sep 17 00:00:00 2001 From: Kara Diaby Date: Mon, 5 Aug 2024 12:06:37 +0000 Subject: [PATCH 3/3] =?UTF-8?q?Ajoute=20la=20validation=20sur=20le=20mod?= =?UTF-8?q?=C3=A8le=20Individual?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/individual.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/individual.rb b/app/models/individual.rb index fd940639177..664e586ad86 100644 --- a/app/models/individual.rb +++ b/app/models/individual.rb @@ -16,6 +16,7 @@ class Individual < ApplicationRecord on: :update validates :email, strict_email: true, presence: true, if: -> { dossier.for_tiers? && self.email? }, on: :update + validate :email_different_from_mandataire, on: :update after_commit -> { dossier.index_search_terms_later }, if: -> { nom_previously_changed? || prenom_previously_changed? } @@ -31,4 +32,10 @@ def self.from_france_connect(fc_information) end def unverified_email? = !email_verified_at? + + def email_different_from_mandataire + if email.present? && email.casecmp?(dossier.user.email) + errors.add(:email, :must_be_different_from_mandataire) + end + end end