Fix button logic

This commit is contained in:
2026-01-03 20:41:15 +03:00
parent 75df8970fd
commit 23c67c9e6e
3 changed files with 101 additions and 21 deletions

View File

@@ -384,10 +384,17 @@ export default class DBAdapter {
}
}
async updateOrderItem({ itemId, quantity }) {
async updateOrderItem({ itemId, quantity, productId }) {
let connection;
try {
if (!itemId || !Number.isFinite(quantity) || quantity <= 0) {
return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Invalid item or quantity")
});
}
connection = await this.#pool.getConnection();
await connection.beginTransaction();
@@ -406,6 +413,51 @@ export default class DBAdapter {
});
}
const targetProductId = productId ?? itemRow.product_id;
if (targetProductId !== itemRow.product_id) {
const newProduct = await connection.query(
'SELECT quantity FROM products WHERE id = ? FOR UPDATE',
[targetProductId]
);
const newProductRow = newProduct?.[0];
if (!newProductRow) {
await connection.rollback();
connection.release();
return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Product not found")
});
}
if (newProductRow.quantity < quantity) {
await connection.rollback();
connection.release();
return Promise.reject({
type: DB_USER_ERROR,
error: new Error("Insufficient product quantity")
});
}
await connection.query(
'UPDATE products SET quantity = quantity + ? WHERE id = ?',
[itemRow.quantity, itemRow.product_id]
);
await connection.query(
'UPDATE products SET quantity = quantity - ? WHERE id = ?',
[quantity, targetProductId]
);
await connection.query(
'UPDATE order_items SET product_id = ?, quantity = ? WHERE id = ?',
[targetProductId, quantity, itemId]
);
await connection.commit();
connection.release();
return;
}
const diff = quantity - itemRow.quantity;
if (diff > 0) {