Adiciona requisito 9.1

This commit is contained in:
Michel Pereira 2021-10-24 00:08:08 -03:00
parent 4f0f7f3dbc
commit 7b67dbfa41
4 changed files with 48 additions and 14 deletions

View file

@ -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,
};

View file

@ -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;
};

View file

@ -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;

View file

@ -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,
};