ibrohm commited on
Commit
2292087
Β·
1 Parent(s): c23d5cf

Backend: added Setting model and Store Location management via bot

Browse files
Files changed (3) hide show
  1. bot/bot.js +31 -2
  2. index.js +15 -0
  3. models/Setting.js +15 -0
bot/bot.js CHANGED
@@ -153,8 +153,8 @@ const getMainMenu = () => {
153
  [Markup.button.callback('πŸ“ Kategoriyalar', 'action_categories'), Markup.button.callback('πŸ” Qidirish', 'action_search')],
154
  [Markup.button.callback('πŸ“‹ Barcha Buyurtmalar', 'action_orders_0'), Markup.button.callback('πŸ”Ž Turniket (Chek O\'qish)', 'action_search_order')],
155
  [Markup.button.callback('πŸ“Š Statistika', 'action_stats'), Markup.button.callback('🏞 Reklamalar', 'action_banners')],
156
- [Markup.button.callback('πŸ“₯ Excel yuklash', 'action_export_excel'), Markup.button.callback('πŸ“’ Push Xabar', 'action_send_push')],
157
- [Markup.button.callback('❓ Yordam', 'action_help')]
158
  ]);
159
  };
160
 
@@ -1357,6 +1357,35 @@ if (process.env.BOT_TOKEN && process.env.BOT_TOKEN !== 'YOUR_TELEGRAM_BOT_TOKEN'
1357
  ctx.reply('πŸ“’ *Xabarnoma yuborish (Push)*\n\n1-qadam: Xabar sarlavhasini yozing (Masalan: 🚨 YANGI CHEGIRMA!):', { parse_mode: 'Markdown', reply_markup: { force_reply: true } });
1358
  });
1359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1360
  // Banner wizard β€” Havola tanlash callbacklari
1361
  bot.action('bwiz_link_none', async (ctx) => {
1362
  const state = appState[ctx.from.id];
 
153
  [Markup.button.callback('πŸ“ Kategoriyalar', 'action_categories'), Markup.button.callback('πŸ” Qidirish', 'action_search')],
154
  [Markup.button.callback('πŸ“‹ Barcha Buyurtmalar', 'action_orders_0'), Markup.button.callback('πŸ”Ž Turniket (Chek O\'qish)', 'action_search_order')],
155
  [Markup.button.callback('πŸ“Š Statistika', 'action_stats'), Markup.button.callback('🏞 Reklamalar', 'action_banners')],
156
+ [Markup.button.callback('πŸ“ Do\'kon Manzili', 'action_set_location'), Markup.button.callback('πŸ“₯ Excel', 'action_export_excel')],
157
+ [Markup.button.callback('πŸ“’ Push Xabar', 'action_send_push'), Markup.button.callback('❓ Yordam', 'action_help')]
158
  ]);
159
  };
160
 
 
1357
  ctx.reply('πŸ“’ *Xabarnoma yuborish (Push)*\n\n1-qadam: Xabar sarlavhasini yozing (Masalan: 🚨 YANGI CHEGIRMA!):', { parse_mode: 'Markdown', reply_markup: { force_reply: true } });
1358
  });
1359
 
1360
+ bot.action('action_set_location', (ctx) => {
1361
+ if (!isAdmin(ctx)) return;
1362
+ appState[ctx.from.id] = { step: 'set_store_location' };
1363
+ ctx.answerCbQuery();
1364
+ ctx.reply('πŸ“ *Do\'kon Manzilini o\'rnatish*\n\nTelegramning **πŸ“Ž Qistirish (Attachment)** tugmasini bosib, xaritadan **Location (Joylashuv)** yuboring.', { parse_mode: 'Markdown' });
1365
+ });
1366
+
1367
+ bot.on('location', async (ctx) => {
1368
+ const userId = ctx.from.id;
1369
+ if (!isAdmin(ctx)) return;
1370
+ const state = appState[userId];
1371
+ if (!state || state.step !== 'set_store_location') return;
1372
+
1373
+ const { latitude, longitude } = ctx.message.location;
1374
+
1375
+ try {
1376
+ const Setting = require('../models/Setting');
1377
+ await Setting.findOneAndUpdate(
1378
+ { key: 'store_location' },
1379
+ { value: { lat: latitude, lng: longitude } },
1380
+ { upsert: true }
1381
+ );
1382
+ ctx.reply(`βœ… Do'kon manzili muvaffaqiyatli saqlandi!\nπŸ“ Koordinatalar: ${latitude}, ${longitude}\n\nIlovadan xaridorlar ushbu xaritani endi bemalol navigator orqali ochishlari mumkin!`, getMainMenu());
1383
+ } catch (e) {
1384
+ ctx.reply('❌ Saqlashda xatolik yuz berdi: ' + e.message, getMainMenu());
1385
+ }
1386
+ delete appState[userId];
1387
+ });
1388
+
1389
  // Banner wizard β€” Havola tanlash callbacklari
1390
  bot.action('bwiz_link_none', async (ctx) => {
1391
  const state = appState[ctx.from.id];
index.js CHANGED
@@ -58,6 +58,21 @@ app.get('/api/banners', async (req, res) => {
58
  }
59
  });
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  // Barcha kategoriyalarni olish
62
  app.get('/api/categories', async (req, res) => {
63
  try {
 
58
  }
59
  });
60
 
61
+ // Sozlamalarni olish (masalan, store_location)
62
+ app.get('/api/settings/:key', async (req, res) => {
63
+ try {
64
+ const Setting = require('./models/Setting');
65
+ const setting = await Setting.findOne({ key: req.params.key });
66
+ if (setting) {
67
+ res.json(setting.value);
68
+ } else {
69
+ res.status(404).json({ message: 'Sozlama topilmadi' });
70
+ }
71
+ } catch (error) {
72
+ res.status(500).json({ message: error.message });
73
+ }
74
+ });
75
+
76
  // Barcha kategoriyalarni olish
77
  app.get('/api/categories', async (req, res) => {
78
  try {
models/Setting.js ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const mongoose = require('mongoose');
2
+
3
+ const settingSchema = new mongoose.Schema(
4
+ {
5
+ key: { type: String, required: true, unique: true },
6
+ value: { type: mongoose.Schema.Types.Mixed, required: true }
7
+ },
8
+ {
9
+ timestamps: true,
10
+ }
11
+ );
12
+
13
+ const Setting = mongoose.model('Setting', settingSchema);
14
+
15
+ module.exports = Setting;