Deploy Bot commited on
Commit
1e82281
Β·
1 Parent(s): 06e2bc3

Feat: Smart Product Condition Logic

Browse files
scripts/migrate_condition.js ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const mongoose = require('mongoose');
2
+ require('dotenv').config();
3
+
4
+ const MONGO_URI = process.env.MONGO_URI;
5
+
6
+ if (!MONGO_URI) {
7
+ console.error("❌ MONGO_URI is missing");
8
+ process.exit(1);
9
+ }
10
+
11
+ mongoose.connect(MONGO_URI)
12
+ .then(async () => {
13
+ console.log("βœ… MongoDB Connected");
14
+
15
+ const Product = require('./src/models/Product'); // Adjust path if needed
16
+
17
+ // 1. Update all products missing 'condition'
18
+ const result = await Product.updateMany(
19
+ { condition: { $exists: false } }, // Filter
20
+ { $set: { condition: 'new' } } // Update
21
+ );
22
+
23
+ console.log(`βœ… Migrated ${result.modifiedCount} products to 'new' condition.`);
24
+
25
+ process.exit(0);
26
+ })
27
+ .catch(err => {
28
+ console.error("❌ Error:", err);
29
+ process.exit(1);
30
+ });
src/controllers/userController.js CHANGED
@@ -189,20 +189,56 @@ exports.showSubCategories = async (ctx, parentId) => {
189
  }
190
  };
191
 
192
- // Show Products (Ask for Condition first)
193
  exports.showProducts = async (ctx, catId) => {
194
- const id = catId.replace('cat_', '');
195
- const buttons = [
196
- [
197
- Markup.button.callback("πŸ†• Yangi", `cond_${id}_new`),
198
- Markup.button.callback("♻️ B/U", `cond_${id}_used`)
199
- ],
200
- [Markup.button.callback("🌐 Barchasi", `cond_${id}_all`)],
201
- [Markup.button.callback(ctx.i18n.btn_back_nav || "⬅️ Orqaga", "back_to_cats")]
202
- ];
203
- // Try edit, fallback to reply
204
- await ctx.editMessageText("πŸ”Ž Qaysi turdagi mahsulotlarni ko'rmoqchisiz?", Markup.inlineKeyboard(buttons))
205
- .catch(() => ctx.reply("πŸ”Ž Qaysi turdagi mahsulotlarni ko'rmoqchisiz?", Markup.inlineKeyboard(buttons)));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
  };
207
 
208
  // Filter Handler
@@ -218,7 +254,14 @@ exports.browseCategory = async (ctx, catId, prodIndex, mediaIndex = 0, condition
218
  if (!category) return ctx.reply("Kategoriya topilmadi.");
219
 
220
  let query = { category: category.name };
221
- if (conditionFilter !== 'all') query.condition = conditionFilter;
 
 
 
 
 
 
 
222
 
223
  const products = await Product.find(query);
224
  if (!products || products.length === 0) return ctx.reply("❌ Bu kategoriyada mahsulotlar yo'q.");
 
189
  }
190
  };
191
 
192
+ // Show Products (Smart Condition Check)
193
  exports.showProducts = async (ctx, catId) => {
194
+ try {
195
+ const id = catId.replace('cat_', '');
196
+ const category = await Category.findOne({ id: id });
197
+ if (!category) return ctx.reply(ctx.i18n.no_cats);
198
+
199
+ // Smart Check: Count products
200
+ // We treat missing condition as 'new'
201
+ const countNew = await Product.countDocuments({
202
+ category: category.name,
203
+ $or: [{ condition: 'new' }, { condition: { $exists: false } }]
204
+ });
205
+ const countUsed = await Product.countDocuments({
206
+ category: category.name,
207
+ condition: 'used'
208
+ });
209
+
210
+ if (countNew === 0 && countUsed === 0) {
211
+ return ctx.reply("❌ Bu kategoriyada mahsulotlar yo'q.");
212
+ }
213
+
214
+ // If only NEW exist -> Show New directly
215
+ if (countUsed === 0) {
216
+ return exports.browseCategory(ctx, catId, 0, 0, 'new');
217
+ }
218
+
219
+ // If only USED exist -> Show Used directly
220
+ if (countNew === 0) {
221
+ return exports.browseCategory(ctx, catId, 0, 0, 'used');
222
+ }
223
+
224
+ // If BOTH exist -> Ask user
225
+ const buttons = [
226
+ [
227
+ Markup.button.callback(`πŸ†• Yangi (${countNew})`, `cond_${id}_new`),
228
+ Markup.button.callback(`♻️ B/U (${countUsed})`, `cond_${id}_used`)
229
+ ],
230
+ [Markup.button.callback(`🌐 Barchasi (${countNew + countUsed})`, `cond_${id}_all`)],
231
+ [Markup.button.callback(ctx.i18n.btn_back_nav || "⬅️ Orqaga", "back_to_cats")]
232
+ ];
233
+
234
+ // Try edit, fallback to reply
235
+ await ctx.editMessageText("πŸ”Ž Qaysi turdagi mahsulotlarni ko'rmoqchisiz?", Markup.inlineKeyboard(buttons))
236
+ .catch(() => ctx.reply("πŸ”Ž Qaysi turdagi mahsulotlarni ko'rmoqchisiz?", Markup.inlineKeyboard(buttons)));
237
+
238
+ } catch (e) {
239
+ console.error("Smart Filter Error:", e);
240
+ ctx.reply("Error in Catalog");
241
+ }
242
  };
243
 
244
  // Filter Handler
 
254
  if (!category) return ctx.reply("Kategoriya topilmadi.");
255
 
256
  let query = { category: category.name };
257
+
258
+ if (conditionFilter === 'new') {
259
+ // Treat missing as new
260
+ query.$or = [{ condition: 'new' }, { condition: { $exists: false } }];
261
+ } else if (conditionFilter === 'used') {
262
+ query.condition = 'used';
263
+ }
264
+ // If 'all', no condition filter needed
265
 
266
  const products = await Product.find(query);
267
  if (!products || products.length === 0) return ctx.reply("❌ Bu kategoriyada mahsulotlar yo'q.");