feat: Implement account management features
- Added ExpenseDetailDialog for displaying expense details and actions. - Created ExpensesTab to list expenses for a group. - Developed GroupExpensesPage to manage group expenses with tabs for expenses, balances, and settlements. - Introduced SettlementsTab to show optimized repayment plans. - Refactored create_trip_content.dart to remove CountBloc and related logic. - Added Account model to manage user accounts and group members. - Replaced CountRepository with AccountRepository for account-related operations. - Removed CountService and CountRepository as part of the refactor. - Updated main.dart and home.dart to integrate new account management components.
This commit is contained in:
117
lib/repositories/account_repository.dart
Normal file
117
lib/repositories/account_repository.dart
Normal file
@@ -0,0 +1,117 @@
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:travel_mate/services/error_service.dart';
|
||||
import '../data/models/group_member.dart';
|
||||
import '../data/models/account.dart';
|
||||
|
||||
class AccountRepository {
|
||||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
||||
final _errorService = ErrorService();
|
||||
|
||||
CollectionReference get _accountCollection => _firestore.collection('accounts');
|
||||
|
||||
CollectionReference _membersCollection(String accountId) {
|
||||
return _accountCollection.doc(accountId).collection('members');
|
||||
}
|
||||
|
||||
Future<String> createAccountWithMembers({
|
||||
required Account account,
|
||||
required List<GroupMember> members,
|
||||
}) async {
|
||||
try {
|
||||
return await _firestore.runTransaction<String>((transaction) async {
|
||||
final accountRef = _accountCollection.doc();
|
||||
|
||||
final accountData = account.toMap();
|
||||
|
||||
transaction.set(accountRef, accountData);
|
||||
|
||||
for (var member in members) {
|
||||
final memberRef = accountRef.collection('members').doc(member.userId);
|
||||
transaction.set(memberRef, member.toMap());
|
||||
}
|
||||
|
||||
return accountRef.id;
|
||||
});
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la création du compte: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Stream<List<Account>> getAccountByUserId(String userId) {
|
||||
return _accountCollection
|
||||
.snapshots()
|
||||
.asyncMap((snapshot) async {
|
||||
|
||||
List<Account> userAccounts = [];
|
||||
|
||||
for (var accountDoc in snapshot.docs) {
|
||||
try {
|
||||
final accountId = accountDoc.id;
|
||||
|
||||
final memberDoc = await accountDoc.reference
|
||||
.collection('members')
|
||||
.doc(userId)
|
||||
.get();
|
||||
if (memberDoc.exists) {
|
||||
final accountData = accountDoc.data() as Map<String, dynamic>;
|
||||
final account = Account.fromMap(accountData);
|
||||
final members = await getAccountMembers(accountId);
|
||||
|
||||
userAccounts.add(account.copyWith(members: members));
|
||||
} else {
|
||||
_errorService.logInfo('account_repository.dart', 'Utilisateur NON membre de $accountId');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
_errorService.logError(e.toString(), stackTrace);
|
||||
}
|
||||
}
|
||||
return userAccounts;
|
||||
})
|
||||
.distinct((prev, next) {
|
||||
if (prev.length != next.length) return false;
|
||||
final prevIds = prev.map((a) => a.id).toSet();
|
||||
final nextIds = next.map((a) => a.id).toSet();
|
||||
|
||||
final identical = prevIds.difference(nextIds).isEmpty &&
|
||||
nextIds.difference(prevIds).isEmpty;
|
||||
|
||||
return identical;
|
||||
})
|
||||
.handleError((error, stackTrace) {
|
||||
_errorService.logError(error, stackTrace);
|
||||
return <Account>[];
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<GroupMember>> getAccountMembers(String accountId) async {
|
||||
try {
|
||||
final snapshot = await _membersCollection(accountId).get();
|
||||
return snapshot.docs
|
||||
.map((doc) {
|
||||
return GroupMember.fromMap(
|
||||
doc.data() as Map<String, dynamic>,
|
||||
doc.id,
|
||||
);
|
||||
})
|
||||
.toList();
|
||||
} catch (e) {
|
||||
throw Exception('Erreur lors de la récupération des membres: $e');
|
||||
}
|
||||
}
|
||||
|
||||
Future<DocumentSnapshot> getAccountById(String accountId) async {
|
||||
return await _firestore.collection('accounts').doc(accountId).get();
|
||||
}
|
||||
|
||||
Future<void> createAccount(Map<String, dynamic> accountData) async {
|
||||
await _firestore.collection('accounts').add(accountData);
|
||||
}
|
||||
|
||||
Future<void> updateAccount(String accountId, Map<String, dynamic> accountData) async {
|
||||
await _firestore.collection('accounts').doc(accountId).update(accountData);
|
||||
}
|
||||
|
||||
Future<void> deleteAccount(String accountId) async {
|
||||
await _firestore.collection('accounts').doc(accountId).delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user