Adiciona requisito 9.1
This commit is contained in:
parent
4f0f7f3dbc
commit
7b67dbfa41
|
@ -38,13 +38,13 @@ const update = async (id, name, quantity) => connection()
|
||||||
return updateLog;
|
return updateLog;
|
||||||
});
|
});
|
||||||
|
|
||||||
// const updateQtd = async (id, quantity) => connection()
|
const updateQtd = async (id, quantity) => connection()
|
||||||
// .then(async (db) => {
|
.then(async (db) => {
|
||||||
// const updateLog = await db.collection('products').updateOne({ _id: ObjectId(id) },
|
const updateLog = await db.collection('products').updateOne({ _id: ObjectId(id) },
|
||||||
// { $set: { quantity } });
|
{ $inc: { quantity } });
|
||||||
|
|
||||||
// return updateLog;
|
return updateLog;
|
||||||
// });
|
});
|
||||||
|
|
||||||
const deleteIt = async (id) => connection()
|
const deleteIt = async (id) => connection()
|
||||||
.then(async (db) => {
|
.then(async (db) => {
|
||||||
|
@ -59,6 +59,6 @@ module.exports = {
|
||||||
getAll,
|
getAll,
|
||||||
getById,
|
getById,
|
||||||
update,
|
update,
|
||||||
// updateQtd,
|
updateQtd,
|
||||||
deleteIt,
|
deleteIt,
|
||||||
};
|
};
|
||||||
|
|
|
@ -42,6 +42,17 @@ const create = async (soldProducts) => {
|
||||||
// CREATING:
|
// CREATING:
|
||||||
|
|
||||||
const insertedId = await models.create(soldProducts); // if come here, thas means all productId were found
|
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;
|
return insertedId;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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 connection;
|
||||||
let db;
|
let db;
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
const { ObjectId } = require('mongodb');
|
const { ObjectId } = require('mongodb');
|
||||||
const modelsProducts = require('../models/products');
|
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_INVALID_DATA = 'invalid_data';
|
||||||
|
// const CODE_INTERNAL_SERVER_ERROR = 'Internal Server Error';
|
||||||
|
|
||||||
|
// const STATUS_INTERNAL_SERVER_ERROR = 500;
|
||||||
const STATUS_UNPROCESSABLE_ENTITY = 422;
|
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) {
|
function quantityInArray(soldProducts) {
|
||||||
const msg = 'Wrong product ID or invalid quantity';
|
const msg = 'Wrong product ID or invalid quantity';
|
||||||
|
@ -12,14 +15,14 @@ function quantityInArray(soldProducts) {
|
||||||
const resultType = soldProducts.some(({ quantity }) => typeof quantity !== 'number');
|
const resultType = soldProducts.some(({ quantity }) => typeof quantity !== 'number');
|
||||||
|
|
||||||
if (resultSize || resultType) {
|
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 {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const idExistsInArray = async (soldProducts) => {
|
const idExistsInArray = async (soldProducts) => {
|
||||||
const promises = soldProducts.map(async ({ productId }) => {
|
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;
|
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 = {
|
module.exports = {
|
||||||
idExistsInArray,
|
idExistsInArray,
|
||||||
quantityInArray,
|
quantityInArray,
|
||||||
validSaleId,
|
validSaleId,
|
||||||
|
decProductQtd,
|
||||||
};
|
};
|
Loading…
Reference in a new issue