Spaces:
Paused
Paused
| 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; | |