Files
ostandari/server/models/Category.js
T
2026-06-19 15:47:02 +03:30

92 lines
3.1 KiB
JavaScript

const mongoose = require("mongoose");
const s3Storage = require("../plugins/s3Storage");
function autoPopulateSubs(next) {
this.populate({
path: "childs",
select:
"title childs navIndex url modelType treeCat parent defaultPageType",
options: {
sort: "navIndex",
},
// sort: {navIndex: -1},
});
next();
}
function file(file) {
if (!file) return null;
if (file.startsWith("http://") || file.startsWith("https://")) return file;
return s3Storage.getPublicUrl(`uploads/files/terms/${file}`);
}
function image(img) {
if (!img) return "/img/no-cover.jpg";
if (img.startsWith("http://") || img.startsWith("https://")) return img;
return s3Storage.getPublicUrl(`uploads/images/category/${img}`);
}
function shafafiatIcon(icon) {
if (!icon) return null;
if (icon.startsWith("http://") || icon.startsWith("https://")) return icon;
return s3Storage.getPublicUrl(`uploads/images/category/shafafiat/${icon}`);
}
const CategoryTermsFile = mongoose.Schema({
file: { type: String, get: file },
title: String,
});
const CategorySchema = mongoose.Schema({
title: String,
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: "Category",
default: null,
},
childs: [{ type: mongoose.Schema.Types.ObjectId, ref: "Category" }],
treeCat: [{ type: mongoose.Schema.Types.ObjectId, ref: "Category" }],
rootParent: { type: mongoose.Schema.Types.ObjectId, ref: "Category" },
priority: { type: Number, default: 0 },
showInMenu: Boolean,
navIndex: { type: Number, default: null },
news: { type: Boolean, default: true },
child: { type: Boolean, default: true },
special: { type: Boolean, default: false },
allowComment: { type: Boolean, default: false },
level: { type: Number, default: 1 },
siteMap: { type: Boolean, default: true },
modelType: String,
_creator: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
_updatedBy: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
haveNews: { type: Boolean, default: false },
///// for client
hasNewsTab: { type: Boolean, default: true },
hasContentTab: { type: Boolean, default: true },
hasMediaTab: { type: Boolean, default: true },
hasSubMenusTab: { type: Boolean, default: true },
hasTermsTab: { type: Boolean, default: true },
hasNotificationsTab: { type: Boolean, default: true },
hasSpecNewsTab: { type: Boolean, default: true },
hasNazarTab: { type: Boolean, default: true },
hasNewsFileTab: { type: Boolean, default: true },
defaultPageType: { type: String, default: "news" },
/** page */
showInFooter: { type: Number, enum: [0, 1, 2, 3], default: 0 },
url: { type: String, default: null },
pageContent: String,
terms: [CategoryTermsFile],
hasManagerInfo: { type: Boolean, default: true },
managerCover: { type: String, get: image },
managerName: String,
managerDescription: String,
/** if category was shafafiat this icon is for that */
icon: { type: String, get: shafafiatIcon, default: null },
});
CategorySchema.index({ parent: 1, childs: 1 });
CategorySchema.pre("findOne", autoPopulateSubs).pre("find", autoPopulateSubs);
module.exports = mongoose.model("Category", CategorySchema);