Initial commit: app Fideliter client (Flutter)
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../models/client.dart';
|
||||
import '../models/mouvement.dart';
|
||||
import '../models/recompense.dart';
|
||||
import '../supabase_config.dart';
|
||||
|
||||
/// Données du client connecté (son compte, ses points, son historique) + le
|
||||
/// contexte magasin (nom, récompenses proposées). Source de vérité côté client.
|
||||
class SessionClient extends ChangeNotifier {
|
||||
SessionClient._();
|
||||
static final SessionClient instance = SessionClient._();
|
||||
|
||||
Client? _monClient;
|
||||
List<Mouvement> _mouvements = [];
|
||||
List<Recompense> _recompenses = [];
|
||||
String _nomMagasin = '';
|
||||
double _eurosParPoint = 0;
|
||||
bool _enCours = false;
|
||||
String? _erreur;
|
||||
|
||||
Client? get monClient => _monClient;
|
||||
List<Mouvement> get mouvements => List.unmodifiable(_mouvements);
|
||||
List<Recompense> get recompenses => List.unmodifiable(_recompenses);
|
||||
String get nomMagasin => _nomMagasin;
|
||||
double get eurosParPoint => _eurosParPoint;
|
||||
bool get enCours => _enCours;
|
||||
String? get erreur => _erreur;
|
||||
|
||||
/// Charge toutes les données du client connecté. [_monClient] reste null si
|
||||
/// le compte n'a pas encore de profil fidélité (→ écran « finaliser profil »).
|
||||
Future<void> charger() async {
|
||||
_enCours = true;
|
||||
_erreur = null;
|
||||
notifyListeners();
|
||||
try {
|
||||
final uid = supabase.auth.currentUser?.id;
|
||||
if (uid == null) {
|
||||
_monClient = null;
|
||||
return;
|
||||
}
|
||||
|
||||
final row = await supabase
|
||||
.from('clients')
|
||||
.select()
|
||||
.eq('user_id', uid)
|
||||
.maybeSingle();
|
||||
_monClient = row == null ? null : Client.fromMap(row);
|
||||
|
||||
// Contexte magasin (lisible par tout compte connecté).
|
||||
final reglages = await supabase
|
||||
.from('reglages')
|
||||
.select('euros_par_point, nom_magasin')
|
||||
.eq('id', 1)
|
||||
.maybeSingle();
|
||||
if (reglages != null) {
|
||||
_eurosParPoint =
|
||||
(reglages['euros_par_point'] as num?)?.toDouble() ?? 0;
|
||||
_nomMagasin = (reglages['nom_magasin'] as String?) ?? '';
|
||||
}
|
||||
|
||||
final recs = await supabase
|
||||
.from('recompenses')
|
||||
.select()
|
||||
.eq('actif', true)
|
||||
.order('cout_points');
|
||||
_recompenses =
|
||||
(recs as List).map((e) => Recompense.fromMap(e)).toList();
|
||||
|
||||
if (_monClient != null) {
|
||||
final mvts = await supabase
|
||||
.from('mouvements')
|
||||
.select()
|
||||
.eq('client_id', _monClient!.id)
|
||||
.order('created_at', ascending: false);
|
||||
_mouvements =
|
||||
(mvts as List).map((e) => Mouvement.fromMap(e)).toList();
|
||||
} else {
|
||||
_mouvements = [];
|
||||
}
|
||||
} catch (e) {
|
||||
_erreur = e.toString();
|
||||
} finally {
|
||||
_enCours = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
/// Crée le profil fidélité du compte connecté (nom + téléphone). Le code et le
|
||||
/// QR sont générés côté base. Utilisé à l'inscription / finalisation de profil.
|
||||
Future<void> creerProfil({
|
||||
required String nom,
|
||||
String? prenom,
|
||||
String? telephone,
|
||||
}) async {
|
||||
final uid = supabase.auth.currentUser?.id;
|
||||
if (uid == null) throw Exception('Non connecté');
|
||||
String? ouNull(String? v) =>
|
||||
(v == null || v.trim().isEmpty) ? null : v.trim();
|
||||
await supabase.from('clients').insert({
|
||||
'user_id': uid,
|
||||
'nom': nom.trim(),
|
||||
'prenom': ouNull(prenom),
|
||||
'telephone': ouNull(telephone),
|
||||
});
|
||||
await charger();
|
||||
}
|
||||
|
||||
void vider() {
|
||||
_monClient = null;
|
||||
_mouvements = [];
|
||||
_recompenses = [];
|
||||
_nomMagasin = '';
|
||||
_eurosParPoint = 0;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user