Spaces:
Sleeping
Sleeping
File size: 3,674 Bytes
66821dd 7392f82 66821dd 7392f82 66821dd 7392f82 66821dd 7392f82 66821dd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | import Product from "../models/ProductModel.js";
export const createProduct = async (req, res) => {
try {
const { name, price, stock, description, barcode, sku, unit, lowStockThreshold, costPrice } = req.body;
const tenantId = req.user.tenant._id;
const exists = await Product.findOne({
tenant: tenantId,
$or: [{ name }, ...(barcode ? [{ barcode }] : [])],
});
if (exists) {
return res.status(400).json({ message: "المنتج موجود بالفعل" });
}
const product = await Product.create({
tenant: tenantId,
name,
price,
stock,
description,
barcode,
sku,
unit,
lowStockThreshold: lowStockThreshold || req.user.tenant.settings?.lowStockThreshold || 5,
costPrice: costPrice || 0,
createdBy: req.user._id,
});
res.status(201).json(product);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
export const getProducts = async (req, res) => {
try {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 5;
const skip = (page - 1) * limit;
const search = req.query.search || "";
const query = { tenant: req.user.tenant._id };
if (search) {
query.name = { $regex: search, $options: "i" };
}
const total = await Product.countDocuments(query);
const products = await Product.find(query)
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit);
res.status(200).json({
data: products.map((product) => ({
...product.toObject(),
isLowStock:
Number(product.stock) <=
Number(product.lowStockThreshold || req.user.tenant.settings?.lowStockThreshold || 5),
})),
pagination: {
total,
page,
limit,
totalPages: Math.ceil(total / limit),
},
});
} catch (error) {
res.status(500).json({ message: error.message });
}
};
export const updateProduct = async (req, res) => {
try {
const { id } = req.params;
const { name, price, stock, description, barcode, sku, unit, lowStockThreshold, costPrice } = req.body;
const product = await Product.findOne({
_id: id,
tenant: req.user.tenant._id,
});
if (!product) {
return res.status(404).json({ message: "المنتج غير موجود" });
}
product.name = name ?? product.name;
product.price = price ?? product.price;
product.stock = stock ?? product.stock;
product.description = description ?? product.description;
product.barcode = barcode ?? product.barcode;
product.sku = sku ?? product.sku;
product.unit = unit ?? product.unit;
product.lowStockThreshold = lowStockThreshold ?? product.lowStockThreshold;
product.costPrice = costPrice ?? product.costPrice;
await product.save();
res.status(200).json(product);
} catch (error) {
res.status(500).json({ message: error.message });
}
};
export const deleteProduct = async (req, res) => {
try {
const { id } = req.params;
const product = await Product.findOneAndDelete({
_id: id,
tenant: req.user.tenant._id,
});
if (!product) {
return res.status(404).json({ message: "المنج غير موجودلهف" });
}
res.status(200).json({ message: "تم حذف المنتج بنجاح" });
} catch (error) {
res.status(500).json({ message: error.message });
}
};
|