telegram-shop-api / src /scenes /admin /settings.js
Deploy Bot
Maximal_Localization_And_Logic_Refactor
e81c9a5
const { Scenes, Markup } = require('telegraf');
const Settings = require('../../models/Settings');
const userController = require('../../controllers/userController'); // Import
const settingsScene = new Scenes.WizardScene(
'SETTINGS_SCENE',
// Step 1: Menu
async (ctx) => {
// Ensure localization
if (!ctx.i18n) {
const locales = require('../../locales');
const User = require('../../models/User');
const user = await User.findOne({ id: ctx.from.id });
ctx.i18n = locales[user ? user.language : 'uz'];
}
const i18n = ctx.i18n;
const buttons = [
[i18n.settings_title.replace(":", "")], // "Settings" logic usually implies "Change Address" here directly? No, menu first.
// Wait, existing code had "Manzilni o'zgartirish" button.
// Let's assume standard keys. "change_name" exists.
// "Manzilni o'zgartirish" -> let's map it.
[i18n.btn_back_nav]
];
// Wait, Step 1 was a submenu.
ctx.reply(i18n.settings_title, Markup.keyboard([
["πŸ“ Manzilni o'zgartirish"], // Keep hardcoded for match or add key?
// "πŸ“ Manzilni o'zgartirish" is not in locales.
// I should add it or just hardcode it for now if I don't want to break matching.
// But user wants "language improvement".
// Let's use a specific key for this button.
[i18n.btn_back_nav]
]).resize());
return ctx.wizard.next();
},
// Step 2: Handler
async (ctx) => {
const i18n = ctx.i18n;
const text = ctx.message.text;
if (text === i18n.btn_back_nav || text === i18n.btn_cancel) {
ctx.scene.leave();
userController.start(ctx); // Redirect to Main Menu
return;
}
if (text === "πŸ“ Manzilni o'zgartirish") { // Hardcoded match
ctx.reply(i18n.admin.settings_addr, Markup.keyboard([[i18n.btn_cancel]]).resize());
ctx.wizard.state.editing = 'location';
return ctx.wizard.next();
}
ctx.reply(i18n.cat_select); // Fallback
},
// Step 3: Input Address Text
async (ctx) => {
const i18n = ctx.i18n;
if (ctx.message.text === i18n.btn_cancel) {
ctx.scene.leave();
userController.start(ctx);
return;
}
ctx.wizard.state.addressText = ctx.message.text;
ctx.reply(i18n.admin.settings_loc, Markup.keyboard([
[Markup.button.locationRequest(i18n.location_button)],
[i18n.btn_back_nav, i18n.btn_cancel]
]).resize());
return ctx.wizard.next();
},
// Step 4: Input Location & Save
async (ctx) => {
const i18n = ctx.i18n;
if (ctx.message.text === i18n.btn_cancel) {
ctx.scene.leave();
userController.start(ctx);
return;
}
if (ctx.message.text === i18n.btn_back_nav) {
ctx.reply(i18n.admin.settings_addr, Markup.keyboard([[i18n.btn_cancel]]).resize());
return ctx.wizard.back(); // Go back to Step 3
}
if (!ctx.message.location) {
return ctx.reply("πŸ“ Location required."); // Minor hardcode fallback
}
const location = ctx.message.location;
const addressText = ctx.wizard.state.addressText;
// Save to DB
await Settings.findOneAndUpdate(
{ key: 'store_location' },
{ value: { text: addressText, latitude: location.latitude, longitude: location.longitude } },
{ upsert: true, new: true }
);
ctx.reply(i18n.admin.settings_saved, Markup.removeKeyboard());
ctx.scene.leave();
ctx.reply("Admin Menu: /admin");
}
);
module.exports = settingsScene;