added ledger as souec of funds for payouts
This commit is contained in:
@@ -13,7 +13,6 @@ import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/payment/wallets.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class WalletsProvider with ChangeNotifier {
|
||||
final WalletsService _service;
|
||||
OrganizationsProvider? _organizations;
|
||||
@@ -31,11 +30,13 @@ class WalletsProvider with ChangeNotifier {
|
||||
bool get isRefreshingBalances => _isRefreshingBalances;
|
||||
|
||||
final Set<String> _refreshingWallets = <String>{};
|
||||
bool isWalletRefreshing(String walletRef) => _refreshingWallets.contains(walletRef);
|
||||
bool isWalletRefreshing(String walletRef) =>
|
||||
_refreshingWallets.contains(walletRef);
|
||||
|
||||
// Expose current org id so UI controller can reset per-org state if needed.
|
||||
String? get organizationRef =>
|
||||
(_organizations?.isOrganizationSet ?? false) ? _organizations!.current.id : null;
|
||||
String? get organizationRef => (_organizations?.isOrganizationSet ?? false)
|
||||
? _organizations!.current.id
|
||||
: null;
|
||||
|
||||
// Used to ignore stale async results (org changes / overlapping requests).
|
||||
int _opSeq = 0;
|
||||
@@ -67,13 +68,25 @@ class WalletsProvider with ChangeNotifier {
|
||||
_isRefreshingBalances = false;
|
||||
_refreshingWallets.clear();
|
||||
|
||||
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: true, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
try {
|
||||
final base = await _service.getWallets(orgId);
|
||||
if (seq != _opSeq) return;
|
||||
|
||||
// Publish wallets as soon as the list is available, then hydrate balances.
|
||||
_isRefreshingBalances = true;
|
||||
_applyResource(
|
||||
Resource<List<Wallet>>(data: base, isLoading: false, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
final result = await _withBalances(orgId, base);
|
||||
if (seq != _opSeq) return;
|
||||
_isRefreshingBalances = false;
|
||||
|
||||
_applyResource(
|
||||
Resource<List<Wallet>>(
|
||||
@@ -85,6 +98,7 @@ class WalletsProvider with ChangeNotifier {
|
||||
);
|
||||
} catch (e) {
|
||||
if (seq != _opSeq) return;
|
||||
_isRefreshingBalances = false;
|
||||
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: false, error: toException(e)),
|
||||
@@ -145,7 +159,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
final balance = await _service.getBalance(orgId, walletRef);
|
||||
if ((_walletSeq[walletRef] ?? 0) != seq) return;
|
||||
|
||||
final next = _replaceWallet(walletRef, (w) => w.copyWith(balance: balance));
|
||||
final next = _replaceWallet(
|
||||
walletRef,
|
||||
(w) => w.copyWith(balance: balance),
|
||||
);
|
||||
if (next == null) return;
|
||||
|
||||
_applyResource(_resource.copyWith(data: next), notify: false);
|
||||
@@ -169,7 +186,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
final org = _organizations;
|
||||
if (org == null || !org.isOrganizationSet) return;
|
||||
|
||||
_applyResource(_resource.copyWith(isLoading: true, error: null), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: true, error: null),
|
||||
notify: true,
|
||||
);
|
||||
|
||||
try {
|
||||
await _service.create(
|
||||
@@ -180,19 +200,28 @@ class WalletsProvider with ChangeNotifier {
|
||||
);
|
||||
await loadWalletsWithBalances();
|
||||
} catch (e) {
|
||||
_applyResource(_resource.copyWith(isLoading: false, error: toException(e)), notify: true);
|
||||
_applyResource(
|
||||
_resource.copyWith(isLoading: false, error: toException(e)),
|
||||
notify: true,
|
||||
);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- internals ----------
|
||||
|
||||
void _applyResource(Resource<List<Wallet>> newResource, {required bool notify}) {
|
||||
void _applyResource(
|
||||
Resource<List<Wallet>> newResource, {
|
||||
required bool notify,
|
||||
}) {
|
||||
_resource = newResource;
|
||||
if (notify) notifyListeners();
|
||||
}
|
||||
|
||||
List<Wallet>? _replaceWallet(String walletRef, Wallet Function(Wallet) updater) {
|
||||
List<Wallet>? _replaceWallet(
|
||||
String walletRef,
|
||||
Wallet Function(Wallet) updater,
|
||||
) {
|
||||
final idx = wallets.indexWhere((w) => w.id == walletRef);
|
||||
if (idx < 0) return null;
|
||||
|
||||
@@ -201,7 +230,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
return next;
|
||||
}
|
||||
|
||||
Future<_WalletLoadResult> _withBalances(String orgRef, List<Wallet> base) async {
|
||||
Future<_WalletLoadResult> _withBalances(
|
||||
String orgRef,
|
||||
List<Wallet> base,
|
||||
) async {
|
||||
Exception? firstError;
|
||||
|
||||
final withBalances = await _mapConcurrent<Wallet, Wallet>(
|
||||
@@ -239,7 +271,10 @@ class WalletsProvider with ChangeNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
final workers = List.generate(min(concurrency, items.length), (_) => worker());
|
||||
final workers = List.generate(
|
||||
min(concurrency, items.length),
|
||||
(_) => worker(),
|
||||
);
|
||||
await Future.wait(workers);
|
||||
|
||||
return results.cast<R>();
|
||||
|
||||
Reference in New Issue
Block a user