Spaces:
Sleeping
Sleeping
Upload 38 files
Browse files
controller/productController.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
import Product from "../models/ProductModel.js";
|
| 2 |
import { resolveBranchForWrite, resolveScopedBranch } from "../utils/branchContext.js";
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
export const createProduct = async (req, res) => {
|
| 5 |
try {
|
| 6 |
const { name, price, stock, description, barcode, sku, unit, category, lowStockThreshold, costPrice } = req.body;
|
|
@@ -24,7 +28,7 @@ export const createProduct = async (req, res) => {
|
|
| 24 |
price,
|
| 25 |
stock,
|
| 26 |
description,
|
| 27 |
-
barcode,
|
| 28 |
sku,
|
| 29 |
unit,
|
| 30 |
category: category || "عام",
|
|
@@ -116,7 +120,7 @@ export const updateProduct = async (req, res) => {
|
|
| 116 |
product.price = price ?? product.price;
|
| 117 |
product.stock = stock ?? product.stock;
|
| 118 |
product.description = description ?? product.description;
|
| 119 |
-
product.barcode = barcode ?
|
| 120 |
product.sku = sku ?? product.sku;
|
| 121 |
product.unit = unit ?? product.unit;
|
| 122 |
product.category = category ?? product.category;
|
|
|
|
| 1 |
import Product from "../models/ProductModel.js";
|
| 2 |
import { resolveBranchForWrite, resolveScopedBranch } from "../utils/branchContext.js";
|
| 3 |
|
| 4 |
+
const normalizeBarcode = (value) =>
|
| 5 |
+
value && value.trim() !== "" ? value.trim() : null;
|
| 6 |
+
|
| 7 |
+
|
| 8 |
export const createProduct = async (req, res) => {
|
| 9 |
try {
|
| 10 |
const { name, price, stock, description, barcode, sku, unit, category, lowStockThreshold, costPrice } = req.body;
|
|
|
|
| 28 |
price,
|
| 29 |
stock,
|
| 30 |
description,
|
| 31 |
+
barcode: normalizeBarcode(barcode),
|
| 32 |
sku,
|
| 33 |
unit,
|
| 34 |
category: category || "عام",
|
|
|
|
| 120 |
product.price = price ?? product.price;
|
| 121 |
product.stock = stock ?? product.stock;
|
| 122 |
product.description = description ?? product.description;
|
| 123 |
+
product.barcode = barcode !== undefined ? normalizeBarcode(barcode) : product.barcode;
|
| 124 |
product.sku = sku ?? product.sku;
|
| 125 |
product.unit = unit ?? product.unit;
|
| 126 |
product.category = category ?? product.category;
|
models/ProductModel.js
CHANGED
|
@@ -75,9 +75,7 @@ productSchema.index(
|
|
| 75 |
{ tenant: 1, branch: 1, barcode: 1 },
|
| 76 |
{
|
| 77 |
unique: true,
|
| 78 |
-
partialFilterExpression: {
|
| 79 |
-
barcode: { $type: "string" }
|
| 80 |
-
}
|
| 81 |
}
|
| 82 |
);
|
| 83 |
export default mongoose.model("Product", productSchema);
|
|
|
|
| 75 |
{ tenant: 1, branch: 1, barcode: 1 },
|
| 76 |
{
|
| 77 |
unique: true,
|
| 78 |
+
partialFilterExpression: { barcode: { $exists: true, $gt: "" } }
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
);
|
| 81 |
export default mongoose.model("Product", productSchema);
|