fix: Make transactions atomic / AutoLogout

This commit is contained in:
2026-08-03 02:22:26 +02:00
parent 6060ca2713
commit 0997d110d4
14 changed files with 604 additions and 285 deletions
+14 -36
View File
@@ -95,16 +95,9 @@ class BarScreenViewModel extends ChangeNotifier {
if (tab == null) return;
if (product.stockQuantity <= 0) {
throw const InsufficientStockException();
}
try {
await barTabService.addProductToTab(
tabId: tab.id,
product: product,
stockAdjustment: () => inventory.decreaseStock(product.id, 1),
);
await barTabService.addProductToTab(tabId: tab.id, product: product);
inventory.applyStockDelta(product.id, -1);
await _reloadTabs();
} catch (e, stack) {
@@ -118,35 +111,15 @@ class BarScreenViewModel extends ChangeNotifier {
}
}
Future<void> changeItemQuantity(TabItem item, int quantity) async {
final difference = quantity - item.quantity;
if (difference > 0) {
Product? product;
for (final candidate in inventory.products) {
if (candidate.id == item.productId) {
product = candidate;
break;
}
}
if (product != null && product.stockQuantity < difference) {
throw const InsufficientStockException();
}
}
Future<void> changeItemQuantity(TabItem item, int delta) async {
if (delta == 0) return;
try {
await barTabService.updateTabItemQuantity(
final actualDelta = await barTabService.adjustTabItemQuantity(
tabItemId: item.id,
quantity: quantity,
stockAdjustment: () async {
if (difference > 0) {
await inventory.decreaseStock(item.productId, difference);
} else if (difference < 0) {
await inventory.increaseStock(item.productId, -difference);
}
},
delta: delta,
);
inventory.applyStockDelta(item.productId, -actualDelta);
await _reloadTabs();
} catch (e, stack) {
@@ -160,7 +133,9 @@ class BarScreenViewModel extends ChangeNotifier {
}
}
Future<void> closeSelectedTab({PaymentMethod paymentMethod = PaymentMethod.cash}) async {
Future<void> closeSelectedTab({
PaymentMethod paymentMethod = PaymentMethod.cash,
}) async {
final tab = selectedTab;
if (tab == null) return;
@@ -177,7 +152,10 @@ class BarScreenViewModel extends ChangeNotifier {
}
}
Future<void> closeTab(String tabId, {PaymentMethod paymentMethod = PaymentMethod.cash}) async {
Future<void> closeTab(
String tabId, {
PaymentMethod paymentMethod = PaymentMethod.cash,
}) async {
try {
await barTabService.closeTab(tabId, paymentMethod: paymentMethod);
await _reloadTabs();
+12
View File
@@ -65,4 +65,16 @@ class InventoryViewModel extends ChangeNotifier {
rethrow;
}
}
void applyStockDelta(String productId, int delta) {
if (delta == 0) return;
final index = _products.indexWhere((product) => product.id == productId);
if (index == -1) return;
_products[index] = _products[index].copyWith(
stockQuantity: _products[index].stockQuantity + delta,
);
notifyListeners();
}
}