diff --git a/models/products.js b/models/products.js index b85624e..77fb655 100644 --- a/models/products.js +++ b/models/products.js @@ -38,13 +38,13 @@ const update = async (id, name, quantity) => connection() return updateLog; }); -// const updateQtd = async (id, quantity) => connection() -// .then(async (db) => { -// const updateLog = await db.collection('products').updateOne({ _id: ObjectId(id) }, -// { $set: { quantity } }); +const updateQtd = async (id, quantity) => connection() +.then(async (db) => { + const updateLog = await db.collection('products').updateOne({ _id: ObjectId(id) }, + { $inc: { quantity } }); -// return updateLog; -// }); + return updateLog; +}); const deleteIt = async (id) => connection() .then(async (db) => { @@ -59,6 +59,6 @@ module.exports = { getAll, getById, update, - // updateQtd, + updateQtd, deleteIt, }; diff --git a/services/sales.js b/services/sales.js index 1bec5b3..a9dc859 100644 --- a/services/sales.js +++ b/services/sales.js @@ -42,6 +42,17 @@ const create = async (soldProducts) => { // CREATING: const insertedId = await models.create(soldProducts); // if come here, thas means all productId were found + +// TÔ AQUI >>>>>>>> + + // UPDATING product stock quantity: + + const result = await validators.decProductQtd(soldProducts, 'dec'); + + console.log(result); // Resultado penas para desenvolvedor. + +// TÔ AQUI >>>>>>>> + return insertedId; }; diff --git a/test/sales.test.js b/test/sales.test.js index 0ea47c6..17a3250 100644 --- a/test/sales.test.js +++ b/test/sales.test.js @@ -664,7 +664,7 @@ describe('8 - Crie um endpoint para deletar uma venda', () => { }); }); -describe.skip('9 - Atualize a quantidade de produtos', () => { +describe('9 - Atualize a quantidade de produtos', () => { let connection; let db; diff --git a/utils/validatorsSales.js b/utils/validatorsSales.js index 178434e..5a928e5 100644 --- a/utils/validatorsSales.js +++ b/utils/validatorsSales.js @@ -1,10 +1,13 @@ const { ObjectId } = require('mongodb'); const modelsProducts = require('../models/products'); -const UNPROCESSABLE_ENTITY = 422; -const MSG_WRONG_ID = 'Wrong sale ID format'; const CODE_INVALID_DATA = 'invalid_data'; +// const CODE_INTERNAL_SERVER_ERROR = 'Internal Server Error'; + +// const STATUS_INTERNAL_SERVER_ERROR = 500; const STATUS_UNPROCESSABLE_ENTITY = 422; +const MSG_WRONG_ID = 'Wrong sale ID format'; +// const MSG_NOT_UPDATED_PRODUCT_QTD = 'We got unable update product quantity'; function quantityInArray(soldProducts) { const msg = 'Wrong product ID or invalid quantity'; @@ -12,14 +15,14 @@ function quantityInArray(soldProducts) { const resultType = soldProducts.some(({ quantity }) => typeof quantity !== 'number'); if (resultSize || resultType) { - return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: msg }; + return { status: STATUS_UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: msg }; } return {}; } const idExistsInArray = async (soldProducts) => { const promises = soldProducts.map(async ({ productId }) => { - const check = await modelsProducts.getById(productId); // Em services/products + const check = await modelsProducts.getById(productId); if (check === null) return productId; @@ -49,12 +52,32 @@ const validSaleId = (id) => { } }; -// const updateProductQtd = (id, qtd) => { +const decProductQtd = async (soldProducts) => { + const promises = soldProducts.map(async ({ productId, quantity }) => { + const { modifiedCount } = await modelsProducts.updateQtd(productId, -quantity); + if (modifiedCount === 0) return { modifiedCount, productId }; -// }; + return {}; + }); + + const result = await Promise.all(promises); + + // DEBUG + + console.log('validatorSales: o que tem no result da proimise:'); + console.log(result); + + const checkIfAllUpdated = result.some((modifiedCount) => modifiedCount === 0); + + if (checkIfAllUpdated) { + return `Os seguintes itens não foram atualizados: ${result} `; + } + return 'Deu certo, atualizou todos produtos'; +}; module.exports = { idExistsInArray, quantityInArray, validSaleId, + decProductQtd, }; \ No newline at end of file