Spaces:
Paused
Paused
Deploy Bot commited on
Commit Β·
5bc8558
1
Parent(s): 5370817
Add_Product_Counts_To_Category_Buttons
Browse files
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
|
| 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 |
-
//
|
| 147 |
return exports.showProducts(ctx, parentId);
|
| 148 |
}
|
| 149 |
|
| 150 |
-
const buttons =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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) });
|