Restore Random_Increase helper
This commit is contained in:
@@ -238,9 +238,12 @@ export default class DBAdapter {
|
||||
'SELECT quantity FROM products WHERE id = ?', [productId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!product || product.quantity < quantity){
|
||||
return Promise.reject();
|
||||
const productRow = product?.[0];
|
||||
if (!productRow || productRow.quantity < quantity){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Insufficient product quantity")
|
||||
});
|
||||
}
|
||||
|
||||
const result = await this.#dbClient.query(
|
||||
@@ -249,7 +252,7 @@ export default class DBAdapter {
|
||||
);
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = ?',
|
||||
[quantity, productId]
|
||||
);
|
||||
|
||||
@@ -267,37 +270,44 @@ export default class DBAdapter {
|
||||
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!item){
|
||||
return Promise.reject();
|
||||
const itemRow = item?.[0];
|
||||
if (!itemRow){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Order item not found")
|
||||
});
|
||||
}
|
||||
|
||||
const diff = quantity - item.quantity;
|
||||
const diff = quantity - itemRow.quantity;
|
||||
|
||||
if (diff > 0){
|
||||
const product = await this.#dbClient.query(
|
||||
'SELECT quantity FROM products WHERE id = ?', [item.productId]
|
||||
'SELECT quantity FROM products WHERE id = ?', [itemRow.product_id]
|
||||
);
|
||||
|
||||
if (product.quantity < diff){
|
||||
return Promise.reject();
|
||||
const productRow = product?.[0];
|
||||
if (!productRow || productRow.quantity < diff){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Insufficient product quantity")
|
||||
});
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
||||
[diff, item.productId]
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = ?',
|
||||
[diff, itemRow.product_id]
|
||||
);
|
||||
}
|
||||
|
||||
if (diff < 0) {
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity - ? WHERE id = >',
|
||||
[-diff, item.productId]
|
||||
'UPDATE products SET quantity = quantity + ? WHERE id = ?',
|
||||
[-diff, itemRow.product_id]
|
||||
);
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE order_items SET quantity = ? WHERE id = >',
|
||||
'UPDATE order_items SET quantity = ? WHERE id = ?',
|
||||
[quantity, itemId]
|
||||
);
|
||||
|
||||
@@ -315,14 +325,17 @@ export default class DBAdapter {
|
||||
'SELECT product_id, quantity FROM order_items WHERE id = ?', [itemId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!item){
|
||||
return Promise.reject();
|
||||
const itemRow = item?.[0];
|
||||
if (!itemRow){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Order item not found")
|
||||
});
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE products SET quantity = quantity + ? WHERE id = >',
|
||||
[item.quantity, item.productId]
|
||||
'UPDATE products SET quantity = quantity + ? WHERE id = ?',
|
||||
[itemRow.quantity, itemRow.product_id]
|
||||
);
|
||||
|
||||
await this.#dbClient.query(
|
||||
@@ -344,13 +357,16 @@ export default class DBAdapter {
|
||||
'SELECT id, order_id FROM order_items WHERE id = ?', [itemId]
|
||||
);
|
||||
|
||||
// TODO
|
||||
if (!item){
|
||||
return Promise.reject();
|
||||
const itemRow = item?.[0];
|
||||
if (!itemRow){
|
||||
return Promise.reject({
|
||||
type: DB_USER_ERROR,
|
||||
error: new Error("Order item not found")
|
||||
});
|
||||
}
|
||||
|
||||
await this.#dbClient.query(
|
||||
'UPDATE order_items SET order_id = ? WHERE id = >',
|
||||
'UPDATE order_items SET order_id = ? WHERE id = ?',
|
||||
[targetOrderId, itemId]
|
||||
);
|
||||
|
||||
@@ -360,4 +376,4 @@ export default class DBAdapter {
|
||||
return Promise.reject();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user