Spaces:
Paused
Paused
File size: 7,729 Bytes
89ec743 aff1ffd 89ec743 aff1ffd 89ec743 253ca85 89ec743 253ca85 89ec743 aff1ffd 89ec743 253ca85 89ec743 253ca85 89ec743 aff1ffd 89ec743 aff1ffd 89ec743 253ca85 89ec743 253ca85 89ec743 aff1ffd 89ec743 aff1ffd 89ec743 aff1ffd 89ec743 aff1ffd 89ec743 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | const { Scenes, Markup } = require('telegraf');
const Product = require('../../models/Product');
const Category = require('../../models/Category');
const userController = require('../../controllers/userController');
// Helper to show confirmation
const askConfirmation = (ctx, field, value, product) => {
let displayValue = value;
if (field === 'media') displayValue = `${value.length} ta yangi fayl`;
ctx.reply(`β οΈ <b>Tasdiqlaysizmi?</b>\n\nMaydon: ${field}\nEski: ${field === 'media' ? 'Media fayllar' : (product[field] || 'Yo\'q')}\nYangi: ${displayValue}`, {
parse_mode: 'HTML',
...Markup.keyboard([['β
Ha', 'β Bekor qilish']]).resize().oneTime()
});
};
const editProductScene = new Scenes.WizardScene(
'EDIT_PRODUCT_SCENE',
// Step 0: Ask what to edit (Index 0)
async (ctx) => {
const prodId = ctx.wizard.state.prodId;
if (!prodId) return ctx.scene.leave();
const product = await Product.findOne({ id: prodId });
if (!product) {
ctx.reply("Mahsulot topilmadi.");
return ctx.scene.leave();
}
ctx.wizard.state.product = product;
ctx.reply(`βοΈ <b>${product.name || 'Nomsiz'}</b> ni tahrirlash.\nMavjud: ${product.quantity} ta\nHolati: ${product.condition === 'used' ? 'β»οΈ B/U' : 'π Yangi'}\n\nQaysi ma'lumotni o'zgartiramiz?`, {
parse_mode: 'HTML',
...Markup.keyboard([
['π Nomini', 'π° Narxini'],
['π’ Sonini', 'π Tavsifni'],
['π Kategoriyani', 'πΈ Rasmni'],
['π/β»οΈ Holatini'], // New Button
['β Bekor qilish']
]).oneTime().resize()
});
return ctx.wizard.next();
},
// Step 1: Handle selection (Index 1)
async (ctx) => {
const text = ctx.message.text;
if (text === 'β Bekor qilish' || text === '/start') {
ctx.scene.leave();
userController.start(ctx, "Tahrirlash bekor qilindi.");
return;
}
let field = '';
if (text === 'π Nomini') field = 'name';
else if (text === 'π° Narxini') field = 'price';
else if (text === 'π’ Sonini') field = 'quantity';
else if (text === 'π Tavsifni') field = 'description';
else if (text === 'π Kategoriyani') field = 'category';
else if (text === 'πΈ Rasmni') field = 'media';
else if (text === 'π/β»οΈ Holatini') field = 'condition'; // Handler
else {
ctx.reply("Iltimos, tugmalardan birini tanlang.");
return;
}
ctx.wizard.state.field = field;
if (field === 'category') {
const categories = await Category.find();
const buttons = categories.map(c => c.name);
buttons.push('β Bekor qilish');
ctx.reply("Yangi kategoriyani tanlang:", Markup.keyboard(buttons).resize());
return ctx.wizard.selectStep(3); // Go to Category Handler
} else if (field === 'media') {
ctx.wizard.state.newMedia = [];
ctx.reply("πΈ Yangi rasmlarni yuboring (Maksimal 4 ta).\nEski rasmlar o'chib ketadi.\n\nBarchasini yuborib bo'lgach, 'β
Tayyor' tugmasini bosing.", Markup.keyboard(['β
Tayyor', 'β Bekor qilish']).resize());
return ctx.wizard.selectStep(4); // Go to Media Handler
} else if (field === 'condition') {
ctx.reply("Yangi holatni tanlang:", Markup.keyboard([['π Yangi', 'β»οΈ B/U'], ['β Bekor qilish']]).resize());
return ctx.wizard.selectStep(2); // Go to Value Input (handled as text)
}
ctx.reply(`Yangi ${text.toLowerCase()} kiriting:`, Markup.keyboard([['β Bekor qilish']]).resize());
return ctx.wizard.next();
},
// Step 2: Handle Text/Number Input (Index 2)
async (ctx) => {
const newValue = ctx.message.text;
if (!newValue || newValue === 'β Bekor qilish') {
ctx.scene.leave();
userController.start(ctx, "Bekor qilindi.");
return;
}
if (newValue.startsWith('/')) {
ctx.scene.leave();
userController.start(ctx);
return;
}
if (ctx.wizard.state.field === 'price' || ctx.wizard.state.field === 'quantity') {
if (isNaN(newValue)) return ctx.reply("Iltimos, raqam kiriting.");
}
// Map Condition Text to Value
if (ctx.wizard.state.field === 'condition') {
if (newValue === 'π Yangi') ctx.wizard.state.newValue = 'new';
else if (newValue === 'β»οΈ B/U') ctx.wizard.state.newValue = 'used';
else return ctx.reply("Iltimos, tugmani tanlang: Yangi yoki B/U");
} else {
ctx.wizard.state.newValue = newValue;
}
// Ask Confirmation HERE
askConfirmation(ctx, ctx.wizard.state.field, ctx.wizard.state.newValue, ctx.wizard.state.product);
return ctx.wizard.selectStep(5); // Go to Confirmation Handler
},
// Step 3: Handle Category Input (Index 3)
async (ctx) => {
const tex = ctx.message.text;
if (!tex || tex === 'β Bekor qilish' || tex.startsWith('/')) {
ctx.scene.leave();
userController.start(ctx, "Bekor qilindi.");
return;
}
ctx.wizard.state.newValue = tex;
askConfirmation(ctx, 'category', tex, ctx.wizard.state.product);
return ctx.wizard.selectStep(5);
},
// Step 4: Handle Media Input (Index 4)
async (ctx) => {
const msg = ctx.message;
if (msg.text === 'β Bekor qilish' || (msg.text && msg.text.startsWith('/'))) {
ctx.scene.leave();
userController.start(ctx, "Bekor qilindi.");
return;
}
if (msg.text === 'β
Tayyor') {
if (ctx.wizard.state.newMedia.length === 0) return ctx.reply("Kamida bitta rasm yuboring.");
ctx.wizard.state.newValue = ctx.wizard.state.newMedia;
askConfirmation(ctx, 'media', ctx.wizard.state.newMedia, ctx.wizard.state.product);
return ctx.wizard.selectStep(5);
}
if (msg.photo) {
ctx.wizard.state.newMedia.push({ type: 'photo', file_id: msg.photo[msg.photo.length - 1].file_id });
ctx.reply(`Rasm qabul qilindi (${ctx.wizard.state.newMedia.length}/4)`);
} else if (msg.video) {
ctx.wizard.state.newMedia.push({ type: 'video', file_id: msg.video.file_id });
ctx.reply(`Video qabul qilindi (${ctx.wizard.state.newMedia.length}/4)`);
}
if (ctx.wizard.state.newMedia.length >= 4) {
ctx.wizard.state.newValue = ctx.wizard.state.newMedia;
askConfirmation(ctx, 'media', ctx.wizard.state.newMedia, ctx.wizard.state.product);
return ctx.wizard.selectStep(5);
}
},
// Step 5: Handle Yes/No (Hander ONLY) (Index 5)
async (ctx) => {
if (ctx.message.text === 'β
Ha') {
try {
const update = {};
update[ctx.wizard.state.field] = ctx.wizard.state.newValue;
await Product.updateOne({ id: ctx.wizard.state.product.id }, update);
ctx.reply("β
Muvaffaqiyatli saqlandi!", Markup.removeKeyboard());
// Redirect to Main Menu
userController.start(ctx);
} catch (e) {
console.error(e);
ctx.reply("Xatolik bo'ldi.");
}
} else {
userController.start(ctx, "Bekor qilindi.");
}
return ctx.scene.leave();
}
);
module.exports = editProductScene;
|