Skip to content

Commit

Permalink
Allow price to be updated from POS
Browse files Browse the repository at this point in the history
Signed-off-by: Diogo Correia <[email protected]>
  • Loading branch information
diogotcorreia committed Mar 19, 2021
1 parent fac0974 commit 36cd3ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
8 changes: 5 additions & 3 deletions backend/api/product/controllers/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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,
});
}
Expand Down
25 changes: 16 additions & 9 deletions backend/api/product/services/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit 36cd3ec

Please sign in to comment.