From ae753dab4e8c290e5544e7399a050bab1ff2f7bc Mon Sep 17 00:00:00 2001 From: Yannick Le Duc Date: Sun, 21 Sep 2025 20:47:14 +0200 Subject: [PATCH] =?UTF-8?q?am=C3=A9liore=20l'import=20(normalisation=20col?= =?UTF-8?q?onnes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + src/components/ImportFileModal.tsx | 10 ++++-- src/lib/file-utils.ts | 49 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5ef6a52..a6a78c2 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,4 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts +test-74-yald.ods diff --git a/src/components/ImportFileModal.tsx b/src/components/ImportFileModal.tsx index 64f56e5..f7d0c7b 100644 --- a/src/components/ImportFileModal.tsx +++ b/src/components/ImportFileModal.tsx @@ -8,7 +8,7 @@ import { Alert, AlertDescription } from '@/components/ui/alert'; import { Upload, FileText, Download, AlertCircle } from 'lucide-react'; import { BaseModal } from './base/BaseModal'; import { ErrorDisplay } from './base/ErrorDisplay'; -import { parseCSV, parseExcel, getExpectedColumns, downloadTemplate, validateFileType } from '@/lib/file-utils'; +import { parseCSV, parseExcel, getExpectedColumns, downloadTemplate, validateFileType, normalizeParsedData } from '@/lib/file-utils'; interface ImportFileModalProps { isOpen: boolean; @@ -52,7 +52,9 @@ export default function ImportFileModal({ return; } - setPreview(result.data.slice(0, 5)); // Afficher les 5 premières lignes + // Normaliser les données pour correspondre aux colonnes attendues + const normalizedData = normalizeParsedData(result.data, type); + setPreview(normalizedData.slice(0, 5)); // Afficher les 5 premières lignes } }; @@ -69,7 +71,9 @@ export default function ImportFileModal({ return; } - onImport(result.data); + // Normaliser les données pour correspondre aux colonnes attendues + const normalizedData = normalizeParsedData(result.data, type); + onImport(normalizedData); onClose(); setFile(null); setPreview([]); diff --git a/src/lib/file-utils.ts b/src/lib/file-utils.ts index d964755..a02972b 100644 --- a/src/lib/file-utils.ts +++ b/src/lib/file-utils.ts @@ -132,6 +132,55 @@ export function getExpectedColumns(type: 'propositions' | 'participants'): strin } } +/** + * Normalise les noms de colonnes pour améliorer la compatibilité + */ +export function normalizeColumnName(columnName: string): string { + if (!columnName) return ''; + + const normalized = columnName.toLowerCase().trim(); + + // Mappings pour les colonnes communes + const mappings: { [key: string]: string } = { + 'email': 'Email', + 'e-mail': 'Email', + 'mail': 'Email', + 'courriel': 'Email', + 'prénom': 'Prénom', + 'prenom': 'Prénom', + 'firstname': 'Prénom', + 'first_name': 'Prénom', + 'nom': 'Nom', + 'lastname': 'Nom', + 'last_name': 'Nom', + 'titre': 'Titre', + 'title': 'Titre', + 'description': 'Description', + 'desc': 'Description' + }; + + return mappings[normalized] || columnName; +} + +/** + * Normalise les données parsées pour correspondre aux colonnes attendues + */ +export function normalizeParsedData(data: any[], type: 'propositions' | 'participants'): any[] { + const expectedColumns = getExpectedColumns(type); + + return data.map(row => { + const normalizedRow: any = {}; + + // Normaliser chaque colonne + Object.keys(row).forEach(key => { + const normalizedKey = normalizeColumnName(key); + normalizedRow[normalizedKey] = row[key]; + }); + + return normalizedRow; + }); +} + export async function downloadTemplate(type: 'propositions' | 'participants'): Promise { const columns = getExpectedColumns(type);