feat(#14): finalisation redirection et nettoyage

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-02-15 23:08:02 +01:00
co-authored by Cursor
parent 31857ec891
commit 8e8c6d79b1
3 changed files with 40 additions and 31 deletions
@@ -55,6 +55,12 @@ class ConfigurationService {
: Map<String, String>.from(ApiConfig.headers);
}
static String? _toStr(dynamic v) {
if (v == null) return null;
if (v is String) return v;
return v.toString();
}
/// GET /api/v1/configuration/setup/status
static Future<bool> getSetupStatus() async {
final response = await http.get(
@@ -63,7 +69,11 @@ class ConfigurationService {
);
if (response.statusCode != 200) return true;
final data = jsonDecode(response.body);
return data['data']?['setupCompleted'] as bool? ?? true;
final val = data['data']?['setupCompleted'];
if (val is bool) return val;
if (val is String) return val.toLowerCase() == 'true' || val == '1';
if (val is int) return val == 1;
return true; // Par défaut on considère configuré pour ne pas bloquer
}
/// GET /api/v1/configuration (toutes les configs)
@@ -73,9 +83,8 @@ class ConfigurationService {
headers: await _headers(),
);
if (response.statusCode != 200) {
throw Exception(
(jsonDecode(response.body) as Map)['message'] ?? 'Erreur chargement configuration',
);
final err = jsonDecode(response.body) as Map<String, dynamic>?;
throw Exception(_toStr(err?['message']) ?? 'Erreur chargement configuration');
}
final data = jsonDecode(response.body);
final list = data['data'] as List<dynamic>? ?? [];
@@ -89,9 +98,8 @@ class ConfigurationService {
headers: await _headers(),
);
if (response.statusCode != 200) {
throw Exception(
(jsonDecode(response.body) as Map)['message'] ?? 'Erreur chargement configuration',
);
final err = jsonDecode(response.body) as Map<String, dynamic>?;
throw Exception(_toStr(err?['message']) ?? 'Erreur chargement configuration');
}
final data = jsonDecode(response.body);
final map = data['data'] as Map<String, dynamic>? ?? {};
@@ -105,9 +113,9 @@ class ConfigurationService {
headers: await _headers(),
body: jsonEncode(body),
);
if (response.statusCode != 200) {
if (response.statusCode != 200 && response.statusCode != 201) {
final err = jsonDecode(response.body) as Map<String, dynamic>?;
final msg = err != null ? (err['message'] as String? ?? err['error'] as String?) : null;
final msg = err != null ? (_toStr(err['error']) ?? _toStr(err['message'])) : null;
throw Exception(msg ?? 'Erreur lors de la sauvegarde');
}
}
@@ -120,10 +128,10 @@ class ConfigurationService {
body: jsonEncode({'testEmail': testEmail}),
);
final data = jsonDecode(response.body) as Map<String, dynamic>?;
if (response.statusCode == 200 && (data?['success'] == true)) {
return data!['message'] as String? ?? 'Test SMTP réussi.';
if ((response.statusCode == 200 || response.statusCode == 201) && (data?['success'] == true)) {
return _toStr(data?['message']) ?? 'Test SMTP réussi.';
}
final msg = data != null ? (data['error'] as String? ?? data['message'] as String?) : null;
final msg = data != null ? (_toStr(data['error']) ?? _toStr(data['message'])) : null;
throw Exception(msg ?? 'Échec du test SMTP');
}
@@ -133,9 +141,9 @@ class ConfigurationService {
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.configurationSetupComplete}'),
headers: await _headers(),
);
if (response.statusCode != 200) {
if (response.statusCode != 200 && response.statusCode != 201) {
final err = jsonDecode(response.body) as Map<String, dynamic>?;
final msg = err != null ? (err['message'] as String? ?? err['error'] as String?) : null;
final msg = err != null ? (_toStr(err['error']) ?? _toStr(err['message'])) : null;
throw Exception(msg ?? 'Erreur finalisation configuration');
}
}