updated inventory code

This commit is contained in:
ROSHAN GARG 2024-10-24 10:54:51 +05:30
parent 6e620d22b3
commit 6b9fbbb5b6
2 changed files with 9 additions and 5 deletions

View File

@ -206,7 +206,7 @@ export const createOrUpdateInventory = async (req, res) => {
const userId = req.user._id;
try {
const { products } = req.body; // products: [{ productid, Stock }]
console.log(products);
// Fetch all products in the system
const allProducts = await Product.find({}, "_id SKU"); // Fetch only _id and SKU fields
const allProductIds = allProducts.map((p) => p._id.toString());
@ -225,17 +225,20 @@ export const createOrUpdateInventory = async (req, res) => {
return {
...existingProduct,
openingInventory: productInRequest
? productInRequest.Stock
? productInRequest.openingInventory
: existingProduct.openingInventory,
Stock: productInRequest ? productInRequest.openingInventory : 0,
};
} else {
// New product, set both stock and opening inventory to the same value
console.log("came here ");
return {
productid: productId,
SKU: allProducts.find((p) => p._id.toString() === productId).SKU,
openingInventory: productInRequest ? productInRequest.Stock : 0,
Stock: productInRequest ? productInRequest.Stock : 0,
openingInventory: productInRequest
? productInRequest.openingInventory
: 0,
Stock: productInRequest ? productInRequest.openingInventory : 0,
};
}
});

View File

@ -1,6 +1,7 @@
import express from "express";
import {
createOrUpdateInventory,
getAllUsersWithStock,
getProductsAndStockByPD,
getProductsAndStockByRD,
getStockPD,