Sindbad~EG File Manager
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _mongoose = _interopRequireDefault(require("mongoose"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var Sale = new _mongoose["default"].Schema({
date: {
type: Date,
required: true
},
note: {
type: String,
required: false,
"default": ""
},
customer: {
type: _mongoose["default"].Schema.ObjectId,
ref: "Customer"
},
added_by: {
type: _mongoose["default"].Schema.ObjectId,
ref: "User",
required: true
},
customer_payment_mode: {
type: _mongoose["default"].Schema.ObjectId,
ref: "CustomerPaymentMode"
},
sold_items: [{
product: {
type: _mongoose["default"].Schema.ObjectId,
ref: "Product",
required: true
},
quantity: {
type: Number,
required: true,
"default": 0,
min: [0, "Ensures quantity is always positive"]
}
}]
}, {
timestamps: true
});
// Middleware to auto-populate refs with specific fields
function autoPopulateRefs(next) {
this.populate({
path: "added_by",
select: "name _id"
}).populate({
path: "customer",
select: "name _id"
}).populate({
path: "customer_payment_mode"
});
next();
}
// Apply the middleware
Sale.pre("find", autoPopulateRefs).pre("findOne", autoPopulateRefs).pre("findOneAndUpdate", autoPopulateRefs);
var _default = exports["default"] = _mongoose["default"].model("Sale", Sale);
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists