From 36cd3ec666f84fe95a08709af748f8b6a1f761dc Mon Sep 17 00:00:00 2001 From: Diogo Correia Date: Fri, 19 Mar 2021 16:50:47 +0000 Subject: [PATCH] Allow price to be updated from POS Signed-off-by: Diogo Correia --- backend/api/product/controllers/Product.js | 8 ++++--- backend/api/product/services/Product.js | 25 ++++++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/backend/api/product/controllers/Product.js b/backend/api/product/controllers/Product.js index f183e3d..712b6b2 100644 --- a/backend/api/product/controllers/Product.js +++ b/backend/api/product/controllers/Product.js @@ -7,7 +7,8 @@ const updateStocksSchema = Joi.array() .items( Joi.object({ ref: Joi.string().pattern(/^\d+$/).required(), - qnt: Joi.number().integer().min(0).required(), + qnt: Joi.number().integer().min(0), + price: Joi.number().min(0), }).required() ) .unique('ref') @@ -136,11 +137,12 @@ module.exports = { const product = await strapi.services.product.findOne({ reference: data.reference }); if (product) { - const { qnt } = await strapi.services.product.updateStock({ + const { qnt, price } = await strapi.services.product.updateStock({ ref: product.reference, qnt: data.quantity, + price: data.price, }); - return sanitizeEntity(addStockStatus({ ...product, quantity: qnt }), { + return sanitizeEntity(addStockStatus({ ...product, quantity: qnt, price }), { model: strapi.models.product, }); } diff --git a/backend/api/product/services/Product.js b/backend/api/product/services/Product.js index 8b5e4c0..55e49e0 100644 --- a/backend/api/product/services/Product.js +++ b/backend/api/product/services/Product.js @@ -101,26 +101,33 @@ module.exports = { if (res.nModified !== 1) throw new Error(`Couldn't decrease stock for ${id}.`); }, - updateStock: async ({ ref, qnt }) => { + updateStock: async ({ ref, qnt, price: cost }) => { try { - const { _id, quantity, lastQuantity } = await strapi + const { _id, quantity /*, lastQuantity*/, price } = await strapi .query('product') - .model.findOne({ reference: ref }, ['_id', 'quantity', 'lastQuantity']); + .model.findOne({ reference: ref }, ['_id', 'quantity', 'lastQuantity', 'price']); - const stockDelta = quantity - lastQuantity + (qnt - lastQuantity); - let newStock = lastQuantity + stockDelta; + // I don't think the client actually updates the stock, so I'm going to stop using that value + + // const stockDelta = quantity - lastQuantity + (qnt - lastQuantity); + // let newStock = lastQuantity + stockDelta; + let newStock = qnt === undefined ? quantity : qnt; if (newStock < 0) newStock = 0; + const newPrice = cost === undefined ? price : cost; + const res = await strapi .query('product') - .model.updateOne({ _id }, { quantity: newStock, lastQuantity: newStock }); + .model.updateOne({ _id }, { quantity: newStock, lastQuantity: newStock, price: newPrice }); if (res.nModified !== 1) { - strapi.log.error(`Couldn't update stock for ${ref} to ${qnt}.`); - return { ref, qnt }; + strapi.log.error( + `Couldn't update stock/price for ${ref} to quantity ${qnt} or price ${cost}.` + ); + return { ref, qnt, price: cost }; } - return { ref, qnt: newStock }; + return { ref, qnt: newStock, price: newPrice }; } catch { // Most likely product not found return { ref, qnt };