stock api done
This commit is contained in:
parent
23c7508f6e
commit
6b4cd0e76d
@ -200,3 +200,97 @@ export const getProductsAndStockByRD = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Create or Update Stock
|
||||||
|
export const createOrUpdateStock = async (req, res) => {
|
||||||
|
const userId = req.user._id;
|
||||||
|
try {
|
||||||
|
const { products } = req.body; // products: [{ productid, Stock }]
|
||||||
|
|
||||||
|
// Get all product IDs from the Product collection
|
||||||
|
const allProducts = await Product.find({}, "_id"); // Fetch only the _id field
|
||||||
|
const allProductIds = allProducts.map((p) => p._id.toString()); // Convert to string array
|
||||||
|
|
||||||
|
// Initialize stock for all products as 0
|
||||||
|
const updatedProducts = allProductIds.map((productId) => {
|
||||||
|
const productInRequest = products.find((p) => p.productid === productId);
|
||||||
|
|
||||||
|
return {
|
||||||
|
productid: productId,
|
||||||
|
Stock: productInRequest ? productInRequest.Stock : 0,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check if stock entry already exists for the user
|
||||||
|
let stock = await PDStock.findOne({ userId });
|
||||||
|
|
||||||
|
if (stock) {
|
||||||
|
// Replace stock with the updated product list
|
||||||
|
stock.products = updatedProducts;
|
||||||
|
await stock.save();
|
||||||
|
return res
|
||||||
|
.status(200)
|
||||||
|
.json({ message: "Stock updated successfully", stock });
|
||||||
|
} else {
|
||||||
|
// Create a new stock entry if it doesn't exist
|
||||||
|
const newStock = new PDStock({ userId, products: updatedProducts });
|
||||||
|
await newStock.save();
|
||||||
|
return res
|
||||||
|
.status(201)
|
||||||
|
.json({ message: "Stock created successfully", stock: newStock });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating or creating stock:", error);
|
||||||
|
res.status(500).json({ message: "Server error", error });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getStockPD = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const userId = req.user._id; // userId is provided in the URL
|
||||||
|
|
||||||
|
// Fetch all products with their _id, name, and SKU
|
||||||
|
const allProducts = await Product.find({}, "_id name SKU"); // Adjust field names if needed
|
||||||
|
|
||||||
|
// Map products into an object for easy access
|
||||||
|
const productMap = new Map(
|
||||||
|
allProducts.map((p) => [p._id.toString(), { name: p.name, sku: p.SKU }])
|
||||||
|
);
|
||||||
|
|
||||||
|
// Fetch the user's stock from the PDStock model
|
||||||
|
let stock = await PDStock.findOne({ userId });
|
||||||
|
|
||||||
|
let productsWithStock = [];
|
||||||
|
|
||||||
|
if (stock) {
|
||||||
|
// Create a map of product stocks from the user's stock
|
||||||
|
const stockMap = new Map(
|
||||||
|
stock.products.map((p) => [p.productid.toString(), p.Stock])
|
||||||
|
);
|
||||||
|
|
||||||
|
// Iterate over all products, assigning stock or initializing to 0
|
||||||
|
productsWithStock = allProducts.map((product) => ({
|
||||||
|
productid: product._id,
|
||||||
|
name: product.name,
|
||||||
|
SKU: product.SKU,
|
||||||
|
Stock: stockMap.get(product._id.toString()) || 0,
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
// If no stock entry exists, initialize all products with stock 0
|
||||||
|
productsWithStock = allProducts.map((product) => ({
|
||||||
|
productid: product._id,
|
||||||
|
name: product.name,
|
||||||
|
sku: product.sku,
|
||||||
|
Stock: 0,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
message: "Stock fetched successfully",
|
||||||
|
stocks: productsWithStock,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching stock:", error);
|
||||||
|
res.status(500).json({ message: "Server error", error });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import {
|
import {
|
||||||
|
createOrUpdateStock,
|
||||||
getProductsAndStockByPD,
|
getProductsAndStockByPD,
|
||||||
getProductsAndStockByRD,
|
getProductsAndStockByRD,
|
||||||
|
getStockPD,
|
||||||
} from "./StockController.js";
|
} from "./StockController.js";
|
||||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||||
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
import { isAuthenticatedRD } from "../../middlewares/rdAuth.js";
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get(
|
router.get("/pd/stock/:userId", isAuthenticatedUser, getProductsAndStockByPD);
|
||||||
"/pd/stock/:userId",
|
router.get("/pd/stock", isAuthenticatedUser, getStockPD);
|
||||||
isAuthenticatedUser,
|
router.put("/pd/stock-update", isAuthenticatedUser, createOrUpdateStock);
|
||||||
getProductsAndStockByPD
|
|
||||||
);
|
|
||||||
router.get(
|
router.get(
|
||||||
"/rd/stock/:userId",
|
"/rd/stock/:userId",
|
||||||
isAuthenticatedUser,
|
isAuthenticatedUser,
|
||||||
|
Loading…
Reference in New Issue
Block a user