From 2fa546e6b7354b02da2a17ccd75ad13143baa65e Mon Sep 17 00:00:00 2001 From: Julien Martin Date: Thu, 26 Feb 2026 21:02:40 +0100 Subject: [PATCH] fix(inscription AM): format NIR Corse + extraction message erreur API Made-with: Cursor --- frontend/lib/services/auth_service.dart | 16 ++++++++++++++-- frontend/lib/utils/nir_utils.dart | 10 +++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/frontend/lib/services/auth_service.dart b/frontend/lib/services/auth_service.dart index 7a27c64..f9b0e75 100644 --- a/frontend/lib/services/auth_service.dart +++ b/frontend/lib/services/auth_service.dart @@ -184,8 +184,20 @@ class AuthService { } final decoded = response.body.isNotEmpty ? jsonDecode(response.body) : null; - final message = decoded is Map ? (decoded['message'] as String? ?? decoded['error'] as String?) : null; - throw Exception(message ?? 'Erreur lors de l\'inscription (${response.statusCode})'); + final message = _extractErrorMessage(decoded, response.statusCode); + throw Exception(message); + } + + /// Extrait le message d'erreur des réponses NestJS (message string, array, ou objet). + static String _extractErrorMessage(dynamic decoded, int statusCode) { + const fallback = 'Erreur lors de l\'inscription'; + if (decoded == null || decoded is! Map) return '$fallback ($statusCode)'; + final msg = decoded['message']; + if (msg == null) return decoded['error'] as String? ?? '$fallback ($statusCode)'; + if (msg is String) return msg; + if (msg is List) return msg.map((e) => e.toString()).join('. ').trim(); + if (msg is Map && msg['message'] != null) return msg['message'].toString(); + return '$fallback ($statusCode)'; } /// Rafraîchit le profil utilisateur depuis l'API diff --git a/frontend/lib/utils/nir_utils.dart b/frontend/lib/utils/nir_utils.dart index ea8d072..cfed04b 100644 --- a/frontend/lib/utils/nir_utils.dart +++ b/frontend/lib/utils/nir_utils.dart @@ -49,15 +49,11 @@ String nirToRaw(String normalized) { return s; } -/// Formate pour affichage : 1 12 34 56 789 012-34 ou 1 12 34 2A 789 012-34 +/// Formate pour affichage : 1 12 34 56 789 012-34 ou 1 12 34 2A 789 012-34 (Corse). String formatNir(String raw) { final r = nirToRaw(raw); if (r.length < 15) return r; - final dept = r.substring(5, 7); - final isCorsica = dept == '2A' || dept == '2B'; - if (isCorsica) { - return '${r.substring(0, 5)} ${r.substring(5, 7)} ${r.substring(7, 10)} ${r.substring(10, 13)}-${r.substring(13, 15)}'; - } + // Même structure pour tous : sexe + année + mois + département + commune + ordre-clé. return '${r.substring(0, 1)} ${r.substring(1, 3)} ${r.substring(3, 5)} ${r.substring(5, 7)} ${r.substring(7, 10)} ${r.substring(10, 13)}-${r.substring(13, 15)}'; } @@ -67,7 +63,7 @@ bool _isFormatValid(String raw) { final dept = raw.substring(5, 7); final restDigits = raw.substring(0, 5) + (dept == '2A' ? '19' : dept == '2B' ? '18' : dept) + raw.substring(7, 15); if (!RegExp(r'^[12]\d{12}\d{2}$').hasMatch(restDigits)) return false; - return RegExp(r'^[12]\d{4}(?:\d{2}|2A|2B)\d{6}$').hasMatch(raw); + return RegExp(r'^[12]\d{4}(?:\d{2}|2A|2B)\d{8}$').hasMatch(raw); } /// Calcule la clé de contrôle (97 - (NIR13 mod 97)). Pour 2A→19, 2B→18.