Spaces:
Paused
Paused
Deploy Bot commited on
Commit Β·
1e82281
1
Parent(s): 06e2bc3
Feat: Smart Product Condition Logic
Browse files- scripts/migrate_condition.js +30 -0
- src/controllers/userController.js +57 -14
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 (
|
| 193 |
exports.showProducts = async (ctx, catId) => {
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.");
|