temp build
This commit is contained in:
59
frontend/pshared/lib/provider/recipient/pmethods.dart
Normal file
59
frontend/pshared/lib/provider/recipient/pmethods.dart
Normal file
@@ -0,0 +1,59 @@
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'package:pshared/data/mapper/payment/method.dart';
|
||||
import 'package:pshared/models/payment/methods/type.dart';
|
||||
import 'package:pshared/provider/organizations.dart';
|
||||
import 'package:pshared/provider/recipient/provider.dart';
|
||||
import 'package:pshared/provider/template.dart';
|
||||
import 'package:pshared/service/recipient/pmethods.dart';
|
||||
|
||||
|
||||
class PaymentMethodsProvider extends GenericProvider<PaymentMethod> {
|
||||
late OrganizationsProvider _organizations;
|
||||
late RecipientsProvider _recipients;
|
||||
|
||||
PaymentMethodsProvider() : super(service: PaymentMethodService.basicService);
|
||||
|
||||
List<PaymentMethod> get methods => List<PaymentMethod>.unmodifiable(items.toList()..sort((a, b) => a.storable.createdAt.compareTo(b.storable.createdAt)));
|
||||
|
||||
void updateProviders(OrganizationsProvider organizations, RecipientsProvider recipients) {
|
||||
_organizations = organizations;
|
||||
_recipients = recipients;
|
||||
if (_organizations.isOrganizationSet && (_recipients.currentObject != null)) {
|
||||
load(_organizations.current.id, _recipients.currentObject!.id);
|
||||
}
|
||||
}
|
||||
|
||||
// void reorderMethods(int oldIndex, int newIndex) {
|
||||
// if (newIndex > oldIndex) newIndex--;
|
||||
// final item = _methods.removeAt(oldIndex);
|
||||
// _methods.insert(newIndex, item);
|
||||
// notifyListeners();
|
||||
// }
|
||||
|
||||
PaymentMethod? get main => methods.firstWhereOrNull((m) => m.isMain);
|
||||
|
||||
Future<void> updateMethod(PaymentMethod method) async => update(method.toDTO().toJson());
|
||||
|
||||
Future<void> setArchivedMethod({
|
||||
required PaymentMethod method,
|
||||
required bool newIsArchived,
|
||||
}) async => setArchived(
|
||||
organizationRef: _organizations.current.id,
|
||||
objectRef: method.id,
|
||||
newIsArchived: newIsArchived,
|
||||
cascade: true,
|
||||
);
|
||||
|
||||
|
||||
Future<void> makeMain(PaymentMethod method) {
|
||||
// TODO: create separate backend method to manage main payment method
|
||||
final updates = <Future<void>>[];
|
||||
final currentMain = main;
|
||||
if (currentMain != null) {
|
||||
updates.add(updateMethod(currentMain.copyWith(isMain: false)));
|
||||
}
|
||||
updates.add(updateMethod(method.copyWith(isMain: true)));
|
||||
return Future.wait(updates).then((_) => null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user