feat: Implement BalanceRepository for group balance calculations feat: Create ExpenseRepository for managing expenses feat: Add services for handling expenses and storage operations fix: Update import paths for models in repositories and services refactor: Rename CountContent to AccountContent in HomePage chore: Add StorageService for image upload and management
146 lines
4.8 KiB
Dart
146 lines
4.8 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:travel_mate/services/error_service.dart';
|
|
import '../models/group_member.dart';
|
|
import '../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> updateAccount(String accountId, Account account) async {
|
|
try {
|
|
await _firestore.collection('accounts').doc(accountId).update(account.toMap());
|
|
} catch (e) {
|
|
_errorService.logError('account_repository.dart', 'Erreur lors de la mise à jour du compte: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> deleteAccount(String tripId) async {
|
|
try {
|
|
final querySnapshot = await _firestore
|
|
.collection('accounts')
|
|
.where('tripId', isEqualTo: tripId)
|
|
.get();
|
|
if (querySnapshot.docs.isEmpty) {
|
|
throw Exception('Aucun compte trouvé pour ce voyage');
|
|
}
|
|
|
|
final docId = querySnapshot.docs.first.id;
|
|
|
|
final membersSnapshot = await _membersCollection(docId).get();
|
|
for (var memberDoc in membersSnapshot.docs) {
|
|
await _membersCollection(docId).doc(memberDoc.id).delete();
|
|
}
|
|
await _accountCollection.doc(docId).delete();
|
|
} catch (e) {
|
|
_errorService.logError('account_repository.dart', 'Erreur lors de la suppression du compte: $e');
|
|
}
|
|
}
|
|
|
|
Stream<List<GroupMember>> watchGroupMembers(String accountId) {
|
|
return _membersCollection(accountId).snapshots().map(
|
|
(snapshot) => snapshot.docs
|
|
.map((doc) => GroupMember.fromMap(
|
|
doc.data() as Map<String, dynamic>,
|
|
doc.id,
|
|
))
|
|
.toList(),
|
|
);
|
|
}
|
|
} |