bugfix: Enhance Agent and AgentCategory schemas with new fields for category, support contact, and promotion status

This commit is contained in:
“Praneeth 2025-06-11 22:55:07 +05:30 committed by Danny Avila
parent f55cdc9b7f
commit 348ee5821e
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
5 changed files with 57 additions and 100 deletions

View file

@ -15,28 +15,28 @@ const agentCategorySchema = new mongoose.Schema(
lowercase: true,
index: true,
},
// Display label for the category
label: {
type: String,
required: true,
trim: true,
},
// Description of the category
description: {
type: String,
trim: true,
default: '',
},
// Display order for sorting categories
order: {
type: Number,
default: 0,
index: true,
},
// Whether the category is active and should be displayed
isActive: {
type: Boolean,
@ -46,7 +46,7 @@ const agentCategorySchema = new mongoose.Schema(
},
{
timestamps: true,
}
},
);
// Indexes for performance
@ -56,32 +56,30 @@ agentCategorySchema.index({ isActive: 1, order: 1 });
* Get all active categories sorted by order
* @returns {Promise<AgentCategory[]>} Array of active categories
*/
agentCategorySchema.statics.getActiveCategories = function() {
return this.find({ isActive: true })
.sort({ order: 1, label: 1 })
.lean();
agentCategorySchema.statics.getActiveCategories = function () {
return this.find({ isActive: true }).sort({ order: 1, label: 1 }).lean();
};
/**
* Get categories with agent counts
* @returns {Promise<AgentCategory[]>} Categories with agent counts
*/
agentCategorySchema.statics.getCategoriesWithCounts = async function() {
const Agent = mongoose.model('agent');
agentCategorySchema.statics.getCategoriesWithCounts = async function () {
const Agent = mongoose.model('Agent');
// Aggregate to get agent counts per category
const categoryCounts = await Agent.aggregate([
{ $match: { category: { $exists: true, $ne: null } } },
{ $group: { _id: '$category', count: { $sum: 1 } } },
]);
// Create a map for quick lookup
const countMap = new Map(categoryCounts.map(c => [c._id, c.count]));
const countMap = new Map(categoryCounts.map((c) => [c._id, c.count]));
// Get all active categories and add counts
const categories = await this.getActiveCategories();
return categories.map(category => ({
return categories.map((category) => ({
...category,
agentCount: countMap.get(category.value) || 0,
}));
@ -91,16 +89,14 @@ agentCategorySchema.statics.getCategoriesWithCounts = async function() {
* Get valid category values for Agent model validation
* @returns {Promise<string[]>} Array of valid category values
*/
agentCategorySchema.statics.getValidCategoryValues = function() {
return this.find({ isActive: true })
.distinct('value')
.lean();
agentCategorySchema.statics.getValidCategoryValues = function () {
return this.find({ isActive: true }).distinct('value').lean();
};
/**
* Seed initial categories from existing constants
*/
agentCategorySchema.statics.seedCategories = async function(categories) {
agentCategorySchema.statics.seedCategories = async function (categories) {
const operations = categories.map((category, index) => ({
updateOne: {
filter: { value: category.value },
@ -116,10 +112,10 @@ agentCategorySchema.statics.seedCategories = async function(categories) {
upsert: true,
},
}));
return this.bulkWrite(operations);
};
const AgentCategory = mongoose.model('AgentCategory', agentCategorySchema);
module.exports = AgentCategory;
module.exports = AgentCategory;