other
This commit is contained in:
132
backend/models/Store.model.js
Normal file
132
backend/models/Store.model.js
Normal file
@@ -0,0 +1,132 @@
|
||||
import mongoose from "mongoose";
|
||||
import { type } from "os";
|
||||
|
||||
const CategorySchema = new mongoose.Schema({
|
||||
_id: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
auto: true
|
||||
},
|
||||
name: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
parentId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
default: null
|
||||
}
|
||||
}, { _id: true });
|
||||
|
||||
const StoreSchema = new mongoose.Schema({
|
||||
vendorId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Vendor",
|
||||
required: true
|
||||
},
|
||||
storeName: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
welcomeMessage: {
|
||||
type: String,
|
||||
default: "Welcome to my store!"
|
||||
},
|
||||
telegramToken: {
|
||||
type: String,
|
||||
default: ""
|
||||
},
|
||||
pgpKey: {
|
||||
type: String,
|
||||
default: ""
|
||||
},
|
||||
createdAt: {
|
||||
type: Date,
|
||||
default: Date.now
|
||||
},
|
||||
shipsFrom: {
|
||||
type: String,
|
||||
default: "UK",
|
||||
},
|
||||
shipsTo: {
|
||||
type: String,
|
||||
default: "UK",
|
||||
},
|
||||
categories: [CategorySchema],
|
||||
|
||||
shippingOptions: [
|
||||
{
|
||||
name: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
price: {
|
||||
type: Number,
|
||||
required: true,
|
||||
min: 0
|
||||
}
|
||||
}
|
||||
],
|
||||
|
||||
wallets:{
|
||||
type: Object,
|
||||
default: {
|
||||
"litecoin": "",
|
||||
"bitcoin": "",
|
||||
"monero":""
|
||||
}
|
||||
},
|
||||
|
||||
feeRate:{
|
||||
type: Number,
|
||||
default: 2
|
||||
}
|
||||
});
|
||||
|
||||
// Add a method to get category hierarchy
|
||||
StoreSchema.methods.getCategoryHierarchy = function() {
|
||||
const categories = this.categories.toObject();
|
||||
|
||||
// Helper function to build tree structure
|
||||
const buildTree = (parentId = null) => {
|
||||
return categories
|
||||
.filter(cat =>
|
||||
(!parentId && !cat.parentId) ||
|
||||
(cat.parentId?.toString() === parentId?.toString())
|
||||
)
|
||||
.map(cat => ({
|
||||
...cat,
|
||||
children: buildTree(cat._id)
|
||||
}));
|
||||
};
|
||||
|
||||
return buildTree();
|
||||
};
|
||||
|
||||
// Add validation to prevent circular references
|
||||
CategorySchema.pre('save', function(next) {
|
||||
if (!this.parentId) {
|
||||
return next();
|
||||
}
|
||||
|
||||
const checkCircular = (categoryId, parentId) => {
|
||||
if (!parentId) return false;
|
||||
if (categoryId.toString() === parentId.toString()) return true;
|
||||
|
||||
const parent = this.parent().categories.id(parentId);
|
||||
if (!parent) return false;
|
||||
|
||||
return checkCircular(categoryId, parent.parentId);
|
||||
};
|
||||
|
||||
if (checkCircular(this._id, this.parentId)) {
|
||||
next(new Error('Circular reference detected in category hierarchy'));
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
// Add index for better query performance
|
||||
StoreSchema.index({ 'categories.name': 1, 'categories.parentId': 1 });
|
||||
|
||||
const Store = mongoose.model("Store", StoreSchema);
|
||||
|
||||
export default Store;
|
||||
Reference in New Issue
Block a user