From b594e3c257c5d0e508aba6a69d7f403725ecbf50 Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Thu, 19 Aug 2021 21:55:30 +0700 Subject: [PATCH] fix transaction Nan --- src/services/postgres/ProductsService.js | 2 +- src/services/postgres/PurchasesService.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/services/postgres/ProductsService.js b/src/services/postgres/ProductsService.js index a45b6e4..883eca2 100644 --- a/src/services/postgres/ProductsService.js +++ b/src/services/postgres/ProductsService.js @@ -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' : ''} diff --git a/src/services/postgres/PurchasesService.js b/src/services/postgres/PurchasesService.js index f0d26c5..332e3aa 100644 --- a/src/services/postgres/PurchasesService.js +++ b/src/services/postgres/PurchasesService.js @@ -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}')`,