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
+35 -18
View File
@@ -130,31 +130,48 @@ class DriftProductService implements ProductService {
@override
Future<void> decreaseStock(String productId, int amount) async {
final product = await getProductById(productId);
_validateStockAmount(amount);
if (product == null) {
throw Exception('Product not found');
}
if (product.stockQuantity < amount) {
throw const InsufficientStockException();
}
await updateProduct(
product.copyWith(stockQuantity: product.stockQuantity - amount),
final updatedRows = await database.customUpdate(
'UPDATE products '
'SET stock_quantity = stock_quantity - ? '
'WHERE id = ? AND stock_quantity >= ?',
variables: [
Variable.withInt(amount),
Variable.withString(productId),
Variable.withInt(amount),
],
updates: {database.products},
);
if (updatedRows == 1) return;
final product = await getProductById(productId);
if (product == null) throw Exception('Product not found');
throw const InsufficientStockException();
}
@override
Future<void> increaseStock(String productId, int amount) async {
final product = await getProductById(productId);
_validateStockAmount(amount);
if (product == null) {
throw Exception('Product not found');
}
await updateProduct(
product.copyWith(stockQuantity: product.stockQuantity + amount),
final updatedRows = await database.customUpdate(
'UPDATE products '
'SET stock_quantity = stock_quantity + ? '
'WHERE id = ?',
variables: [Variable.withInt(amount), Variable.withString(productId)],
updates: {database.products},
);
if (updatedRows == 1) return;
throw Exception('Product not found');
}
void _validateStockAmount(int amount) {
if (amount <= 0) {
throw ArgumentError.value(amount, 'amount', 'Must be greater than zero');
}
}
}