fix: Possibilité d'ajouter un 2ème guardian, même si son mail est

associé à un profil existant
This commit is contained in:
N3WT DE COMPET
2025-05-30 14:16:00 +02:00
parent 314c31fab1
commit 8cf22905e5
2 changed files with 28 additions and 4 deletions

View File

@ -5,10 +5,12 @@ import { useTranslations } from 'next-intl';
import { Trash2, Plus, Users } from 'lucide-react';
import SectionHeader from '@/components/SectionHeader';
import { useEstablishment } from '@/context/EstablishmentContext';
import logger from '@/utils/logger';
export default function ResponsableInputFields({
guardians,
setGuardians,
profiles,
errors,
setIsPageValid,
enable = true,
@ -70,8 +72,17 @@ export default function ResponsableInputFields({
// Synchroniser profile_data.email et profile_data.username avec associated_profile_email
if (field === 'associated_profile_email') {
updatedGuardian.profile_role_data.profile_data.email = value;
updatedGuardian.profile_role_data.profile_data.username = value;
const existingProfile = profiles?.find(
(profile) => profile.email === value
);
if (existingProfile) {
updatedGuardian.profile_role_data.profile = existingProfile.id;
delete updatedGuardian.profile_role_data.profile_data;
} else {
updatedGuardian.profile_role_data.profile_data.email = value;
updatedGuardian.profile_role_data.profile_data.username = value;
delete updatedGuardian.profile_role_data.profile;
}
}
return updatedGuardian;