feat: Version 3.6.2 - Correctifs tâches #17-20

- #17: Amélioration gestion des secteurs et statistiques
- #18: Optimisation services API et logs
- #19: Corrections Flutter widgets et repositories
- #20: Fix création passage - détection automatique ope_users.id vs users.id

Suppression dossier web/ (migration vers app Flutter)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-16 14:11:15 +01:00
parent 7b78037175
commit 232940b1eb
196 changed files with 8483 additions and 7966 deletions

View File

@@ -0,0 +1,594 @@
// Modèles pour les statistiques d'événements (connexions, passages, etc.)
//
// Ces modèles ne sont PAS stockés dans Hive car les données sont récupérées
// à la demande depuis l'API et ne nécessitent pas de persistance locale.
/// Statistiques d'authentification
class AuthStats {
final int success;
final int failed;
final int logout;
const AuthStats({
required this.success,
required this.failed,
required this.logout,
});
factory AuthStats.fromJson(Map<String, dynamic> json) {
return AuthStats(
success: _parseInt(json['success']),
failed: _parseInt(json['failed']),
logout: _parseInt(json['logout']),
);
}
int get total => success + failed + logout;
}
/// Statistiques de passages
class PassageStats {
final int created;
final int updated;
final int deleted;
final double amount;
const PassageStats({
required this.created,
required this.updated,
required this.deleted,
required this.amount,
});
factory PassageStats.fromJson(Map<String, dynamic> json) {
return PassageStats(
created: _parseInt(json['created']),
updated: _parseInt(json['updated']),
deleted: _parseInt(json['deleted']),
amount: _parseDouble(json['amount']),
);
}
int get total => created + updated + deleted;
}
/// Statistiques utilisateurs
class UserStats {
final int created;
final int updated;
final int deleted;
const UserStats({
required this.created,
required this.updated,
required this.deleted,
});
factory UserStats.fromJson(Map<String, dynamic> json) {
return UserStats(
created: _parseInt(json['created']),
updated: _parseInt(json['updated']),
deleted: _parseInt(json['deleted']),
);
}
int get total => created + updated + deleted;
}
/// Statistiques secteurs
class SectorStats {
final int created;
final int updated;
final int deleted;
const SectorStats({
required this.created,
required this.updated,
required this.deleted,
});
factory SectorStats.fromJson(Map<String, dynamic> json) {
return SectorStats(
created: _parseInt(json['created']),
updated: _parseInt(json['updated']),
deleted: _parseInt(json['deleted']),
);
}
int get total => created + updated + deleted;
}
/// Statistiques Stripe
class StripeStats {
final int created;
final int success;
final int failed;
final int cancelled;
final double amount;
const StripeStats({
required this.created,
required this.success,
required this.failed,
required this.cancelled,
required this.amount,
});
factory StripeStats.fromJson(Map<String, dynamic> json) {
return StripeStats(
created: _parseInt(json['created']),
success: _parseInt(json['success']),
failed: _parseInt(json['failed']),
cancelled: _parseInt(json['cancelled']),
amount: _parseDouble(json['amount']),
);
}
int get total => created + success + failed + cancelled;
}
/// Statistiques globales d'une journée
class DayStats {
final AuthStats auth;
final PassageStats passages;
final UserStats users;
final SectorStats sectors;
final StripeStats stripe;
const DayStats({
required this.auth,
required this.passages,
required this.users,
required this.sectors,
required this.stripe,
});
factory DayStats.fromJson(Map<String, dynamic> json) {
return DayStats(
auth: AuthStats.fromJson(json['auth'] ?? {}),
passages: PassageStats.fromJson(json['passages'] ?? {}),
users: UserStats.fromJson(json['users'] ?? {}),
sectors: SectorStats.fromJson(json['sectors'] ?? {}),
stripe: StripeStats.fromJson(json['stripe'] ?? {}),
);
}
}
/// Totaux d'une journée
class DayTotals {
final int events;
final int uniqueUsers;
const DayTotals({
required this.events,
required this.uniqueUsers,
});
factory DayTotals.fromJson(Map<String, dynamic> json) {
return DayTotals(
events: _parseInt(json['events']),
uniqueUsers: _parseInt(json['unique_users']),
);
}
}
/// Résumé complet d'une journée (réponse de /stats/summary)
class EventSummary {
final DateTime date;
final DayStats stats;
final DayTotals totals;
const EventSummary({
required this.date,
required this.stats,
required this.totals,
});
factory EventSummary.fromJson(Map<String, dynamic> json) {
return EventSummary(
date: DateTime.parse(json['date']),
stats: DayStats.fromJson(json['stats'] ?? {}),
totals: DayTotals.fromJson(json['totals'] ?? {}),
);
}
}
/// Statistiques d'un type d'événement pour une période
class EventTypeStats {
final int count;
final double sumAmount;
final int uniqueUsers;
const EventTypeStats({
required this.count,
required this.sumAmount,
required this.uniqueUsers,
});
factory EventTypeStats.fromJson(Map<String, dynamic> json) {
return EventTypeStats(
count: _parseInt(json['count']),
sumAmount: _parseDouble(json['sum_amount']),
uniqueUsers: _parseInt(json['unique_users']),
);
}
}
/// Données d'une journée dans les stats quotidiennes
class DailyStatsEntry {
final DateTime date;
final Map<String, EventTypeStats> events;
final int totalCount;
final double totalAmount;
const DailyStatsEntry({
required this.date,
required this.events,
required this.totalCount,
required this.totalAmount,
});
factory DailyStatsEntry.fromJson(Map<String, dynamic> json) {
final eventsJson = json['events'] as Map<String, dynamic>? ?? {};
final events = <String, EventTypeStats>{};
for (final entry in eventsJson.entries) {
events[entry.key] = EventTypeStats.fromJson(entry.value);
}
final totals = json['totals'] as Map<String, dynamic>? ?? {};
return DailyStatsEntry(
date: DateTime.parse(json['date']),
events: events,
totalCount: _parseInt(totals['count']),
totalAmount: _parseDouble(totals['sum_amount']),
);
}
}
/// Réponse des stats quotidiennes (/stats/daily)
class DailyStats {
final DateTime from;
final DateTime to;
final List<DailyStatsEntry> days;
const DailyStats({
required this.from,
required this.to,
required this.days,
});
factory DailyStats.fromJson(Map<String, dynamic> json) {
final daysJson = json['days'] as List<dynamic>? ?? [];
return DailyStats(
from: DateTime.parse(json['from']),
to: DateTime.parse(json['to']),
days: daysJson.map((d) => DailyStatsEntry.fromJson(d)).toList(),
);
}
}
/// Données d'une semaine dans les stats hebdomadaires
class WeeklyStatsEntry {
final DateTime weekStart;
final int weekNumber;
final int year;
final Map<String, EventTypeStats> events;
final int totalCount;
final double totalAmount;
const WeeklyStatsEntry({
required this.weekStart,
required this.weekNumber,
required this.year,
required this.events,
required this.totalCount,
required this.totalAmount,
});
factory WeeklyStatsEntry.fromJson(Map<String, dynamic> json) {
final eventsJson = json['events'] as Map<String, dynamic>? ?? {};
final events = <String, EventTypeStats>{};
for (final entry in eventsJson.entries) {
events[entry.key] = EventTypeStats.fromJson(entry.value);
}
final totals = json['totals'] as Map<String, dynamic>? ?? {};
return WeeklyStatsEntry(
weekStart: DateTime.parse(json['week_start']),
weekNumber: _parseInt(json['week_number']),
year: _parseInt(json['year']),
events: events,
totalCount: _parseInt(totals['count']),
totalAmount: _parseDouble(totals['sum_amount']),
);
}
}
/// Réponse des stats hebdomadaires (/stats/weekly)
class WeeklyStats {
final DateTime from;
final DateTime to;
final List<WeeklyStatsEntry> weeks;
const WeeklyStats({
required this.from,
required this.to,
required this.weeks,
});
factory WeeklyStats.fromJson(Map<String, dynamic> json) {
final weeksJson = json['weeks'] as List<dynamic>? ?? [];
return WeeklyStats(
from: DateTime.parse(json['from']),
to: DateTime.parse(json['to']),
weeks: weeksJson.map((w) => WeeklyStatsEntry.fromJson(w)).toList(),
);
}
}
/// Données d'un mois dans les stats mensuelles
class MonthlyStatsEntry {
final String month; // Format: "2025-01"
final int year;
final int monthNumber;
final Map<String, EventTypeStats> events;
final int totalCount;
final double totalAmount;
const MonthlyStatsEntry({
required this.month,
required this.year,
required this.monthNumber,
required this.events,
required this.totalCount,
required this.totalAmount,
});
factory MonthlyStatsEntry.fromJson(Map<String, dynamic> json) {
final eventsJson = json['events'] as Map<String, dynamic>? ?? {};
final events = <String, EventTypeStats>{};
for (final entry in eventsJson.entries) {
events[entry.key] = EventTypeStats.fromJson(entry.value);
}
final totals = json['totals'] as Map<String, dynamic>? ?? {};
return MonthlyStatsEntry(
month: json['month'] ?? '',
year: _parseInt(json['year']),
monthNumber: _parseInt(json['month_number']),
events: events,
totalCount: _parseInt(totals['count']),
totalAmount: _parseDouble(totals['sum_amount']),
);
}
}
/// Réponse des stats mensuelles (/stats/monthly)
class MonthlyStats {
final int year;
final List<MonthlyStatsEntry> months;
const MonthlyStats({
required this.year,
required this.months,
});
factory MonthlyStats.fromJson(Map<String, dynamic> json) {
final monthsJson = json['months'] as List<dynamic>? ?? [];
return MonthlyStats(
year: _parseInt(json['year']),
months: monthsJson.map((m) => MonthlyStatsEntry.fromJson(m)).toList(),
);
}
}
/// Détail d'un événement individuel
class EventDetail {
final DateTime timestamp;
final String event;
final String? username;
final String? reason;
final int? attempt;
final String? ip;
final String? platform;
final String? appVersion;
final Map<String, dynamic>? extra;
const EventDetail({
required this.timestamp,
required this.event,
this.username,
this.reason,
this.attempt,
this.ip,
this.platform,
this.appVersion,
this.extra,
});
factory EventDetail.fromJson(Map<String, dynamic> json) {
return EventDetail(
timestamp: DateTime.parse(json['timestamp']),
event: json['event'] ?? '',
username: json['username'],
reason: json['reason'],
attempt: json['attempt'] != null ? _parseInt(json['attempt']) : null,
ip: json['ip'],
platform: json['platform'],
appVersion: json['app_version'],
extra: json,
);
}
}
/// Pagination pour les détails
class EventPagination {
final int total;
final int limit;
final int offset;
final bool hasMore;
const EventPagination({
required this.total,
required this.limit,
required this.offset,
required this.hasMore,
});
factory EventPagination.fromJson(Map<String, dynamic> json) {
return EventPagination(
total: _parseInt(json['total']),
limit: _parseInt(json['limit']),
offset: _parseInt(json['offset']),
hasMore: json['has_more'] == true,
);
}
}
/// Réponse des détails d'événements (/stats/details)
class EventDetails {
final DateTime date;
final List<EventDetail> events;
final EventPagination pagination;
const EventDetails({
required this.date,
required this.events,
required this.pagination,
});
factory EventDetails.fromJson(Map<String, dynamic> json) {
final eventsJson = json['events'] as List<dynamic>? ?? [];
return EventDetails(
date: DateTime.parse(json['date']),
events: eventsJson.map((e) => EventDetail.fromJson(e)).toList(),
pagination: EventPagination.fromJson(json['pagination'] ?? {}),
);
}
}
/// Types d'événements disponibles
class EventTypes {
final List<String> auth;
final List<String> passages;
final List<String> sectors;
final List<String> users;
final List<String> entities;
final List<String> operations;
final List<String> stripe;
const EventTypes({
required this.auth,
required this.passages,
required this.sectors,
required this.users,
required this.entities,
required this.operations,
required this.stripe,
});
factory EventTypes.fromJson(Map<String, dynamic> json) {
return EventTypes(
auth: _parseStringList(json['auth']),
passages: _parseStringList(json['passages']),
sectors: _parseStringList(json['sectors']),
users: _parseStringList(json['users']),
entities: _parseStringList(json['entities']),
operations: _parseStringList(json['operations']),
stripe: _parseStringList(json['stripe']),
);
}
/// Obtient tous les types d'événements à plat
List<String> get allTypes => [
...auth,
...passages,
...sectors,
...users,
...entities,
...operations,
...stripe,
];
/// Obtient le libellé français d'un type d'événement
static String getLabel(String eventType) {
switch (eventType) {
// Auth
case 'login_success': return 'Connexion réussie';
case 'login_failed': return 'Connexion échouée';
case 'logout': return 'Déconnexion';
// Passages
case 'passage_created': return 'Passage créé';
case 'passage_updated': return 'Passage modifié';
case 'passage_deleted': return 'Passage supprimé';
// Sectors
case 'sector_created': return 'Secteur créé';
case 'sector_updated': return 'Secteur modifié';
case 'sector_deleted': return 'Secteur supprimé';
// Users
case 'user_created': return 'Utilisateur créé';
case 'user_updated': return 'Utilisateur modifié';
case 'user_deleted': return 'Utilisateur supprimé';
// Entities
case 'entity_created': return 'Entité créée';
case 'entity_updated': return 'Entité modifiée';
case 'entity_deleted': return 'Entité supprimée';
// Operations
case 'operation_created': return 'Opération créée';
case 'operation_updated': return 'Opération modifiée';
case 'operation_deleted': return 'Opération supprimée';
// Stripe
case 'stripe_payment_created': return 'Paiement créé';
case 'stripe_payment_success': return 'Paiement réussi';
case 'stripe_payment_failed': return 'Paiement échoué';
case 'stripe_payment_cancelled': return 'Paiement annulé';
case 'stripe_terminal_error': return 'Erreur terminal';
default: return eventType;
}
}
/// Obtient la catégorie d'un type d'événement
static String getCategory(String eventType) {
if (eventType.startsWith('login') || eventType == 'logout') return 'auth';
if (eventType.startsWith('passage')) return 'passages';
if (eventType.startsWith('sector')) return 'sectors';
if (eventType.startsWith('user')) return 'users';
if (eventType.startsWith('entity')) return 'entities';
if (eventType.startsWith('operation')) return 'operations';
if (eventType.startsWith('stripe')) return 'stripe';
return 'other';
}
}
// Helpers pour parser les types depuis JSON (gère int/string)
int _parseInt(dynamic value) {
if (value == null) return 0;
if (value is int) return value;
if (value is String) return int.tryParse(value) ?? 0;
if (value is double) return value.toInt();
return 0;
}
double _parseDouble(dynamic value) {
if (value == null) return 0.0;
if (value is double) return value;
if (value is int) return value.toDouble();
if (value is String) return double.tryParse(value) ?? 0.0;
return 0.0;
}
List<String> _parseStringList(dynamic value) {
if (value == null) return [];
if (value is List) return value.map((e) => e.toString()).toList();
return [];
}

View File

@@ -247,10 +247,10 @@ class OperationRepository extends ChangeNotifier {
debugPrint('✅ Opérations traitées');
}
// Traiter les secteurs (groupe secteurs) via DataLoadingService
if (responseData['secteurs'] != null) {
// Traiter les secteurs (groupe sectors) via DataLoadingService
if (responseData['sectors'] != null) {
await DataLoadingService.instance
.processSectorsFromApi(responseData['secteurs']);
.processSectorsFromApi(responseData['sectors']);
debugPrint('✅ Secteurs traités');
}
@@ -521,10 +521,10 @@ class OperationRepository extends ChangeNotifier {
debugPrint('✅ Opérations traitées');
}
// Traiter les secteurs (groupe secteurs) via DataLoadingService
if (responseData['secteurs'] != null) {
// Traiter les secteurs (groupe sectors) via DataLoadingService
if (responseData['sectors'] != null) {
await DataLoadingService.instance
.processSectorsFromApi(responseData['secteurs']);
.processSectorsFromApi(responseData['sectors']);
debugPrint('✅ Secteurs traités');
}

View File

@@ -246,8 +246,9 @@ class PassageRepository extends ChangeNotifier {
// Mode online : traitement normal
if (response.statusCode == 201 || response.statusCode == 200) {
// Récupérer l'ID du nouveau passage depuis la réponse
final passageId = response.data['id'] is String ? int.parse(response.data['id']) : response.data['id'] as int;
// Récupérer l'ID du nouveau passage depuis la réponse (passage_id ou id)
final rawId = response.data['passage_id'] ?? response.data['id'];
final passageId = rawId is String ? int.parse(rawId) : rawId as int;
// Créer le passage localement avec l'ID retourné par l'API
final newPassage = passage.copyWith(
@@ -431,10 +432,10 @@ class PassageRepository extends ChangeNotifier {
return true;
}
return false;
throw Exception('Mise à jour refusée par le serveur');
} catch (e) {
debugPrint('Erreur lors de la mise à jour du passage: $e');
return false;
rethrow; // Propager l'exception originale avec son message
} finally {
_isLoading = false;
notifyListeners();

View File

@@ -2,6 +2,9 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:path_provider/path_provider.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:geosector_app/core/data/models/user_model.dart';
@@ -65,16 +68,44 @@ class ApiService {
headers['X-App-Identifier'] = _appIdentifier;
_dio.options.headers.addAll(headers);
// Gestionnaire de cookies pour les sessions PHP
// Utilise CookieJar en mémoire (cookies maintenus pendant la durée de vie de l'app)
final cookieJar = CookieJar();
_dio.interceptors.add(CookieManager(cookieJar));
debugPrint('🍪 [API] Gestionnaire de cookies activé');
_dio.interceptors.add(InterceptorsWrapper(
onRequest: (options, handler) {
debugPrint('🌐 [API] Requête: ${options.method} ${options.path}');
debugPrint('🔑 [API] _sessionId présent: ${_sessionId != null}');
debugPrint('🔑 [API] Headers: ${options.headers}');
if (_sessionId != null) {
debugPrint('🔑 [API] Token: Bearer ${_sessionId!.substring(0, 20)}...');
options.headers[AppKeys.sessionHeader] = 'Bearer $_sessionId';
} else {
debugPrint('⚠️ [API] PAS DE TOKEN - Requête non authentifiée');
}
handler.next(options);
},
onError: (DioException error, handler) {
if (error.response?.statusCode == 401) {
_sessionId = null;
final path = error.requestOptions.path;
debugPrint('❌ [API] Erreur 401 sur: $path');
// Ne pas reset le token pour les requêtes non critiques
final nonCriticalPaths = [
'/users/device-info',
'/chat/rooms',
];
final isNonCritical = nonCriticalPaths.any((p) => path.contains(p));
if (isNonCritical) {
debugPrint('⚠️ [API] Requête non critique - Token conservé');
} else {
debugPrint('❌ [API] Requête critique - Token invalidé');
_sessionId = null;
}
}
handler.next(error);
},
@@ -1066,15 +1097,21 @@ class ApiService {
if (data.containsKey('session_id')) {
final sessionId = data['session_id'];
if (sessionId != null) {
debugPrint('🔐 [LOGIN] Token reçu du serveur: $sessionId');
debugPrint('🔐 [LOGIN] Longueur token: ${sessionId.toString().length}');
setSessionId(sessionId);
debugPrint('🔐 [LOGIN] Token stocké dans _sessionId');
// Collecter et envoyer les informations du device après login réussi
debugPrint('📱 Collecte des informations device après login...');
DeviceInfoService.instance.collectAndSendDeviceInfo().then((_) {
debugPrint('✅ Informations device collectées et envoyées');
}).catchError((error) {
debugPrint('⚠️ Erreur lors de l\'envoi des infos device: $error');
// Ne pas bloquer le login si l'envoi des infos device échoue
// Délai de 1 seconde pour laisser la session PHP se stabiliser
debugPrint('📱 Collecte des informations device après login (délai 1s)...');
Future.delayed(const Duration(seconds: 1), () {
DeviceInfoService.instance.collectAndSendDeviceInfo().then((_) {
debugPrint('✅ Informations device collectées et envoyées');
}).catchError((error) {
debugPrint('⚠️ Erreur lors de l\'envoi des infos device: $error');
// Ne pas bloquer le login si l'envoi des infos device échoue
});
});
}
}

View File

@@ -1,6 +1,6 @@
// ⚠️ AUTO-GENERATED FILE - DO NOT EDIT MANUALLY
// This file is automatically generated by deploy-app.sh script
// Last update: 2025-11-09 12:39:26
// Last update: 2026-01-16 13:37:45
// Source: ../VERSION file
//
// GEOSECTOR App Version Service
@@ -8,10 +8,10 @@
class AppInfoService {
// Version number (format: x.x.x)
static const String version = '3.5.2';
static const String version = '3.6.2';
// Build number (version without dots: xxx)
static const String buildNumber = '352';
static const String buildNumber = '362';
// Full version string (format: vx.x.x+xxx)
static String get fullVersion => 'v$version+$buildNumber';

View File

@@ -1,4 +1,5 @@
import 'dart:io';
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:battery_plus/battery_plus.dart';
@@ -211,18 +212,18 @@ class DeviceInfoService {
}
bool _checkIosTapToPaySupport(String machine, String systemVersion) {
// iPhone XS et plus récents (liste des identifiants)
final supportedDevices = [
'iPhone11,', // XS, XS Max
'iPhone12,', // 11, 11 Pro, 11 Pro Max
'iPhone13,', // 12 series
'iPhone14,', // 13 series
'iPhone15,', // 14 series
'iPhone16,', // 15 series
];
// Vérifier le modèle : iPhone11,x ou supérieur (iPhone XS+)
// Extraire le numéro majeur de l'identifiant (ex: "iPhone17,3" -> 17)
bool deviceSupported = false;
// Vérifier le modèle
bool deviceSupported = supportedDevices.any((prefix) => machine.startsWith(prefix));
if (machine.startsWith('iPhone')) {
final match = RegExp(r'iPhone(\d+),').firstMatch(machine);
if (match != null) {
final majorVersion = int.tryParse(match.group(1) ?? '0') ?? 0;
// iPhone XS = iPhone11,x donc >= 11 pour supporter Tap to Pay
deviceSupported = majorVersion >= 11;
}
}
// Vérifier la version iOS (16.4+ selon la documentation officielle Stripe)
final versionParts = systemVersion.split('.');
@@ -334,10 +335,10 @@ class DeviceInfoService {
return deviceInfo;
}
/// Vérifie la certification Stripe Tap to Pay via l'API
/// Vérifie la certification Stripe Tap to Pay via le SDK Stripe Terminal
Future<bool> checkStripeCertification() async {
try {
// Sur Web, toujours non certifié
// Sur Web, toujours non supporté
if (kIsWeb) {
debugPrint('📱 Web platform - Tap to Pay non supporté');
return false;
@@ -354,33 +355,35 @@ class DeviceInfoService {
return isSupported;
}
// Android : vérification via l'API Stripe
// Android : vérification des pré-requis hardware de base
// Note: Le vrai check de compatibilité avec découverte de readers se fera
// dans StripeTapToPayService lors du premier paiement
if (Platform.isAndroid) {
final androidInfo = await _deviceInfo.androidInfo;
debugPrint('📱 Vérification Tap to Pay pour ${androidInfo.manufacturer} ${androidInfo.model}');
debugPrint('📱 Vérification certification Stripe pour ${androidInfo.manufacturer} ${androidInfo.model}');
try {
final response = await ApiService.instance.post(
'/stripe/devices/check-tap-to-pay',
data: {
'platform': 'android',
'manufacturer': androidInfo.manufacturer,
'model': androidInfo.model,
},
);
final tapToPaySupported = response.data['tap_to_pay_supported'] == true;
final message = response.data['message'] ?? '';
debugPrint('📱 Résultat certification Stripe: $tapToPaySupported - $message');
return tapToPaySupported;
} catch (e) {
debugPrint('❌ Erreur lors de la vérification Stripe: $e');
// En cas d'erreur API, on se base sur la vérification locale
return androidInfo.version.sdkInt >= 28;
// Vérifications préalables de base
if (androidInfo.version.sdkInt < 28) {
debugPrint('❌ Android SDK trop ancien: ${androidInfo.version.sdkInt} (minimum 28)');
return false;
}
// Vérifier la disponibilité du NFC
try {
final nfcAvailable = await NfcManager.instance.isAvailable();
if (!nfcAvailable) {
debugPrint('❌ NFC non disponible sur cet appareil');
return false;
}
debugPrint('✅ NFC disponible');
} catch (e) {
debugPrint('⚠️ Impossible de vérifier NFC: $e');
// On continue quand même, ce n'est pas bloquant à ce stade
}
// Pré-requis de base OK
debugPrint('✅ Pré-requis Android OK (SDK ${androidInfo.version.sdkInt}, NFC disponible)');
return true;
}
return false;
@@ -390,22 +393,89 @@ class DeviceInfoService {
}
}
/// Vérifie si le device peut utiliser Tap to Pay
bool canUseTapToPay() {
final deviceInfo = getStoredDeviceInfo();
// Vérifications requises
final nfcCapable = deviceInfo['device_nfc_capable'] == true;
// Utiliser la certification Stripe si disponible, sinon l'ancienne vérification
// checkStripeCertification() fait déjà toutes les vérifications (modèle, iOS version, NFC Android)
final stripeCertified = deviceInfo['device_stripe_certified'] ?? deviceInfo['device_supports_tap_to_pay'];
final batteryLevel = deviceInfo['battery_level'] as int?;
// Batterie minimum 10% pour les paiements
final sufficientBattery = batteryLevel != null && batteryLevel >= 10;
return nfcCapable && stripeCertified == true && sufficientBattery;
return stripeCertified == true && sufficientBattery;
}
/// Stream pour surveiller les changements de batterie
Stream<BatteryState> get batteryStateStream => _battery.onBatteryStateChanged;
/// Retourne la raison pour laquelle Tap to Pay n'est pas disponible
/// Retourne null si Tap to Pay est disponible
String? getTapToPayUnavailableReason() {
// Sur Web, Tap to Pay n'est jamais disponible
if (kIsWeb) {
return 'Tap to Pay non disponible sur Web';
}
final deviceInfo = getStoredDeviceInfo();
// Vérifier la batterie
final batteryLevel = deviceInfo['battery_level'] as int?;
if (batteryLevel == null) {
return 'Niveau de batterie inconnu';
}
if (batteryLevel < 10) {
return 'Batterie insuffisante ($batteryLevel%) - Min. 10% requis';
}
// Vérifier la certification Stripe (inclut déjà modèle, iOS version, NFC Android)
final stripeCertified = deviceInfo['device_stripe_certified'] ?? deviceInfo['device_supports_tap_to_pay'];
if (stripeCertified != true) {
return 'Appareil non certifié pour Tap to Pay';
}
// Tout est OK
return null;
}
/// Version asynchrone avec vérification NFC en temps réel (Android uniquement)
Future<String?> getTapToPayUnavailableReasonAsync() async {
// Sur Web, Tap to Pay n'est jamais disponible
if (kIsWeb) {
return 'Tap to Pay non disponible sur Web';
}
final deviceInfo = getStoredDeviceInfo();
final platform = deviceInfo['platform'];
// Vérifier la batterie
final batteryLevel = deviceInfo['battery_level'] as int?;
if (batteryLevel == null) {
return 'Niveau de batterie inconnu';
}
if (batteryLevel < 10) {
return 'Batterie insuffisante ($batteryLevel%) - Min. 10% requis';
}
// Sur Android, vérifier le NFC EN TEMPS RÉEL (peut être désactivé dans les paramètres)
if (platform == 'Android') {
try {
final nfcAvailable = await NfcManager.instance.isAvailable();
if (!nfcAvailable) {
return 'NFC désactivé - Activez-le dans les paramètres Android';
}
} catch (e) {
debugPrint('⚠️ Impossible de vérifier le statut NFC: $e');
return 'Impossible de vérifier le NFC';
}
}
// Vérifier la certification Stripe (inclut déjà modèle, iOS version)
final stripeCertified = deviceInfo['device_stripe_certified'] ?? deviceInfo['device_supports_tap_to_pay'];
if (stripeCertified != true) {
return 'Appareil non certifié pour Tap to Pay';
}
// Tout est OK
return null;
}
}

View File

@@ -0,0 +1,312 @@
import 'package:flutter/foundation.dart';
import 'package:geosector_app/core/services/api_service.dart';
import 'package:geosector_app/core/data/models/event_stats_model.dart';
import 'package:geosector_app/core/utils/api_exception.dart';
import 'package:intl/intl.dart';
/// Service pour récupérer les statistiques d'événements depuis l'API.
///
/// Ce service est un singleton qui gère les appels API vers les endpoints
/// /api/events/stats/*. Il est accessible uniquement aux admins (rôle >= 2).
class EventStatsService {
static EventStatsService? _instance;
EventStatsService._internal();
static EventStatsService get instance {
_instance ??= EventStatsService._internal();
return _instance!;
}
final _dateFormat = DateFormat('yyyy-MM-dd');
/// Récupère le résumé des stats pour une date donnée.
///
/// GET /api/events/stats/summary?date=YYYY-MM-DD&entity_id=X
///
/// [date] : Date à récupérer (défaut: aujourd'hui)
/// [entityId] : ID de l'entité (super-admin uniquement, optionnel)
Future<EventSummary> getSummary({
DateTime? date,
int? entityId,
}) async {
try {
final queryParams = <String, dynamic>{};
if (date != null) {
queryParams['date'] = _dateFormat.format(date);
}
if (entityId != null) {
queryParams['entity_id'] = entityId.toString();
}
debugPrint('📊 [EventStats] Récupération résumé: $queryParams');
final response = await ApiService.instance.get(
'/events/stats/summary',
queryParameters: queryParams.isNotEmpty ? queryParams : null,
);
if (response.data['status'] == 'success') {
return EventSummary.fromJson(response.data['data']);
} else {
throw ApiException(
response.data['message'] ?? 'Erreur lors de la récupération du résumé',
);
}
} catch (e) {
debugPrint('❌ [EventStats] Erreur getSummary: $e');
if (e is ApiException) rethrow;
throw ApiException('Erreur lors de la récupération des statistiques', originalError: e);
}
}
/// Récupère les stats quotidiennes pour une période.
///
/// GET /api/events/stats/daily?from=YYYY-MM-DD&to=YYYY-MM-DD&events=type1,type2
///
/// [from] : Date de début (obligatoire)
/// [to] : Date de fin (obligatoire)
/// [events] : Types d'événements à filtrer (optionnel)
/// [entityId] : ID de l'entité (super-admin uniquement, optionnel)
///
/// Limite : 90 jours maximum
Future<DailyStats> getDailyStats({
required DateTime from,
required DateTime to,
List<String>? events,
int? entityId,
}) async {
try {
// Vérifier la limite de 90 jours
final daysDiff = to.difference(from).inDays;
if (daysDiff > 90) {
throw const ApiException('La période ne peut pas dépasser 90 jours');
}
final queryParams = <String, dynamic>{
'from': _dateFormat.format(from),
'to': _dateFormat.format(to),
};
if (events != null && events.isNotEmpty) {
queryParams['events'] = events.join(',');
}
if (entityId != null) {
queryParams['entity_id'] = entityId.toString();
}
debugPrint('📊 [EventStats] Récupération stats quotidiennes: $queryParams');
final response = await ApiService.instance.get(
'/events/stats/daily',
queryParameters: queryParams,
);
if (response.data['status'] == 'success') {
return DailyStats.fromJson(response.data['data']);
} else {
throw ApiException(
response.data['message'] ?? 'Erreur lors de la récupération des stats quotidiennes',
);
}
} catch (e) {
debugPrint('❌ [EventStats] Erreur getDailyStats: $e');
if (e is ApiException) rethrow;
throw ApiException('Erreur lors de la récupération des statistiques quotidiennes', originalError: e);
}
}
/// Récupère les stats hebdomadaires pour une période.
///
/// GET /api/events/stats/weekly?from=YYYY-MM-DD&to=YYYY-MM-DD&events=type1,type2
///
/// [from] : Date de début (obligatoire)
/// [to] : Date de fin (obligatoire)
/// [events] : Types d'événements à filtrer (optionnel)
/// [entityId] : ID de l'entité (super-admin uniquement, optionnel)
///
/// Limite : 365 jours maximum
Future<WeeklyStats> getWeeklyStats({
required DateTime from,
required DateTime to,
List<String>? events,
int? entityId,
}) async {
try {
// Vérifier la limite de 365 jours
final daysDiff = to.difference(from).inDays;
if (daysDiff > 365) {
throw const ApiException('La période ne peut pas dépasser 365 jours');
}
final queryParams = <String, dynamic>{
'from': _dateFormat.format(from),
'to': _dateFormat.format(to),
};
if (events != null && events.isNotEmpty) {
queryParams['events'] = events.join(',');
}
if (entityId != null) {
queryParams['entity_id'] = entityId.toString();
}
debugPrint('📊 [EventStats] Récupération stats hebdomadaires: $queryParams');
final response = await ApiService.instance.get(
'/events/stats/weekly',
queryParameters: queryParams,
);
if (response.data['status'] == 'success') {
return WeeklyStats.fromJson(response.data['data']);
} else {
throw ApiException(
response.data['message'] ?? 'Erreur lors de la récupération des stats hebdomadaires',
);
}
} catch (e) {
debugPrint('❌ [EventStats] Erreur getWeeklyStats: $e');
if (e is ApiException) rethrow;
throw ApiException('Erreur lors de la récupération des statistiques hebdomadaires', originalError: e);
}
}
/// Récupère les stats mensuelles pour une année.
///
/// GET /api/events/stats/monthly?year=YYYY&events=type1,type2
///
/// [year] : Année (défaut: année courante)
/// [events] : Types d'événements à filtrer (optionnel)
/// [entityId] : ID de l'entité (super-admin uniquement, optionnel)
Future<MonthlyStats> getMonthlyStats({
int? year,
List<String>? events,
int? entityId,
}) async {
try {
final queryParams = <String, dynamic>{};
if (year != null) {
queryParams['year'] = year.toString();
}
if (events != null && events.isNotEmpty) {
queryParams['events'] = events.join(',');
}
if (entityId != null) {
queryParams['entity_id'] = entityId.toString();
}
debugPrint('📊 [EventStats] Récupération stats mensuelles: $queryParams');
final response = await ApiService.instance.get(
'/events/stats/monthly',
queryParameters: queryParams.isNotEmpty ? queryParams : null,
);
if (response.data['status'] == 'success') {
return MonthlyStats.fromJson(response.data['data']);
} else {
throw ApiException(
response.data['message'] ?? 'Erreur lors de la récupération des stats mensuelles',
);
}
} catch (e) {
debugPrint('❌ [EventStats] Erreur getMonthlyStats: $e');
if (e is ApiException) rethrow;
throw ApiException('Erreur lors de la récupération des statistiques mensuelles', originalError: e);
}
}
/// Récupère les détails des événements pour une date.
///
/// GET /api/events/stats/details?date=YYYY-MM-DD&event=type&limit=50&offset=0
///
/// [date] : Date à récupérer (obligatoire)
/// [event] : Type d'événement à filtrer (optionnel)
/// [limit] : Nombre de résultats max (défaut: 50, max: 100)
/// [offset] : Pagination (défaut: 0)
/// [entityId] : ID de l'entité (super-admin uniquement, optionnel)
Future<EventDetails> getDetails({
required DateTime date,
String? event,
int? limit,
int? offset,
int? entityId,
}) async {
try {
final queryParams = <String, dynamic>{
'date': _dateFormat.format(date),
};
if (event != null && event.isNotEmpty) {
queryParams['event'] = event;
}
if (limit != null) {
queryParams['limit'] = limit.toString();
}
if (offset != null) {
queryParams['offset'] = offset.toString();
}
if (entityId != null) {
queryParams['entity_id'] = entityId.toString();
}
debugPrint('📊 [EventStats] Récupération détails: $queryParams');
final response = await ApiService.instance.get(
'/events/stats/details',
queryParameters: queryParams,
);
if (response.data['status'] == 'success') {
return EventDetails.fromJson(response.data['data']);
} else {
throw ApiException(
response.data['message'] ?? 'Erreur lors de la récupération des détails',
);
}
} catch (e) {
debugPrint('❌ [EventStats] Erreur getDetails: $e');
if (e is ApiException) rethrow;
throw ApiException('Erreur lors de la récupération des détails', originalError: e);
}
}
/// Récupère les types d'événements disponibles.
///
/// GET /api/events/stats/types
Future<EventTypes> getEventTypes() async {
try {
debugPrint('📊 [EventStats] Récupération types d\'événements');
final response = await ApiService.instance.get('/events/stats/types');
if (response.data['status'] == 'success') {
return EventTypes.fromJson(response.data['data']);
} else {
throw ApiException(
response.data['message'] ?? 'Erreur lors de la récupération des types',
);
}
} catch (e) {
debugPrint('❌ [EventStats] Erreur getEventTypes: $e');
if (e is ApiException) rethrow;
throw ApiException('Erreur lors de la récupération des types d\'événements', originalError: e);
}
}
/// Réinitialise le singleton (pour les tests)
static void reset() {
_instance = null;
}
}

View File

@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:mek_stripe_terminal/mek_stripe_terminal.dart';
import 'api_service.dart';
import 'device_info_service.dart';
@@ -13,6 +14,7 @@ class StripeTapToPayService {
StripeTapToPayService._internal();
bool _isInitialized = false;
static bool _terminalInitialized = false; // Flag STATIC pour savoir si Terminal.initTerminal a été appelé (global à l'app)
String? _stripeAccountId;
String? _locationId;
bool _deviceCompatible = false;
@@ -78,6 +80,36 @@ class StripeTapToPayService {
return false;
}
// 4. Initialiser le SDK Stripe Terminal (une seule fois par session app)
if (!_terminalInitialized) {
try {
debugPrint('🔧 Initialisation du SDK Stripe Terminal...');
await Terminal.initTerminal(
fetchToken: _fetchConnectionToken,
);
_terminalInitialized = true;
debugPrint('✅ SDK Stripe Terminal initialisé');
} catch (e) {
final errorMsg = e.toString().toLowerCase();
debugPrint('🔍 Exception capturée lors de l\'initialisation: $e');
debugPrint('🔍 Type d\'exception: ${e.runtimeType}');
// Vérifier plusieurs variantes du message "already initialized"
if (errorMsg.contains('already initialized') ||
errorMsg.contains('already been initialized') ||
errorMsg.contains('sdkfailure')) {
debugPrint(' SDK Stripe Terminal déjà initialisé (détecté via exception)');
_terminalInitialized = true;
// Ne PAS rethrow - continuer normalement car c'est un état valide
} else {
debugPrint('❌ Erreur inattendue lors de l\'initialisation du SDK');
rethrow; // Autre erreur, on la propage
}
}
} else {
debugPrint(' SDK Stripe Terminal déjà initialisé, réutilisation');
}
_isInitialized = true;
debugPrint('✅ Tap to Pay initialisé avec succès');
@@ -101,6 +133,34 @@ class StripeTapToPayService {
}
}
/// Callback pour récupérer un token de connexion depuis l'API
Future<String> _fetchConnectionToken() async {
try {
debugPrint('🔑 Récupération du token de connexion Stripe...');
final response = await ApiService.instance.post(
'/stripe/terminal/connection-token',
data: {
'amicale_id': CurrentAmicaleService.instance.amicaleId,
'stripe_account': _stripeAccountId,
'location_id': _locationId,
}
);
final token = response.data['secret'];
if (token == null || token.isEmpty) {
throw Exception('Token de connexion invalide');
}
debugPrint('✅ Token de connexion récupéré');
return token;
} catch (e) {
debugPrint('❌ Erreur récupération token: $e');
throw Exception('Impossible de récupérer le token de connexion');
}
}
/// Crée un PaymentIntent pour un paiement Tap to Pay
Future<PaymentIntentResult?> createPaymentIntent({
required int amountInCents,
@@ -124,21 +184,25 @@ class StripeTapToPayService {
// Extraire passage_id des metadata si présent
final passageId = metadata?['passage_id'] ?? '0';
final requestData = {
'amount': amountInCents,
'currency': 'eur',
'description': description ?? 'Calendrier pompiers',
'payment_method_types': ['card_present'], // Pour Tap to Pay
'capture_method': 'automatic',
'passage_id': int.tryParse(passageId.toString()) ?? 0,
'amicale_id': CurrentAmicaleService.instance.amicaleId,
'member_id': CurrentUserService.instance.userId,
'stripe_account': _stripeAccountId,
'location_id': _locationId,
'metadata': metadata,
};
debugPrint('🔵 Données envoyées create-intent: $requestData');
final response = await ApiService.instance.post(
'/stripe/payments/create-intent',
data: {
'amount': amountInCents,
'currency': 'eur',
'description': description ?? 'Calendrier pompiers',
'payment_method_types': ['card_present'], // Pour Tap to Pay
'capture_method': 'automatic',
'passage_id': int.tryParse(passageId.toString()) ?? 0,
'amicale_id': CurrentAmicaleService.instance.amicaleId,
'member_id': CurrentUserService.instance.userId,
'stripe_account': _stripeAccountId,
'location_id': _locationId,
'metadata': metadata,
},
data: requestData,
);
final result = PaymentIntentResult(
@@ -169,11 +233,110 @@ class StripeTapToPayService {
}
}
/// Simule le processus de collecte de paiement
/// (Dans la version finale, cela appellera le SDK natif)
/// Découvre et connecte le reader Tap to Pay local
Future<bool> _ensureReaderConnected() async {
try {
debugPrint('🔍 Découverte du reader Tap to Pay...');
// Configuration pour découvrir le reader local (Tap to Pay)
// Détection de l'environnement via l'URL de l'API (plus fiable que kDebugMode)
final apiUrl = ApiService.instance.baseUrl;
final isProduction = apiUrl.contains('app3.geosector.fr');
final isSimulated = !isProduction; // Simulé uniquement si pas en PROD
final config = TapToPayDiscoveryConfiguration(
isSimulated: isSimulated,
);
debugPrint('🔧 Environnement: ${isProduction ? "PRODUCTION (réel)" : "DEV/REC (simulé)"}');
debugPrint('🔧 isSimulated: $isSimulated');
// Découvrir les readers avec un Completer pour gérer le stream correctement
final completer = Completer<Reader?>();
StreamSubscription<List<Reader>>? subscription;
subscription = Terminal.instance.discoverReaders(config).listen(
(readers) {
debugPrint('📡 Stream readers reçu: ${readers.length} reader(s)');
if (readers.isNotEmpty && !completer.isCompleted) {
debugPrint('📱 ${readers.length} reader(s) trouvé(s): ${readers.map((r) => r.label).join(", ")}');
completer.complete(readers.first);
subscription?.cancel();
}
},
onError: (error) {
debugPrint('❌ Erreur lors de la découverte: $error');
if (!completer.isCompleted) {
completer.complete(null);
}
subscription?.cancel();
},
onDone: () {
debugPrint('🏁 Stream découverte terminé');
if (!completer.isCompleted) {
debugPrint('⚠️ Découverte terminée sans reader trouvé');
completer.complete(null);
}
},
);
debugPrint('⏳ Attente du résultat de la découverte...');
// Attendre le résultat avec timeout
final reader = await completer.future.timeout(
const Duration(seconds: 15),
onTimeout: () {
debugPrint('⏱️ Timeout lors de la découverte du reader');
subscription?.cancel();
return null;
},
);
if (reader == null) {
debugPrint('❌ Aucun reader Tap to Pay trouvé');
return false;
}
debugPrint('📱 Reader trouvé: ${reader.label}');
// Se connecter au reader
debugPrint('🔌 Connexion au reader...');
final connectionConfig = TapToPayConnectionConfiguration(
locationId: _locationId ?? '',
readerDelegate: null, // Pas de delegate pour l'instant
);
await Terminal.instance.connectReader(
reader,
configuration: connectionConfig,
);
debugPrint('✅ Connecté au reader Tap to Pay');
return true;
} catch (e) {
debugPrint('❌ Erreur connexion reader: $e');
return false;
}
}
/// Collecte le paiement avec le SDK Stripe Terminal
Future<bool> collectPayment(PaymentIntentResult paymentIntent) async {
try {
debugPrint('💳 Collecte du paiement...');
debugPrint('💳 Collecte du paiement avec SDK...');
_paymentStatusController.add(TapToPayStatus(
type: TapToPayStatusType.processing,
message: 'Préparation du terminal...',
paymentIntentId: paymentIntent.paymentIntentId,
));
// 1. S'assurer qu'un reader est connecté
debugPrint('🔌 Vérification connexion reader...');
final readerConnected = await _ensureReaderConnected();
if (!readerConnected) {
throw Exception('Impossible de se connecter au reader Tap to Pay');
}
_paymentStatusController.add(TapToPayStatus(
type: TapToPayStatusType.processing,
@@ -181,11 +344,22 @@ class StripeTapToPayService {
paymentIntentId: paymentIntent.paymentIntentId,
));
// TODO: Ici, intégrer le vrai SDK Stripe Terminal
// Pour l'instant, on simule une attente
await Future.delayed(const Duration(seconds: 2));
// 2. Récupérer le PaymentIntent depuis le SDK avec le clientSecret
debugPrint('💳 Récupération du PaymentIntent...');
final stripePaymentIntent = await Terminal.instance.retrievePaymentIntent(
paymentIntent.clientSecret,
);
debugPrint('✅ Paiement collecté');
// 3. Utiliser le SDK Stripe Terminal pour collecter le paiement
debugPrint('💳 En attente du paiement sans contact...');
final collectedPaymentIntent = await Terminal.instance.collectPaymentMethod(
stripePaymentIntent,
);
// Sauvegarder le PaymentIntent collecté pour l'étape de confirmation
paymentIntent._collectedPaymentIntent = collectedPaymentIntent;
debugPrint('✅ Paiement collecté via SDK');
_paymentStatusController.add(TapToPayStatus(
type: TapToPayStatusType.confirming,
@@ -208,33 +382,37 @@ class StripeTapToPayService {
}
}
/// Confirme le paiement auprès du serveur
/// Confirme le paiement via le SDK Stripe Terminal
Future<bool> confirmPayment(PaymentIntentResult paymentIntent) async {
try {
debugPrint('✅ Confirmation du paiement...');
debugPrint('✅ Confirmation du paiement via SDK...');
// Notifier le serveur du succès
await ApiService.instance.post(
'/stripe/payments/confirm',
data: {
'payment_intent_id': paymentIntent.paymentIntentId,
'amount': paymentIntent.amount,
'status': 'succeeded',
'amicale_id': CurrentAmicaleService.instance.amicaleId,
'member_id': CurrentUserService.instance.userId,
},
// Vérifier que le paiement a été collecté
if (paymentIntent._collectedPaymentIntent == null) {
throw Exception('Le paiement doit d\'abord être collecté');
}
// Utiliser le SDK Stripe Terminal pour confirmer le paiement
final confirmedPaymentIntent = await Terminal.instance.confirmPaymentIntent(
paymentIntent._collectedPaymentIntent!,
);
debugPrint('🎉 Paiement confirmé avec succès');
// Vérifier le statut final
if (confirmedPaymentIntent.status == PaymentIntentStatus.succeeded) {
debugPrint('🎉 Paiement confirmé avec succès via SDK');
debugPrint(' Payment Intent: ${paymentIntent.paymentIntentId}');
_paymentStatusController.add(TapToPayStatus(
type: TapToPayStatusType.success,
message: 'Paiement réussi',
paymentIntentId: paymentIntent.paymentIntentId,
amount: paymentIntent.amount,
));
_paymentStatusController.add(TapToPayStatus(
type: TapToPayStatusType.success,
message: 'Paiement réussi',
paymentIntentId: paymentIntent.paymentIntentId,
amount: paymentIntent.amount,
));
return true;
return true;
} else {
throw Exception('Paiement non confirmé: ${confirmedPaymentIntent.status}');
}
} catch (e) {
debugPrint('❌ Erreur confirmation paiement: $e');
@@ -304,6 +482,9 @@ class PaymentIntentResult {
final String clientSecret;
final int amount;
// PaymentIntent collecté (utilisé entre collectPayment et confirmPayment)
PaymentIntent? _collectedPaymentIntent;
PaymentIntentResult({
required this.paymentIntentId,
required this.clientSecret,

View File

@@ -31,6 +31,7 @@ class ApiException implements Exception {
if (response?.data != null) {
try {
final data = response!.data as Map<String, dynamic>;
debugPrint('🔍 API Error Response: $data');
// Message spécifique de l'API
if (data.containsKey('message')) {
@@ -42,12 +43,21 @@ class ApiException implements Exception {
errorCode = data['error_code'] as String;
}
// Détails supplémentaires
// Détails supplémentaires - peut être une Map ou une List
if (data.containsKey('errors')) {
details = data['errors'] as Map<String, dynamic>?;
final errorsData = data['errors'];
if (errorsData is Map<String, dynamic>) {
// Format: {field: [errors]}
details = errorsData;
} else if (errorsData is List) {
// Format: [error1, error2, ...]
details = {'errors': errorsData};
}
debugPrint('🔍 Validation Errors: $details');
}
} catch (e) {
// Si on ne peut pas parser la réponse, utiliser le message par défaut
debugPrint('⚠️ Impossible de parser la réponse d\'erreur: $e');
}
}
@@ -130,7 +140,43 @@ class ApiException implements Exception {
String toString() => message;
/// Obtenir un message d'erreur formaté pour l'affichage
String get displayMessage => message;
String get displayMessage {
debugPrint('🔍 [displayMessage] statusCode: $statusCode');
debugPrint('🔍 [displayMessage] isValidationError: $isValidationError');
debugPrint('🔍 [displayMessage] details: $details');
debugPrint('🔍 [displayMessage] details != null: ${details != null}');
debugPrint('🔍 [displayMessage] details!.isNotEmpty: ${details != null ? details!.isNotEmpty : "null"}');
// Si c'est une erreur de validation avec des détails, formater le message
if (isValidationError && details != null && details!.isNotEmpty) {
debugPrint('✅ [displayMessage] Formatage des erreurs de validation');
final buffer = StringBuffer(message);
buffer.write('\n');
details!.forEach((field, errors) {
if (errors is List) {
// Si le champ est 'errors', c'est une liste simple d'erreurs
if (field == 'errors') {
for (final error in errors) {
buffer.write('$error\n');
}
} else {
// Sinon c'est un champ avec une liste d'erreurs
for (final error in errors) {
buffer.write('$field: $error\n');
}
}
} else {
buffer.write('$field: $errors\n');
}
});
return buffer.toString().trim();
}
debugPrint('⚠️ [displayMessage] Retour du message simple');
return message;
}
/// Vérifier si c'est une erreur de validation
bool get isValidationError => statusCode == 422 || statusCode == 400;