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

View File

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