Spaces:
Sleeping
Sleeping
File size: 5,141 Bytes
4dbde5b 0159174 4dbde5b 5f8e928 4dbde5b 2671db8 4dbde5b 0159174 4dbde5b 0159174 4dbde5b 0b68098 4dbde5b 0159174 4dbde5b 5f8e928 4dbde5b 2671db8 4dbde5b 622a0ec 4dbde5b 622a0ec 0159174 622a0ec 4dbde5b 622a0ec 4dbde5b 622a0ec 4dbde5b 2671db8 0159174 4dbde5b 0159174 4dbde5b 0b68098 4dbde5b 5f8e928 4dbde5b 2671db8 4dbde5b 0159174 4dbde5b 0159174 4dbde5b 0330133 4dbde5b 0b68098 4dbde5b | 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | import Product from "../models/ProductModel.js";
import { resolveBranchForWrite, resolveScopedBranch } from "../utils/branchContext.js";
const normalizeBarcode = (value) =>
value && value.trim() !== "" ? value.trim() : null;
export const createProduct = async (req, res) => {
try {
const { name, price, stock, description, barcode, sku, unit, category, lowStockThreshold, costPrice } = req.body;
const tenantId = req.user.tenant._id;
const branch = await resolveBranchForWrite(req);
const exists = await Product.findOne({
tenant: tenantId,
branch: branch._id,
$or: [{ name }, ...(barcode ? [{ barcode }] : [])],
});
if (exists) {
return res.status(400).json({ message: "المنتج موجود بالفعل" });
}
const product = await Product.create({
tenant: tenantId,
branch: branch._id,
name,
price,
stock,
description,
barcode: normalizeBarcode(barcode),
sku,
unit,
category: category || "عام",
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 filter = req.query.filter || "all";
const defaultThreshold = Number(req.user.tenant.settings?.lowStockThreshold || 5);
const { tenantId, branchFilter } = await resolveScopedBranch(req);
const query = { tenant: tenantId, ...branchFilter };
if (search) {
query.name = { $regex: search, $options: "i" };
}
if (filter === "low") {
query.$expr = {
$lte: [
{ $toDouble: "$stock" },
{ $toDouble: { $ifNull: ["$lowStockThreshold", defaultThreshold] } },
],
};
} else if (filter === "available") {
query.$expr = {
$gt: [
{ $toDouble: "$stock" },
{ $toDouble: { $ifNull: ["$lowStockThreshold", defaultThreshold] } },
],
};
}
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 || defaultThreshold),
})),
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, category, lowStockThreshold, costPrice } = req.body;
const { branchFilter } = await resolveScopedBranch(req);
const product = await Product.findOne({
_id: id,
tenant: req.user.tenant._id,
...branchFilter,
});
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 !== undefined ? normalizeBarcode(barcode) : product.barcode;
product.sku = sku ?? product.sku;
product.unit = unit ?? product.unit;
product.category = category ?? product.category;
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 { branchFilter } = await resolveScopedBranch(req);
const product = await Product.findOneAndDelete({
_id: id,
tenant: req.user.tenant._id,
...branchFilter,
});
if (!product) {
return res.status(404).json({ message: "المنتج غير موجود" });
}
res.status(200).json({ message: "تم حذف المنتج بنجاح" });
} catch (error) {
res.status(500).json({ message: error.message });
}
};
|