feat: Refactor account handling and improve group creation logic
This commit is contained in:
@@ -54,9 +54,8 @@ class AccountRepository {
|
||||
.get();
|
||||
if (memberDoc.exists) {
|
||||
final accountData = accountDoc.data() as Map<String, dynamic>;
|
||||
final account = Account.fromMap(accountData);
|
||||
final account = Account.fromMap(accountData, accountId); // ✅ Ajout de l'ID
|
||||
final members = await getAccountMembers(accountId);
|
||||
|
||||
userAccounts.add(account.copyWith(members: members));
|
||||
} else {
|
||||
_errorService.logInfo('account_repository.dart', 'Utilisateur NON membre de $accountId');
|
||||
@@ -99,15 +98,44 @@ class AccountRepository {
|
||||
}
|
||||
}
|
||||
|
||||
Future<DocumentSnapshot> getAccountById(String accountId) async {
|
||||
return await _firestore.collection('accounts').doc(accountId).get();
|
||||
Future<Account?> getAccountById(String accountId) async {
|
||||
try {
|
||||
final doc = await _firestore.collection('accounts').doc(accountId).get();
|
||||
if (doc.exists) {
|
||||
return Account.fromMap(doc.data() as Map<String, dynamic>, doc.id);
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération du compte: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Account?> getAccountByTripId(String tripId) async {
|
||||
try {
|
||||
final querySnapshot = await _firestore
|
||||
.collection('accounts')
|
||||
.where('tripId', isEqualTo: tripId)
|
||||
.limit(1)
|
||||
.get();
|
||||
|
||||
if (querySnapshot.docs.isNotEmpty) {
|
||||
final doc = querySnapshot.docs.first;
|
||||
return Account.fromMap(doc.data(), doc.id);
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération du compte: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateAccount(String accountId, Account account) async {
|
||||
try {
|
||||
await _firestore.collection('accounts').doc(accountId).update(account.toMap());
|
||||
// Mettre à jour la date de modification
|
||||
final updatedAccount = account.copyWith(updatedAt: DateTime.now());
|
||||
await _firestore.collection('accounts').doc(accountId).update(updatedAccount.toMap());
|
||||
} catch (e) {
|
||||
_errorService.logError('account_repository.dart', 'Erreur lors de la mise à jour du compte: $e');
|
||||
throw Exception('Erreur lors de la mise à jour du compte: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,13 +151,17 @@ class AccountRepository {
|
||||
|
||||
final docId = querySnapshot.docs.first.id;
|
||||
|
||||
// Supprimer tous les membres
|
||||
final membersSnapshot = await _membersCollection(docId).get();
|
||||
for (var memberDoc in membersSnapshot.docs) {
|
||||
await _membersCollection(docId).doc(memberDoc.id).delete();
|
||||
}
|
||||
|
||||
// Supprimer le compte
|
||||
await _accountCollection.doc(docId).delete();
|
||||
} catch (e) {
|
||||
_errorService.logError('account_repository.dart', 'Erreur lors de la suppression du compte: $e');
|
||||
throw Exception('Erreur lors de la suppression du compte: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,4 +175,27 @@ class AccountRepository {
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Stream<Account?> watchAccount(String accountId) {
|
||||
return _accountCollection.doc(accountId).snapshots().map((doc) {
|
||||
if (doc.exists) {
|
||||
return Account.fromMap(doc.data() as Map<String, dynamic>, doc.id);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
Stream<Account?> watchAccountByTripId(String tripId) {
|
||||
return _accountCollection
|
||||
.where('tripId', isEqualTo: tripId)
|
||||
.limit(1)
|
||||
.snapshots()
|
||||
.map((snapshot) {
|
||||
if (snapshot.docs.isNotEmpty) {
|
||||
final doc = snapshot.docs.first;
|
||||
return Account.fromMap(doc.data() as Map<String, dynamic>, doc.id);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user