Spaces:
Paused
Paused
File size: 19,908 Bytes
89ec743 5b32161 89ec743 a549203 2cbca83 89ec743 5e459fe 89ec743 a549203 5e459fe 2cbca83 a549203 2cbca83 89ec743 2cbca83 89ec743 2cbca83 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 4fa4be7 89ec743 a549203 89ec743 a549203 89ec743 6517b12 2cbca83 6517b12 5e459fe 6517b12 89ec743 6517b12 89ec743 6517b12 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 5e459fe 89ec743 a549203 89ec743 a549203 89ec743 edea4a9 89ec743 2cd8edd 89ec743 4ab4872 89ec743 01aa324 fff265a 4ab4872 fff265a a549203 fff265a 01aa324 2fd57ac a549203 89ec743 a549203 89ec743 2cd8edd 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 2fd57ac a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 c04d99a a549203 c04d99a a549203 89ec743 c04d99a 1e66a57 c04d99a a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 c04d99a 1e66a57 c04d99a a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 89ec743 a549203 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | const { Scenes, Markup } = require('telegraf');
const Order = require('../../models/Order');
const Product = require('../../models/Product');
const User = require('../../models/User');
const config = require('../../config');
const userController = require('../../controllers/userController');
const formatPrice = (price) => {
return price.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
};
// Common Keyboard
const navKeyboard = (ctx, i18n, extras = []) => {
const back = i18n.btn_back_nav || "β¬
οΈ Back";
const cancel = i18n.btn_cancel || "β Cancel";
const k = [[back, cancel]];
if (extras.length) k.unshift(...extras);
return Markup.keyboard(k).resize().oneTime();
};
const checkNav = (ctx) => {
if (!ctx.message || !ctx.message.text) return null;
const text = ctx.message.text;
const i18n = ctx.i18n || require('../../locales').uz;
const btnCancel = i18n.btn_cancel || "β Cancel";
const btnBack = i18n.btn_back_nav || "β¬
οΈ Back";
// Robust checking against localised strings
if (text === btnCancel || text === 'β Bekor qilish' || text === 'β ΠΡΠΌΠ΅Π½Π°' || text === 'β Cancel') {
ctx.scene.leave();
ctx.reply(i18n.cancel_process || "Cancelled", Markup.removeKeyboard());
return 'STOP';
}
if (text === btnBack || text === 'β¬
οΈ Ortga' || text === 'β¬
οΈ ΠΠ°Π·Π°Π΄' || text === 'β¬
οΈ Back') {
return 'BACK';
}
return null;
};
async function finishOrder(ctx) {
try {
const state = ctx.wizard.state;
const cart = ctx.session.cart || [];
const i18n = ctx.i18n || require('../../locales').uz;
for (const item of cart) {
await Product.updateOne({ id: item.id }, { $inc: { quantity: -item.count } });
}
// Save Address if New and Delivery (Yandex)
if (state.deliveryMethod === i18n.delivery_yandex && state.isNewAddress && state.location) {
await User.updateOne(
{ id: ctx.from.id },
{ $push: { addresses: { name: `Manzil ${new Date().toLocaleDateString()}`, latitude: state.location.latitude, longitude: state.location.longitude } } }
);
}
const orderData = {
id: Date.now(),
userId: ctx.from.id,
user: state.fullName || ctx.from.first_name,
phone: state.phone,
deliveryMethod: state.deliveryMethod,
location: state.location,
addressText: state.addressText,
deliveryTime: state.deliveryTime || 'Standard',
comment: state.comment,
items: cart,
total: state.total,
paymentMethod: state.paymentMethod,
status: 'new'
};
const newOrder = new Order(orderData);
await newOrder.save();
// Update User Phone if new
await User.updateOne({ id: ctx.from.id }, { phone: state.phone });
ctx.reply(`${i18n.confirm_order}\n#${newOrder.id}\n${i18n.order_details_pay} ${state.paymentMethod}`, Markup.removeKeyboard());
// Notify Admins
let locInfo = "";
if (state.deliveryMethod === i18n.delivery_bts) {
locInfo = `π <b>BTS:</b>\n${state.addressText}\nπ€ <b>F.I.O:</b> ${state.fullName}`;
} else if (state.location) {
locInfo = `π <a href="https://www.google.com/maps?q=${state.location.latitude},${state.location.longitude}">Lokatsiya</a>`;
} else {
locInfo = "π Olib ketish";
}
const adminText = `π <b>Yangi buyurtma!</b>\n#${newOrder.id}\nπ€: ${newOrder.user}\nπ: ${newOrder.phone}\nπ: ${state.deliveryMethod}\n${locInfo}\nπ: ${state.deliveryTime || '-'}\nπ¬: ${state.comment || "Yo'q"}\nπ°: ${state.paymentMethod}\n\n` +
cart.map(item => `- ${item.name} (${item.count}x)`).join('\n') +
`\n\nJami: ${formatPrice(newOrder.total)} ${i18n.currency}`;
const btns = Markup.inlineKeyboard([Markup.button.callback("π Ko'rish", `admin_order_${newOrder.id}`)]);
for (const adminId of config.ADMIN_IDS) {
try {
await ctx.telegram.sendMessage(adminId, adminText, { parse_mode: 'HTML', ...btns });
if (state.location) await ctx.telegram.sendLocation(adminId, state.location.latitude, state.location.longitude);
if (state.paymentProof) await ctx.telegram.sendPhoto(adminId, state.paymentProof, { caption: `π§Ύ Chek (#${newOrder.id})` });
} catch (e) { console.error(e); }
}
ctx.session.cart = [];
return ctx.scene.leave();
} catch (err) {
console.error("ORDER_CREATION_ERROR:", err);
// Handle Duplicate Key Error (Self-Healing Attempt)
if (err.code === 11000) {
ctx.reply("β οΈ Tizimda qisqa nosozlik (ID Duplicate). Iltimos, qayta 'Buyurtma berish' tugmasini bosing.");
// Ideally we retry, but for now asking user is safer than infinite loop
} else {
const errorMsg = (err.message || "").substring(0, 50);
ctx.reply(`β Tizim xatoligi yuz berdi: ${errorMsg}\n\nIltimos, adminga xabar bering.`);
// Notify admin about the crash
for (const adminId of config.ADMIN_IDS) {
ctx.telegram.sendMessage(adminId, `π¨ **CRITICAL CHECKOUT ERROR**:\nUser: ${ctx.from.id}\nError: ${err.message}`, { parse_mode: 'Markdown' }).catch(e => console.error(e));
}
}
return ctx.scene.leave();
}
}
const checkoutScene = new Scenes.WizardScene(
'CHECKOUT_SCENE',
// Step 0: Pre-flight Check & Phone
async (ctx) => {
const i18n = ctx.i18n || require('../../locales').uz;
const cart = ctx.session.cart || [];
ctx.wizard.state.i18n = i18n; // Save specifically for wizard reuse
// 1. Stock Validation
const conflicts = [];
for (const item of cart) {
const product = await Product.findOne({ id: item.id });
if (!product || product.quantity < item.count) {
conflicts.push(`${item.name} (${product ? product.quantity : 0})`);
}
}
if (conflicts.length > 0) {
ctx.reply(`β οΈ ${i18n.cart_stock_err.replace('{item}', conflicts.join(', ')).replace('{stock}', '...')} \n\nIltimos savatni yangilang.`);
ctx.scene.leave();
return; // Stop checkout
}
// 2. Phone Input
const user = await User.findOne({ id: ctx.from.id });
ctx.wizard.state.user = user;
// NUCLEAR DEFENSIVE FIX
const btnPhone = (i18n && i18n.phone_button) ? i18n.phone_button : "π± Raqamni yuborish";
const btnCancel = (i18n && i18n.btn_cancel) ? i18n.btn_cancel : "β Bekor qilish";
const msgText = (i18n && i18n.phone_prompt) ? i18n.phone_prompt : "Telefon raqamingizni yuboring:";
console.log("CHECKOUT_DEBUG: Initializing Step 0 with safe keys:", { btnPhone, btnCancel });
let keyboardRows = [];
// Add Saved Phone if exists
if (user && user.phone) {
const savedTemplate = (i18n && i18n.checkout_phone_saved) ? i18n.checkout_phone_saved : "π± Mening raqamim: {phone}";
const savedBtnText = savedTemplate.replace('{phone}', user.phone || "");
if (savedBtnText) keyboardRows.push([Markup.button.text(savedBtnText)]);
}
// Add Contact Request Button
if (btnPhone) keyboardRows.push([Markup.button.contactRequest(btnPhone)]);
// Add Cancel Button
if (btnCancel) keyboardRows.push([Markup.button.text(btnCancel)]);
// Filter any potentially empty rows just in case
keyboardRows = keyboardRows.filter(row => row && row.length > 0 && row[0]);
try {
ctx.reply(msgText, Markup.keyboard(keyboardRows).resize().oneTime());
} catch (e) {
console.error("CHECKOUT_DEBUG_CRASH:", e);
ctx.reply("Checkout Error (Keyboard). Type /start");
}
return ctx.wizard.next();
},
// Step 1: Handle Phone -> Delivery
(ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
let phone = '';
if (ctx.message.contact) {
phone = ctx.message.contact.phone_number;
} else if (ctx.message.text) {
// Check if user clicked "My Saved Phone"
const savedText = i18n.checkout_phone_saved.replace('{phone}', '');
if (ctx.message.text.includes(savedText.trim()) && ctx.wizard.state.user && ctx.wizard.state.user.phone) {
phone = ctx.wizard.state.user.phone;
} else {
if (!/^\+?[0-9]{9,15}$/.test(ctx.message.text.replace(/\s/g, ''))) {
ctx.reply(i18n.error_phone, navKeyboard(ctx, i18n));
return;
}
phone = ctx.message.text;
}
} else {
ctx.reply(i18n.phone_prompt, navKeyboard(ctx, i18n)); return;
}
ctx.wizard.state.phone = phone;
ctx.reply(i18n.delivery_select, Markup.keyboard([
[i18n.delivery_yandex, i18n.delivery_bts],
[i18n.delivery_pickup],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize().oneTime());
return ctx.wizard.next();
},
// Step 2: Handle Delivery Selection
async (ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') {
ctx.wizard.selectStep(0);
return await ctx.wizard.steps[0](ctx);
}
const text = ctx.message.text.trim(); // Trim input
ctx.wizard.state.deliveryMethod = text;
if (text.includes('BTS') || text.includes('ΠΠΎΡΡΠ°')) {
ctx.wizard.state.deliveryMethod = i18n.delivery_bts;
ctx.reply("π " + i18n.settings_addr, navKeyboard(ctx, i18n));
return ctx.wizard.next(); // Step 3
}
if (text.includes('Yandex') || text.includes('Taxi') || text.includes('Π’Π°ΠΊΡΠΈ')) {
ctx.wizard.state.deliveryMethod = i18n.delivery_yandex;
// Yandex Explicit Check
const user = ctx.wizard.state.user;
const btns = [];
if (user && user.addresses && user.addresses.length > 0) {
user.addresses.forEach((addr, i) => {
btns.push([i18n.checkout_location_saved.replace('{name}', addr.name || 'Saved Address')]);
});
}
btns.push([Markup.button.locationRequest(i18n.location_button)]);
btns.push([i18n.btn_back_nav, i18n.btn_cancel]);
ctx.reply(i18n.address_prompt, Markup.keyboard(btns).resize().oneTime());
return ctx.wizard.selectStep(5); // Step 5
}
// Pickup Check (Robust)
if (text.includes('Olib ketish') || text.includes('Π‘Π°ΠΌΠΎΠ²ΡΠ²ΠΎΠ·') || text.includes('Pickup')) {
ctx.wizard.state.deliveryMethod = i18n.delivery_pickup;
ctx.wizard.state.location = null;
const Settings = require('../../models/Settings');
const storeSettings = await Settings.findOne({ key: 'store_location' }); // Optimized query placement
if (storeSettings && storeSettings.value) {
const { text, latitude, longitude } = storeSettings.value;
await ctx.reply(`π **Bizning manzil:**\n${text}`, { parse_mode: 'Markdown' });
await ctx.replyWithLocation(latitude, longitude);
} else {
await ctx.reply("π **Bizning manzil:**\nToshkent sh... (Default)");
}
ctx.reply("π Iltimos, vaqtni yozing (Masalan: 14:30):", Markup.keyboard([
["π
Bugun", "π
Ertaga"],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize().oneTime());
return ctx.wizard.selectStep(6);
}
// Unknown Input
ctx.reply(i18n.delivery_select, Markup.keyboard([
[i18n.delivery_yandex, i18n.delivery_bts],
[i18n.delivery_pickup],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize().oneTime());
return; // Stay in step
},
// Step 3: BTS Address
(ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') {
ctx.reply(i18n.delivery_select, Markup.keyboard([
[i18n.delivery_yandex, i18n.delivery_bts],
[i18n.delivery_pickup],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize().oneTime());
return ctx.wizard.selectStep(2);
}
ctx.wizard.state.addressText = ctx.message.text;
ctx.reply("π€ F.I.O (To'liq):", navKeyboard(ctx, i18n));
return ctx.wizard.next();
},
// Step 4: BTS Name
(ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') return ctx.wizard.selectStep(3);
ctx.wizard.state.fullName = ctx.message.text;
ctx.wizard.state.paymentMethod = i18n.payment_card; // BTS is usually Card only logic from previous Code
ctx.reply("π Izoh (Comment)?", Markup.keyboard([
["Yo'q"],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize());
return ctx.wizard.selectStep(7);
},
// Step 5: Yandex Address
async (ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') {
ctx.reply(i18n.delivery_select, Markup.keyboard([
[i18n.delivery_yandex, i18n.delivery_bts],
[i18n.delivery_pickup],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize().oneTime());
return ctx.wizard.selectStep(2);
}
let location = null;
if (ctx.message.location) {
location = ctx.message.location;
ctx.wizard.state.isNewAddress = true;
} else if (ctx.message.text) {
// Find in saved
const user = ctx.wizard.state.user;
if (user && user.addresses) {
// Fuzzy match or exact match depending on format
// We formatted buttons as: i18n.checkout_location_saved.replace('{name}', addr.name)
// Let's try to match by name
const savedFormat = i18n.checkout_location_saved.replace('{name}', '');
const name = ctx.message.text.replace(savedFormat, '').trim();
// This might be tricky if "Saved: " is empty.
// Simple loop match
const found = user.addresses.find(a => ctx.message.text.includes(a.name));
if (found) location = { latitude: found.latitude, longitude: found.longitude };
}
}
if (!location) {
ctx.reply("β οΈ " + i18n.settings_loc, navKeyboard(ctx, i18n));
return;
}
ctx.wizard.state.location = location;
ctx.reply("π Iltimos, vaqtni yozing (Masalan: 15:00):", Markup.keyboard([
["π
Bugun", "π
Ertaga"],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize().oneTime());
return ctx.wizard.next(); // To Step 6
},
// Step 6: Time
(ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') {
// Logic to go back to Address
return ctx.wizard.selectStep(5);
}
ctx.wizard.state.deliveryTime = ctx.message.text;
ctx.reply("π Izoh (Comment)?", Markup.keyboard([
["Yo'q"],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize());
return ctx.wizard.next();
},
// Step 7: Comment & Total Calc
(ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') return ctx.wizard.selectStep(6);
const text = ctx.message.text;
ctx.wizard.state.comment = text === "Yo'q" ? "" : text;
const cart = ctx.session.cart || [];
const total = cart.reduce((acc, item) => acc + (item.price * item.count), 0);
ctx.wizard.state.total = total;
// BTS is strictly Card (handled via pre-set or just flow)
if (ctx.wizard.state.deliveryMethod === i18n.delivery_bts) {
// BTS flow usually requires Card payment proof, so we treat it same as Card
// But existing code jumped to Step 9 directly?
// Line 358 in original code: return ctx.wizard.selectStep(9);
// We keep that behavior or merge it.
// Original: 358: if (ctx.wizard.state.deliveryMethod === i18n.delivery_bts) ... selectStep(9)
// Let's keep existing BTS logic if it was working, but here we are replacing the block.
// The original code handled BTS specifically at line 358.
// Let's replicate that logic first.
ctx.reply(`π¨ **BTS Pay**\n\n8600 ...`, navKeyboard(ctx, i18n));
return ctx.wizard.selectStep(9);
}
// Conditional Payment Buttons
let paymentButtons = [];
if (ctx.wizard.state.deliveryMethod === i18n.delivery_pickup) {
// Pickup -> Cash AND Card
paymentButtons.push([i18n.payment_cash, i18n.payment_card]);
} else {
// Yandex (Delivery) -> Card Only
paymentButtons.push([i18n.payment_card]);
}
paymentButtons.push([i18n.btn_back_nav, i18n.btn_cancel]);
ctx.reply(i18n.payment_select, Markup.keyboard(paymentButtons).resize().oneTime());
return ctx.wizard.next();
},
// Step 8: Payment Method
async (ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') return ctx.wizard.selectStep(7);
const choice = ctx.message.text;
// Validation: Verify if choice is allowed for current delivery method
// Pickup Logic: Allow both Cash and Card (Relaxed)
// Delivery Logic: Strict Card Only
if (ctx.wizard.state.deliveryMethod === i18n.delivery_yandex && choice !== i18n.payment_card) {
return ctx.reply("β οΈ Yetkazib berish uchun faqat Karta orqali to'lov mavjud.", Markup.keyboard([[i18n.payment_card], [i18n.btn_back_nav, i18n.btn_cancel]]).resize());
}
if (choice === i18n.payment_cash) {
ctx.wizard.state.paymentMethod = 'Naqd';
return await finishOrder(ctx);
} else if (choice === i18n.payment_card) {
ctx.wizard.state.paymentMethod = 'Karta';
ctx.reply(`π³ **Humo:** 9860 ...\n\nChek yuboring:`, navKeyboard(ctx, i18n));
return ctx.wizard.next();
} else {
ctx.reply("Tanlang.");
}
},
// Step 9: Proof
async (ctx) => {
const i18n = ctx.wizard.state.i18n || ctx.i18n;
const nav = checkNav(ctx);
if (nav === 'STOP') return;
if (nav === 'BACK') return ctx.wizard.selectStep(8);
if (ctx.message.photo) {
ctx.wizard.state.paymentProof = ctx.message.photo[ctx.message.photo.length - 1].file_id;
return await finishOrder(ctx);
} else {
ctx.reply("β οΈ Chek rasmini yuboring.");
}
}
);
module.exports = checkoutScene;
|