Deploy Bot commited on
Commit
5bc8558
Β·
1 Parent(s): 5370817

Add_Product_Counts_To_Category_Buttons

Browse files
Files changed (1) hide show
  1. src/controllers/userController.js +8 -3
src/controllers/userController.js CHANGED
@@ -137,17 +137,22 @@ exports.showCategories = async (ctx) => {
137
  };
138
 
139
  // Show Subcategories (called when a parent category is clicked)
140
- exports.showSubCategories = async (ctx, parentId) => {
141
  try {
142
  const parent = await Category.findOne({ id: parentId });
143
  const subCats = await Category.find({ parent: parentId });
144
 
145
  if (!subCats || subCats.length === 0) {
146
- // Should not happen if logic is correct, but fallback to products
147
  return exports.showProducts(ctx, parentId);
148
  }
149
 
150
- const buttons = subCats.map(c => [Markup.button.callback(c.name, `cat_${c.id}`)]);
 
 
 
 
 
151
  buttons.push([Markup.button.callback("πŸ”™ Orqaga", "back_to_cats")]); // Back to Root
152
 
153
  await ctx.editMessageText(`πŸ“‚ **${parent.name}**\n\nIchki bo'limni tanlang:`, { parse_mode: 'Markdown', ...Markup.inlineKeyboard(buttons) });
 
137
  };
138
 
139
  // Show Subcategories (called when a parent category is clicked)
140
+ exports.showSubCategories = async (ctx) => {
141
  try {
142
  const parent = await Category.findOne({ id: parentId });
143
  const subCats = await Category.find({ parent: parentId });
144
 
145
  if (!subCats || subCats.length === 0) {
146
+ // Fallback to products if no subcategories
147
  return exports.showProducts(ctx, parentId);
148
  }
149
 
150
+ const buttons = [];
151
+ for (const c of subCats) {
152
+ const count = await Product.countDocuments({ category: c.id });
153
+ buttons.push([Markup.button.callback(`${c.name} (${count})`, `cat_${c.id}`)]);
154
+ }
155
+
156
  buttons.push([Markup.button.callback("πŸ”™ Orqaga", "back_to_cats")]); // Back to Root
157
 
158
  await ctx.editMessageText(`πŸ“‚ **${parent.name}**\n\nIchki bo'limni tanlang:`, { parse_mode: 'Markdown', ...Markup.inlineKeyboard(buttons) });