fix transaction Nan

pull/1/head
Aji Kamaludin 3 years ago
parent 6fecf7eef3
commit b594e3c257
No known key found for this signature in database
GPG Key ID: 670E1F26AD5A8099

@ -28,7 +28,7 @@ class ProductsService {
const query = {
text: `SELECT
products.id, products.name, products.description, price, cost
${withStock === 'true' ? ', stock' : ''}
${withStock === 'true' ? ', stock, sale, purchase' : ''}
${withCategory === 'true' ? ', categories.name as category_name' : ''}
FROM products
${withStock === 'true' ? 'LEFT JOIN stocks ON stocks.product_id = products.id' : ''}

@ -28,7 +28,8 @@ class PurchasesService {
const purchaseId = purchase.rows[0].id;
await items.map(async (item) => {
await client.query(`UPDATE stocks SET stock = '${+item.stock + +item.quantity}', purchase = '${+item.purchase + +item.quantity}' WHERE product_id = '${item.productId}'`);
const { rows } = await client.query(`SELECT stock, purchase FROM stocks WHERE product_id = '${item.productId}'`)
await client.query(`UPDATE stocks SET stock = '${+rows[0].stock + +item.quantity}', purchase = '${+rows[0].purchase + +item.quantity}' WHERE product_id = '${item.productId}'`);
const itemQuery = {
text: `INSERT INTO purchase_items(purchase_id, product_id, quantity, cost) VALUES ('${purchaseId}', '${item.productId}', '${item.quantity}', '${item.cost}')`,

Loading…
Cancel
Save