79 lines
2.4 KiB
Dart
79 lines
2.4 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../../core/api/api_client.dart';
|
|
import '../../../core/api/api_endpoints.dart';
|
|
import '../../../core/api/api_response.dart';
|
|
import '../../../core/auth/auth_provider.dart';
|
|
import '../domain/payment.dart';
|
|
import '../domain/wallet.dart';
|
|
|
|
/// Dépôt de données pour le portefeuille électronique.
|
|
class WalletRepository {
|
|
WalletRepository(this._apiClient);
|
|
|
|
final ApiClient _apiClient;
|
|
|
|
Future<Wallet> fetchWallet() async {
|
|
final response = await _apiClient.get(ApiEndpoints.wallet);
|
|
final json = ApiResponse.object(response.data, 'wallet');
|
|
return Wallet.fromJson(json);
|
|
}
|
|
|
|
Future<List<WalletTransaction>> fetchTransactions() async {
|
|
final response = await _apiClient.get(ApiEndpoints.walletTransactions);
|
|
final list = ApiResponse.list(response.data, 'transactions');
|
|
|
|
return list
|
|
.map((json) => WalletTransaction.fromJson(json as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
|
|
Future<PaymentTransaction> initiateTopUp({
|
|
required double amount,
|
|
required String idempotencyKey,
|
|
}) async {
|
|
final response = await _apiClient.post(
|
|
ApiEndpoints.walletTopUpInitiate,
|
|
data: {
|
|
'amount': amount,
|
|
'idempotency_key': idempotencyKey,
|
|
},
|
|
);
|
|
|
|
final json = ApiResponse.object(response.data, 'payment');
|
|
return PaymentTransaction.fromJson(json);
|
|
}
|
|
|
|
Future<TopUpResult> confirmTopUp({required String paymentUuid}) async {
|
|
final response = await _apiClient.post(
|
|
ApiEndpoints.walletTopUpConfirm,
|
|
data: {'payment_uuid': paymentUuid},
|
|
);
|
|
|
|
return TopUpResult.fromJson(ApiResponse.payload(response.data));
|
|
}
|
|
}
|
|
|
|
final walletRepositoryProvider = Provider<WalletRepository>((ref) {
|
|
return WalletRepository(ref.watch(apiClientProvider));
|
|
});
|
|
|
|
final walletProvider = FutureProvider<Wallet>((ref) async {
|
|
final token = ref.watch(authProvider.select((state) => state.accessToken));
|
|
if (token == null || token.isEmpty) {
|
|
throw StateError('Non authentifié');
|
|
}
|
|
|
|
return ref.watch(walletRepositoryProvider).fetchWallet();
|
|
});
|
|
|
|
final walletTransactionsProvider =
|
|
FutureProvider<List<WalletTransaction>>((ref) async {
|
|
final token = ref.watch(authProvider.select((state) => state.accessToken));
|
|
if (token == null || token.isEmpty) {
|
|
throw StateError('Non authentifié');
|
|
}
|
|
|
|
return ref.watch(walletRepositoryProvider).fetchTransactions();
|
|
});
|