Files
sendico/frontend/pshared/lib/provider/organizations.dart
Stephan D c6a56071b5
Some checks failed
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
+signup +login
2025-11-17 20:16:45 +01:00

81 lines
2.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
import 'package:pshared/config/constants.dart';
import 'package:pshared/models/organization/organization.dart';
import 'package:pshared/provider/resource.dart';
import 'package:pshared/service/organization.dart';
import 'package:pshared/service/secure_storage.dart';
import 'package:pshared/utils/exception.dart';
class OrganizationsProvider extends ChangeNotifier {
Resource<List<Organization>> _resource = Resource(data: []);
Resource<List<Organization>> get resource => _resource;
List<Organization> get organizations => _resource.data ?? [];
String? _currentOrg;
Organization get current => isOrganizationSet ? _current! : throw StateError('Organization is not set');
Organization? _org(String? orgRef) => organizations.firstWhereOrNull((org) => org.id == orgRef);
Organization? get _current => _org(_currentOrg);
bool get isOrganizationSet => _current != null;
bool get isLoading => _resource.isLoading;
Object? get error => _resource.error;
void _setResource(Resource<List<Organization>> newResource) {
_resource = newResource;
notifyListeners();
}
Future<List<Organization>> load() async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final orgs = await OrganizationService.list();
// fetch stored org
String? org = await SecureStorageService.get(Constants.currentOrgKey);
// check stored org availability
org = orgs.firstWhereOrNull((o) => o.id == org)?.id;
// fallback if org is not set or not available
org ??= orgs.first.id;
await setCurrentOrganization(org);
_setResource(Resource(data: orgs, isLoading: false));
return orgs;
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
Future<Organization> loadByInvitation(String invitationRef) async {
_setResource(_resource.copyWith(isLoading: true, error: null));
try {
final org = await OrganizationService.loadByInvitation(invitationRef);
await setCurrentOrganization(org.id);
_setResource(Resource(data: [org], isLoading: false));
return org;
} catch (e) {
_setResource(_resource.copyWith(isLoading: false, error: toException(e)));
rethrow;
}
}
bool _setCurrentOrganization(String? orgRef) {
final organizationRef = _org(orgRef)?.id;
if (organizationRef == null) return false;
_currentOrg = organizationRef;
return true;
}
Future<bool> setCurrentOrganization(String? orgRef) async {
if (!_setCurrentOrganization(orgRef)) return false;
await SecureStorageService.set(Constants.currentOrgKey, orgRef);
notifyListeners();
return true;
}
}