Merge squash develop into master (incl. #14 première config setup/complete)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
MARTIN Julien 2026-02-15 23:20:15 +01:00
parent 11aa66feff
commit dfe7daed14
8 changed files with 150 additions and 51 deletions

View File

@ -53,8 +53,7 @@ export class ConfigController {
// @Roles('super_admin') // @Roles('super_admin')
async completeSetup(@Request() req: any) { async completeSetup(@Request() req: any) {
try { try {
// TODO: Récupérer l'ID utilisateur depuis le JWT const userId = req.user?.id ?? null;
const userId = req.user?.id || 'system';
await this.configService.markSetupCompleted(userId); await this.configService.markSetupCompleted(userId);

View File

@ -259,10 +259,10 @@ export class AppConfigService implements OnModuleInit {
/** /**
* Marquer la configuration initiale comme terminée * Marquer la configuration initiale comme terminée
* @param userId ID de l'utilisateur qui termine la configuration * @param userId ID de l'utilisateur qui termine la configuration (null si non authentifié)
*/ */
async markSetupCompleted(userId: string): Promise<void> { async markSetupCompleted(userId: string | null): Promise<void> {
await this.set('setup_completed', 'true', userId); await this.set('setup_completed', 'true', userId ?? undefined);
this.logger.log('✅ Configuration initiale marquée comme terminée'); this.logger.log('✅ Configuration initiale marquée comme terminée');
} }

View File

@ -0,0 +1,37 @@
# Ticket #14 Note pour modifications backend
**Contexte :** Première connexion admin → panneau Paramètres, déblocage après clic sur « Sauvegarder ». Le front appelle `POST /api/v1/configuration/setup/complete` au clic sur Sauvegarder.
## Problème
Erreur renvoyée par le back :
`invalid input syntax for type uuid: "system"`
- Le controller fait `const userId = req.user?.id || 'system'` puis `markSetupCompleted(userId)`.
- Le service `set()` fait `config.modifiePar = { id: userId }` ; la colonne `modifie_par` est une FK UUID vers `users`.
- La chaîne `"system"` nest pas un UUID valide → erreur PostgreSQL.
## Modifications à apporter au backend
**Option A Accepter labsence dutilisateur (recommandé si la route peut être appelée sans JWT)**
1. **`config.controller.ts`** (route `completeSetup`)
- Remplacer :
`const userId = req.user?.id || 'system';`
- Par :
`const userId = req.user?.id ?? null;`
2. **`config.service.ts`** (`markSetupCompleted`)
- Changer la signature :
`async markSetupCompleted(userId: string | null): Promise<void>`
- Et appeler :
`await this.set('setup_completed', 'true', userId ?? undefined);`
- Dans `set()`, ne pas remplir `modifiePar` quand `userId` est absent (déjà le cas si `if (userId)`).
**Option B Imposer un utilisateur authentifié**
- Activer le guard JWT (et éventuellement RolesGuard) sur `POST /configuration/setup/complete` pour que `req.user` soit toujours défini, et garder `userId = req.user.id` (plus de fallback `'system'`).
---
Une fois le back modifié, le flux « Sauvegarder » → déblocage des panneaux fonctionne sans erreur.

View File

@ -6,7 +6,7 @@ class Env {
); );
// Construit une URL vers l'API v1 à partir d'un chemin (commençant par '/') // Construit une URL vers l'API v1 à partir d'un chemin (commençant par '/')
static String apiV1(String path) => "${apiBaseUrl}/api/v1$path"; static String apiV1(String path) => '$apiBaseUrl/api/v1$path';
} }

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:p_tits_pas/services/configuration_service.dart';
import 'package:p_tits_pas/widgets/admin/assistante_maternelle_management_widget.dart'; import 'package:p_tits_pas/widgets/admin/assistante_maternelle_management_widget.dart';
import 'package:p_tits_pas/widgets/admin/gestionnaire_management_widget.dart'; import 'package:p_tits_pas/widgets/admin/gestionnaire_management_widget.dart';
import 'package:p_tits_pas/widgets/admin/parent_managmant_widget.dart'; import 'package:p_tits_pas/widgets/admin/parent_managmant_widget.dart';
@ -14,12 +15,32 @@ class AdminDashboardScreen extends StatefulWidget {
} }
class _AdminDashboardScreenState extends State<AdminDashboardScreen> { class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
/// 0 = Gestion des utilisateurs, 1 = Paramètres bool? _setupCompleted;
int mainTabIndex = 0; int mainTabIndex = 0;
/// Sous-onglet quand mainTabIndex == 0 : 0=Gestionnaires, 1=Parents, 2=AM, 3=Administrateurs
int subIndex = 0; int subIndex = 0;
@override
void initState() {
super.initState();
_loadSetupStatus();
}
Future<void> _loadSetupStatus() async {
try {
final completed = await ConfigurationService.getSetupStatus();
if (!mounted) return;
setState(() {
_setupCompleted = completed;
if (!completed) mainTabIndex = 1;
});
} catch (e) {
if (mounted) setState(() {
_setupCompleted = false;
mainTabIndex = 1;
});
}
}
void onMainTabChange(int index) { void onMainTabChange(int index) {
setState(() { setState(() {
mainTabIndex = index; mainTabIndex = index;
@ -34,6 +55,11 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_setupCompleted == null) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
return Scaffold( return Scaffold(
appBar: PreferredSize( appBar: PreferredSize(
preferredSize: const Size.fromHeight(60.0), preferredSize: const Size.fromHeight(60.0),
@ -46,6 +72,7 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
child: DashboardAppBarAdmin( child: DashboardAppBarAdmin(
selectedIndex: mainTabIndex, selectedIndex: mainTabIndex,
onTabChange: onMainTabChange, onTabChange: onMainTabChange,
setupCompleted: _setupCompleted!,
), ),
), ),
), ),
@ -67,7 +94,7 @@ class _AdminDashboardScreenState extends State<AdminDashboardScreen> {
Widget _getBody() { Widget _getBody() {
if (mainTabIndex == 1) { if (mainTabIndex == 1) {
return const ParametresPanel(); return ParametresPanel(redirectToLoginAfterSave: !_setupCompleted!);
} }
switch (subIndex) { switch (subIndex) {
case 0: case 0:

View File

@ -55,6 +55,12 @@ class ConfigurationService {
: Map<String, String>.from(ApiConfig.headers); : 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 /// GET /api/v1/configuration/setup/status
static Future<bool> getSetupStatus() async { static Future<bool> getSetupStatus() async {
final response = await http.get( final response = await http.get(
@ -63,7 +69,11 @@ class ConfigurationService {
); );
if (response.statusCode != 200) return true; if (response.statusCode != 200) return true;
final data = jsonDecode(response.body); 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) /// GET /api/v1/configuration (toutes les configs)
@ -73,9 +83,8 @@ class ConfigurationService {
headers: await _headers(), headers: await _headers(),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception( final err = jsonDecode(response.body) as Map<String, dynamic>?;
(jsonDecode(response.body) as Map)['message'] ?? 'Erreur chargement configuration', throw Exception(_toStr(err?['message']) ?? 'Erreur chargement configuration');
);
} }
final data = jsonDecode(response.body); final data = jsonDecode(response.body);
final list = data['data'] as List<dynamic>? ?? []; final list = data['data'] as List<dynamic>? ?? [];
@ -89,9 +98,8 @@ class ConfigurationService {
headers: await _headers(), headers: await _headers(),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception( final err = jsonDecode(response.body) as Map<String, dynamic>?;
(jsonDecode(response.body) as Map)['message'] ?? 'Erreur chargement configuration', throw Exception(_toStr(err?['message']) ?? 'Erreur chargement configuration');
);
} }
final data = jsonDecode(response.body); final data = jsonDecode(response.body);
final map = data['data'] as Map<String, dynamic>? ?? {}; final map = data['data'] as Map<String, dynamic>? ?? {};
@ -105,9 +113,10 @@ class ConfigurationService {
headers: await _headers(), headers: await _headers(),
body: jsonEncode(body), body: jsonEncode(body),
); );
if (response.statusCode != 200) { if (response.statusCode != 200 && response.statusCode != 201) {
final err = jsonDecode(response.body) as Map; final err = jsonDecode(response.body) as Map<String, dynamic>?;
throw Exception(err['message'] ?? 'Erreur lors de la sauvegarde'); final msg = err != null ? (_toStr(err['error']) ?? _toStr(err['message'])) : null;
throw Exception(msg ?? 'Erreur lors de la sauvegarde');
} }
} }
@ -118,11 +127,12 @@ class ConfigurationService {
headers: await _headers(), headers: await _headers(),
body: jsonEncode({'testEmail': testEmail}), body: jsonEncode({'testEmail': testEmail}),
); );
final data = jsonDecode(response.body) as Map; final data = jsonDecode(response.body) as Map<String, dynamic>?;
if (response.statusCode == 200 && data['success'] == true) { if ((response.statusCode == 200 || response.statusCode == 201) && (data?['success'] == true)) {
return data['message'] as String? ?? 'Test SMTP réussi.'; return _toStr(data?['message']) ?? 'Test SMTP réussi.';
} }
throw Exception(data['error'] ?? data['message'] ?? 'Échec du test SMTP'); final msg = data != null ? (_toStr(data['error']) ?? _toStr(data['message'])) : null;
throw Exception(msg ?? 'Échec du test SMTP');
} }
/// POST /api/v1/configuration/setup/complete (après première config) /// POST /api/v1/configuration/setup/complete (après première config)
@ -131,9 +141,10 @@ class ConfigurationService {
Uri.parse('${ApiConfig.baseUrl}${ApiConfig.configurationSetupComplete}'), Uri.parse('${ApiConfig.baseUrl}${ApiConfig.configurationSetupComplete}'),
headers: await _headers(), headers: await _headers(),
); );
if (response.statusCode != 200) { if (response.statusCode != 200 && response.statusCode != 201) {
final err = jsonDecode(response.body) as Map; final err = jsonDecode(response.body) as Map<String, dynamic>?;
throw Exception(err['message'] ?? 'Erreur finalisation configuration'); final msg = err != null ? (_toStr(err['error']) ?? _toStr(err['message'])) : null;
throw Exception(msg ?? 'Erreur finalisation configuration');
} }
} }
} }

View File

@ -1,14 +1,18 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:p_tits_pas/services/auth_service.dart';
/// Barre principale du dashboard admin : 2 onglets (Gestion des utilisateurs | Paramètres) + infos utilisateur. /// Barre du dashboard admin : onglets Gestion des utilisateurs | Paramètres + déconnexion.
class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidget { class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidget {
final int selectedIndex; final int selectedIndex;
final ValueChanged<int> onTabChange; final ValueChanged<int> onTabChange;
final bool setupCompleted;
const DashboardAppBarAdmin({ const DashboardAppBarAdmin({
Key? key, Key? key,
required this.selectedIndex, required this.selectedIndex,
required this.onTabChange, required this.onTabChange,
this.setupCompleted = true,
}) : super(key: key); }) : super(key: key);
@override @override
@ -32,9 +36,9 @@ class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidge
child: Row( child: Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
_buildNavItem(context, 'Gestion des utilisateurs', 0), _buildNavItem(context, 'Gestion des utilisateurs', 0, enabled: setupCompleted),
const SizedBox(width: 24), const SizedBox(width: 24),
_buildNavItem(context, 'Paramètres', 1), _buildNavItem(context, 'Paramètres', 1, enabled: true),
], ],
), ),
), ),
@ -74,23 +78,26 @@ class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidge
); );
} }
Widget _buildNavItem(BuildContext context, String title, int index) { Widget _buildNavItem(BuildContext context, String title, int index, {bool enabled = true}) {
final bool isActive = index == selectedIndex; final bool isActive = index == selectedIndex;
return InkWell( return InkWell(
onTap: () => onTabChange(index), onTap: enabled ? () => onTabChange(index) : null,
child: Container( child: Opacity(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), opacity: enabled ? 1.0 : 0.5,
decoration: BoxDecoration( child: Container(
color: isActive ? const Color(0xFF9CC5C0) : Colors.transparent, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
borderRadius: BorderRadius.circular(20), decoration: BoxDecoration(
border: isActive ? null : Border.all(color: Colors.black26), color: isActive ? const Color(0xFF9CC5C0) : Colors.transparent,
), borderRadius: BorderRadius.circular(20),
child: Text( border: isActive ? null : Border.all(color: Colors.black26),
title, ),
style: TextStyle( child: Text(
color: isActive ? Colors.white : Colors.black, title,
fontWeight: isActive ? FontWeight.w600 : FontWeight.normal, style: TextStyle(
fontSize: 14, color: isActive ? Colors.white : Colors.black,
fontWeight: isActive ? FontWeight.w600 : FontWeight.normal,
fontSize: 14,
),
), ),
), ),
), ),
@ -109,9 +116,10 @@ class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidge
child: const Text('Annuler'), child: const Text('Annuler'),
), ),
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () async {
Navigator.pop(context); Navigator.pop(context);
// TODO: Implémenter la logique de déconnexion await AuthService.logout();
if (context.mounted) context.go('/login');
}, },
child: const Text('Déconnecter'), child: const Text('Déconnecter'),
), ),
@ -121,7 +129,7 @@ class DashboardAppBarAdmin extends StatelessWidget implements PreferredSizeWidge
} }
} }
/// Sous-barre affichée quand "Gestion des utilisateurs" est actif : 4 onglets sans infos utilisateur. /// Sous-barre : Gestionnaires | Parents | Assistantes maternelles | Administrateurs.
class DashboardUserManagementSubBar extends StatelessWidget { class DashboardUserManagementSubBar extends StatelessWidget {
final int selectedSubIndex; final int selectedSubIndex;
final ValueChanged<int> onSubTabChange; final ValueChanged<int> onSubTabChange;

View File

@ -1,9 +1,13 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:p_tits_pas/services/configuration_service.dart'; import 'package:p_tits_pas/services/configuration_service.dart';
/// Panneau Paramètres / Configuration (ticket #15) : 3 sections sur une page. /// Panneau Paramètres admin : Email (SMTP), Personnalisation, Avancé.
class ParametresPanel extends StatefulWidget { class ParametresPanel extends StatefulWidget {
const ParametresPanel({super.key}); /// Si true, après sauvegarde on redirige vers le login (première config). Sinon on reste sur la page.
final bool redirectToLoginAfterSave;
const ParametresPanel({super.key, this.redirectToLoginAfterSave = false});
@override @override
State<ParametresPanel> createState() => _ParametresPanelState(); State<ParametresPanel> createState() => _ParametresPanelState();
@ -104,7 +108,14 @@ class _ParametresPanelState extends State<ParametresPanel> {
return payload; return payload;
} }
/// Sauvegarde en base sans completeSetup (utilisé avant test SMTP).
Future<void> _saveBulkOnly() async {
await ConfigurationService.updateBulk(_buildPayload());
}
/// Sauvegarde la config, marque le setup comme terminé. Si première config, redirige vers le login.
Future<void> _save() async { Future<void> _save() async {
final redirectAfter = widget.redirectToLoginAfterSave;
setState(() { setState(() {
_message = null; _message = null;
_isSaving = true; _isSaving = true;
@ -112,10 +123,16 @@ class _ParametresPanelState extends State<ParametresPanel> {
try { try {
await ConfigurationService.updateBulk(_buildPayload()); await ConfigurationService.updateBulk(_buildPayload());
if (!mounted) return; if (!mounted) return;
await ConfigurationService.completeSetup();
if (!mounted) return;
setState(() { setState(() {
_isSaving = false; _isSaving = false;
_message = 'Configuration enregistrée.'; _message = 'Configuration enregistrée.';
}); });
if (!mounted) return;
if (redirectAfter) {
GoRouter.of(context).go('/login');
}
} catch (e) { } catch (e) {
if (mounted) { if (mounted) {
setState(() { setState(() {
@ -160,7 +177,7 @@ class _ParametresPanelState extends State<ParametresPanel> {
if (email == null || !mounted) return; if (email == null || !mounted) return;
setState(() => _message = null); setState(() => _message = null);
try { try {
await _save(); await _saveBulkOnly();
if (!mounted) return; if (!mounted) return;
final msg = await ConfigurationService.testSmtp(email); final msg = await ConfigurationService.testSmtp(email);
if (!mounted) return; if (!mounted) return;