RD stock create
This commit is contained in:
parent
3e7452a484
commit
90495fdad1
2
app.js
2
app.js
@ -203,7 +203,7 @@ import TaskRoute from "./resources/Task/TaskRoute.js";
|
||||
// visit RD and PD
|
||||
import VisitRDandPDRoute from "./resources/VisitRD&PD/VisitRD&PDRoute.js";
|
||||
//Stock
|
||||
import Stock from "./resources/Stock/PdStockRoute.js";
|
||||
import Stock from "./resources/Stock/StockRoute.js";
|
||||
app.use("/api/v1", user);
|
||||
|
||||
//Product
|
||||
|
@ -1,103 +0,0 @@
|
||||
import mongoose from "mongoose";
|
||||
import { PDStock } from "./PdStockModel.js";
|
||||
import { Product } from "../Products/ProductModel.js";
|
||||
|
||||
export const getProductsAndStockByUser = async (req, res) => {
|
||||
try {
|
||||
const { userId } = req.params;
|
||||
|
||||
// Pagination parameters
|
||||
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const skip = (page - 1) * PAGE_SIZE;
|
||||
|
||||
// Filtering criteria
|
||||
const filter = {};
|
||||
if (req.query.name) {
|
||||
filter.name = {
|
||||
$regex: new RegExp(req.query.name, "i"),
|
||||
};
|
||||
}
|
||||
if (req.query.category) {
|
||||
filter.category = mongoose.Types.ObjectId(req.query.category);
|
||||
}
|
||||
if (req.query.brand) {
|
||||
filter.brand = mongoose.Types.ObjectId(req.query.brand);
|
||||
}
|
||||
|
||||
// Fetch user's PDStock data and products concurrently
|
||||
const [userStock, products] = await Promise.all([
|
||||
PDStock.findOne({ userId: mongoose.Types.ObjectId(userId) }),
|
||||
Product.aggregate([
|
||||
{ $match: filter },
|
||||
{
|
||||
$lookup: {
|
||||
from: "categorymodels",
|
||||
localField: "category",
|
||||
foreignField: "_id",
|
||||
as: "categoryDetails",
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "brandmodels",
|
||||
localField: "brand",
|
||||
foreignField: "_id",
|
||||
as: "brandDetails",
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
category: { $arrayElemAt: ["$categoryDetails.categoryName", 0] },
|
||||
brand: { $arrayElemAt: ["$brandDetails.brandName", 0] },
|
||||
GST: 1,
|
||||
HSN_Code: 1,
|
||||
SKU: 1,
|
||||
addedBy: 1,
|
||||
createdAt: 1,
|
||||
description: 1,
|
||||
image: 1,
|
||||
name: 1,
|
||||
price: 1,
|
||||
product_Status: 1,
|
||||
updatedAt: 1,
|
||||
},
|
||||
},
|
||||
{ $skip: skip },
|
||||
{ $limit: PAGE_SIZE },
|
||||
]),
|
||||
]);
|
||||
|
||||
// Create a stock map for easy lookup
|
||||
const stockMap = {};
|
||||
if (userStock && userStock.products) {
|
||||
userStock.products.forEach((product) => {
|
||||
stockMap[product.productid.toString()] = product.Stock;
|
||||
});
|
||||
}
|
||||
|
||||
// Combine products with their respective stock
|
||||
const productsWithStock = products.map((product) => ({
|
||||
...product,
|
||||
stock: stockMap[product._id.toString()] || 0,
|
||||
}));
|
||||
|
||||
// Get total count for pagination purposes
|
||||
const total = await Product.countDocuments(filter);
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
totalProducts: total,
|
||||
totalPages: Math.ceil(total / PAGE_SIZE),
|
||||
currentPage: page,
|
||||
products: productsWithStock,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching products with stock:", error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Error fetching products and stock",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
26
resources/Stock/RdStockModel.js
Normal file
26
resources/Stock/RdStockModel.js
Normal file
@ -0,0 +1,26 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
// Define Product record schema
|
||||
const ProductRecordSchema = new mongoose.Schema({
|
||||
productid: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: 'Product',
|
||||
required: true,
|
||||
},
|
||||
Stock: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
});
|
||||
|
||||
// Define main Stock schema
|
||||
const StockSchema = new mongoose.Schema({
|
||||
userId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
refPath: 'RetailDistributor',
|
||||
required: true,
|
||||
},
|
||||
products: [ProductRecordSchema],
|
||||
}, { timestamps: true, versionKey: false });
|
||||
|
||||
export const RDStock = mongoose.model('RDStock', StockSchema);
|
202
resources/Stock/StockController.js
Normal file
202
resources/Stock/StockController.js
Normal file
@ -0,0 +1,202 @@
|
||||
import mongoose from "mongoose";
|
||||
import { PDStock } from "./PdStockModel.js";
|
||||
import { Product } from "../Products/ProductModel.js";
|
||||
import { RDStock } from "./RdStockModel.js";
|
||||
|
||||
export const getProductsAndStockByPD = async (req, res) => {
|
||||
try {
|
||||
const { userId } = req.params;
|
||||
|
||||
// Pagination parameters
|
||||
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const skip = (page - 1) * PAGE_SIZE;
|
||||
|
||||
// Filtering criteria
|
||||
const filter = {};
|
||||
if (req.query.name) {
|
||||
filter.name = {
|
||||
$regex: new RegExp(req.query.name, "i"),
|
||||
};
|
||||
}
|
||||
if (req.query.category) {
|
||||
filter.category = mongoose.Types.ObjectId(req.query.category);
|
||||
}
|
||||
if (req.query.brand) {
|
||||
filter.brand = mongoose.Types.ObjectId(req.query.brand);
|
||||
}
|
||||
|
||||
// Fetch user's PDStock data and products concurrently
|
||||
const [userStock, products] = await Promise.all([
|
||||
PDStock.findOne({ userId: mongoose.Types.ObjectId(userId) }),
|
||||
Product.aggregate([
|
||||
{ $match: filter },
|
||||
{
|
||||
$lookup: {
|
||||
from: "categorymodels",
|
||||
localField: "category",
|
||||
foreignField: "_id",
|
||||
as: "categoryDetails",
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "brandmodels",
|
||||
localField: "brand",
|
||||
foreignField: "_id",
|
||||
as: "brandDetails",
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
category: { $arrayElemAt: ["$categoryDetails.categoryName", 0] },
|
||||
brand: { $arrayElemAt: ["$brandDetails.brandName", 0] },
|
||||
GST: 1,
|
||||
HSN_Code: 1,
|
||||
SKU: 1,
|
||||
addedBy: 1,
|
||||
createdAt: 1,
|
||||
description: 1,
|
||||
image: 1,
|
||||
name: 1,
|
||||
price: 1,
|
||||
product_Status: 1,
|
||||
updatedAt: 1,
|
||||
},
|
||||
},
|
||||
{ $skip: skip },
|
||||
{ $limit: PAGE_SIZE },
|
||||
]),
|
||||
]);
|
||||
|
||||
// Create a stock map for easy lookup
|
||||
const stockMap = {};
|
||||
if (userStock && userStock.products) {
|
||||
userStock.products.forEach((product) => {
|
||||
stockMap[product.productid.toString()] = product.Stock;
|
||||
});
|
||||
}
|
||||
|
||||
// Combine products with their respective stock
|
||||
const productsWithStock = products.map((product) => ({
|
||||
...product,
|
||||
stock: stockMap[product._id.toString()] || 0,
|
||||
}));
|
||||
|
||||
// Get total count for pagination purposes
|
||||
const total = await Product.countDocuments(filter);
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
totalProducts: total,
|
||||
totalPages: Math.ceil(total / PAGE_SIZE),
|
||||
currentPage: page,
|
||||
products: productsWithStock,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching products with stock:", error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Error fetching products and stock",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const getProductsAndStockByRD = async (req, res) => {
|
||||
try {
|
||||
const { userId } = req.params;
|
||||
|
||||
// Pagination parameters
|
||||
const PAGE_SIZE = parseInt(req.query.show) || 10;
|
||||
const page = parseInt(req.query.page) || 1;
|
||||
const skip = (page - 1) * PAGE_SIZE;
|
||||
|
||||
// Filtering criteria
|
||||
const filter = {};
|
||||
if (req.query.name) {
|
||||
filter.name = {
|
||||
$regex: new RegExp(req.query.name, "i"),
|
||||
};
|
||||
}
|
||||
if (req.query.category) {
|
||||
filter.category = mongoose.Types.ObjectId(req.query.category);
|
||||
}
|
||||
if (req.query.brand) {
|
||||
filter.brand = mongoose.Types.ObjectId(req.query.brand);
|
||||
}
|
||||
|
||||
// Fetch user's RDStock data and products concurrently
|
||||
const [userStock, products] = await Promise.all([
|
||||
RDStock.findOne({ userId: mongoose.Types.ObjectId(userId) }),
|
||||
Product.aggregate([
|
||||
{ $match: filter },
|
||||
{
|
||||
$lookup: {
|
||||
from: "categorymodels",
|
||||
localField: "category",
|
||||
foreignField: "_id",
|
||||
as: "categoryDetails",
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "brandmodels",
|
||||
localField: "brand",
|
||||
foreignField: "_id",
|
||||
as: "brandDetails",
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
category: { $arrayElemAt: ["$categoryDetails.categoryName", 0] },
|
||||
brand: { $arrayElemAt: ["$brandDetails.brandName", 0] },
|
||||
GST: 1,
|
||||
HSN_Code: 1,
|
||||
SKU: 1,
|
||||
addedBy: 1,
|
||||
createdAt: 1,
|
||||
description: 1,
|
||||
image: 1,
|
||||
name: 1,
|
||||
price: 1,
|
||||
product_Status: 1,
|
||||
updatedAt: 1,
|
||||
},
|
||||
},
|
||||
{ $skip: skip },
|
||||
{ $limit: PAGE_SIZE },
|
||||
]),
|
||||
]);
|
||||
|
||||
// Create a stock map for easy lookup
|
||||
const stockMap = {};
|
||||
if (userStock && userStock.products) {
|
||||
userStock.products.forEach((product) => {
|
||||
stockMap[product.productid.toString()] = product.Stock;
|
||||
});
|
||||
}
|
||||
|
||||
// Combine products with their respective stock
|
||||
const productsWithStock = products.map((product) => ({
|
||||
...product,
|
||||
stock: stockMap[product._id.toString()] || 0,
|
||||
}));
|
||||
|
||||
// Get total count for pagination purposes
|
||||
const total = await Product.countDocuments(filter);
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
totalProducts: total,
|
||||
totalPages: Math.ceil(total / PAGE_SIZE),
|
||||
currentPage: page,
|
||||
products: productsWithStock,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error fetching products with stock:", error);
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Error fetching products and stock",
|
||||
});
|
||||
}
|
||||
};
|
@ -1,5 +1,5 @@
|
||||
import express from "express";
|
||||
import { getProductsAndStockByUser } from "./PdStockController.js";
|
||||
import { getProductsAndStockByPD ,getProductsAndStockByRD} from "./StockController.js";
|
||||
import { authorizeRoles, isAuthenticatedUser } from "../../middlewares/auth.js";
|
||||
const router = express.Router();
|
||||
|
||||
@ -7,6 +7,12 @@ router.get(
|
||||
"/pd/stock/:userId",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
getProductsAndStockByUser
|
||||
getProductsAndStockByPD
|
||||
);
|
||||
router.get(
|
||||
"/rd/stock/:userId",
|
||||
isAuthenticatedUser,
|
||||
authorizeRoles("admin"),
|
||||
getProductsAndStockByRD
|
||||
);
|
||||
export default router;
|
Loading…
Reference in New Issue
Block a user