70 lines
1.5 KiB
JavaScript
70 lines
1.5 KiB
JavaScript
import mongoose from "mongoose";
|
|
|
|
const ProductSchema = new mongoose.Schema({
|
|
storeId: {
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
ref: "Store",
|
|
required: true,
|
|
},
|
|
category: {
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
required: true,
|
|
ref: "Store.categories"
|
|
},
|
|
|
|
name: { type: String, required: true },
|
|
description: { type: String },
|
|
|
|
unitType: {
|
|
type: String,
|
|
enum: ["pcs", "gr", "kg"],
|
|
required: true,
|
|
},
|
|
|
|
// Add inventory tracking fields
|
|
stockTracking: { type: Boolean, default: true },
|
|
currentStock: {
|
|
type: Number,
|
|
default: 0,
|
|
validate: {
|
|
validator: function(value) {
|
|
return !this.stockTracking || value >= 0;
|
|
},
|
|
message: "Stock cannot be negative"
|
|
}
|
|
},
|
|
lowStockThreshold: { type: Number, default: 10 },
|
|
stockStatus: {
|
|
type: String,
|
|
enum: ["in_stock", "low_stock", "out_of_stock"],
|
|
default: "out_of_stock"
|
|
},
|
|
|
|
pricing: [
|
|
{
|
|
minQuantity: {
|
|
type: Number,
|
|
required: true,
|
|
validate: {
|
|
validator: function(value) {
|
|
if (this.parent().unitType === "gr") {
|
|
return value >= 0.1;
|
|
}
|
|
return value >= 1;
|
|
},
|
|
message: "Invalid minQuantity for unitType"
|
|
}
|
|
},
|
|
pricePerUnit: {
|
|
type: Number,
|
|
required: true,
|
|
min: 0.01
|
|
},
|
|
},
|
|
],
|
|
|
|
image: { type: String },
|
|
});
|
|
|
|
export default mongoose.model("Product", ProductSchema);
|