refactor(arch): replace if/else router with Map-based dispatcher (#53)
- index.js: 394→69 lines (82% reduction) - callbackRouter.js (36 lines): Map-based dispatch with exact + prefix matching - Longest-prefix-first for specificity - Logs warning for unregistered callbacks - messageRouter.js (27 lines): Ordered input handlers + text command Map - routes.js (345 lines): All 59 callback routes + 9 text commands + 7 input handlers - Exact routes: 19 (add_wallet, back_to_balance, etc.) - Prefix routes: 40 (generate_wallet_, view_product_, etc.) - Admin text commands with isAdmin guard - Special cases: view_transaction_history_ page extraction - Input handler order preserved (location → category → import → edit → dump → bonus)
This commit is contained in:
349
src/index.js
349
src/index.js
@@ -1,39 +1,24 @@
|
||||
import { runMigrations, cleanUpInvalidForeignKeys } from './migrations/runner.js';
|
||||
import adminUserHandler from './handlers/adminHandlers/adminUserHandler.js';
|
||||
import './router/routes.js';
|
||||
import bot from './context/bot.js';
|
||||
import ErrorHandler from './utils/errorHandler.js';
|
||||
import bot from "./context/bot.js";
|
||||
import userHandler from './handlers/userHandlers/userHandler.js';
|
||||
import adminHandler from './handlers/adminHandlers/adminHandler.js';
|
||||
import callbackRouter from './router/callbackRouter.js';
|
||||
import messageRouter from './router/messageRouter.js';
|
||||
|
||||
await runMigrations();
|
||||
await cleanUpInvalidForeignKeys();
|
||||
import userHandler from "./handlers/userHandlers/userHandler.js";
|
||||
import userPurchaseHandler from "./handlers/userHandlers/userPurchaseHandler.js";
|
||||
import userLocationHandler from "./handlers/userHandlers/userLocationHandler.js";
|
||||
import userProductHandler from "./handlers/userHandlers/userProductHandler.js";
|
||||
import userWalletsHandler from "./handlers/userHandlers/wallet/index.js";
|
||||
import userDeletionHandler from "./handlers/userHandlers/userDeletionHandler.js";
|
||||
import adminHandler from "./handlers/adminHandlers/adminHandler.js";
|
||||
import adminUserLocationHandler from "./handlers/adminHandlers/adminUserLocationHandler.js";
|
||||
import adminDumpHandler from "./handlers/adminHandlers/adminDumpHandler.js";
|
||||
import adminLocationHandler from "./handlers/adminHandlers/adminLocationHandler.js";
|
||||
import productHandler from "./handlers/adminHandlers/product/index.js";
|
||||
import adminWalletsHandler from "./handlers/adminHandlers/adminWalletsHandler.js";
|
||||
|
||||
// Debug logging function
|
||||
const logDebug = (action, functionName) => {
|
||||
console.log(`[DEBUG] Button Press: ${action}`);
|
||||
console.log(`[DEBUG] Calling Function: ${functionName}`);
|
||||
};
|
||||
|
||||
// Start command - Create user profile
|
||||
bot.onText(/\/start/, async (msg) => {
|
||||
logDebug('/start', 'handleStart');
|
||||
|
||||
const canUse = await userHandler.canUseBot(msg);
|
||||
|
||||
if (!canUse) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!canUse) return;
|
||||
try {
|
||||
await userHandler.handleStart(msg);
|
||||
} catch (error) {
|
||||
@@ -41,7 +26,6 @@ bot.onText(/\/start/, async (msg) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Admin command
|
||||
bot.onText(/\/admin/, async (msg) => {
|
||||
logDebug('/admin', 'handleAdminCommand');
|
||||
try {
|
||||
@@ -51,340 +35,31 @@ bot.onText(/\/admin/, async (msg) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Handle user menu buttons
|
||||
bot.on('message', async (msg) => {
|
||||
if (msg.text && msg.text.toLowerCase() === '/start') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.text?.toLowerCase() === '/start') return;
|
||||
const canUse = await userHandler.canUseBot(msg);
|
||||
|
||||
if (!canUse) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!canUse) return;
|
||||
try {
|
||||
// Check for admin location input
|
||||
if (await adminLocationHandler.handleLocationInput(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for admin category input
|
||||
if (await productHandler.handleCategoryInput(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for product import
|
||||
if (await productHandler.handleProductImport(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for product edition
|
||||
if (await productHandler.handleProductEditImport(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for database dump import
|
||||
if (await adminDumpHandler.handleDumpImport(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for bonus balance input
|
||||
if (await adminUserHandler.handleBonusBalanceInput(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for category update input
|
||||
if (await productHandler.handleCategoryUpdate(msg)) {
|
||||
return;
|
||||
}
|
||||
|
||||
logDebug(msg.text, 'handleMessage');
|
||||
|
||||
switch (msg.text) {
|
||||
case '📦 Products':
|
||||
await userProductHandler.showProducts(msg);
|
||||
break;
|
||||
case '👤 Profile':
|
||||
await userHandler.showProfile(msg);
|
||||
break;
|
||||
case '💰 Wallets':
|
||||
await userWalletsHandler.showBalance(msg);
|
||||
break;
|
||||
case '🛍 Purchases':
|
||||
await userPurchaseHandler.showPurchases(msg);
|
||||
break;
|
||||
case '📦 Manage Products':
|
||||
if (adminHandler.isAdmin(msg.from.id)) {
|
||||
await productHandler.handleProductManagement(msg);
|
||||
}
|
||||
break;
|
||||
case '👥 Manage Users':
|
||||
if (adminHandler.isAdmin(msg.from.id)) {
|
||||
await adminUserHandler.handleUserList(msg);
|
||||
}
|
||||
break;
|
||||
case '📍 Manage Locations':
|
||||
if (adminHandler.isAdmin(msg.from.id)) {
|
||||
await adminLocationHandler.handleViewLocations(msg);
|
||||
}
|
||||
break;
|
||||
case '💾 Database Backup':
|
||||
if (adminHandler.isAdmin(msg.from.id)) {
|
||||
await adminDumpHandler.handleDump(msg);
|
||||
}
|
||||
break;
|
||||
case '💰 Manage Wallets':
|
||||
if (adminHandler.isAdmin(msg.from.id)) {
|
||||
await adminWalletsHandler.handleWalletManagement(msg);
|
||||
}
|
||||
break;
|
||||
}
|
||||
await messageRouter.dispatch(msg);
|
||||
} catch (error) {
|
||||
await ErrorHandler.handleError(bot, msg.chat.id, error, 'message handler');
|
||||
}
|
||||
});
|
||||
|
||||
// Handle callback queries
|
||||
bot.on('callback_query', async (callbackQuery) => {
|
||||
const action = callbackQuery.data;
|
||||
const msg = callbackQuery.message;
|
||||
|
||||
const canUse = await userHandler.canUseBot(callbackQuery);
|
||||
|
||||
if (!canUse) {
|
||||
await bot.answerCallbackQuery(callbackQuery.id);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Profile and location management
|
||||
if (action === 'set_location') {
|
||||
logDebug(action, 'handleSetLocation');
|
||||
await userLocationHandler.handleSetLocation(callbackQuery);
|
||||
} else if (action.startsWith('set_country_')) {
|
||||
logDebug(action, 'handleSetCountry');
|
||||
await userLocationHandler.handleSetCountry(callbackQuery);
|
||||
} else if (action.startsWith('set_city_')) {
|
||||
logDebug(action, 'handleSetCity');
|
||||
await userLocationHandler.handleSetCity(callbackQuery);
|
||||
} else if (action.startsWith('set_district_')) {
|
||||
logDebug(action, 'handleSetDistrict');
|
||||
await userLocationHandler.handleSetDistrict(callbackQuery);
|
||||
} else if (action === 'back_to_profile') {
|
||||
logDebug(action, 'handleBackToProfile');
|
||||
await userHandler.handleBackToProfile(callbackQuery);
|
||||
} else if (action === 'back_to_balance') {
|
||||
logDebug(action, 'handleBackToBalance');
|
||||
await userWalletsHandler.handleBackToBalance(callbackQuery);
|
||||
} else if (action === 'delete_account') {
|
||||
logDebug(action, 'handleDeleteAccount');
|
||||
await userDeletionHandler.handleDeleteAccount(callbackQuery);
|
||||
} else if (action === 'confirm_delete_account') {
|
||||
logDebug(action, 'handleConfirmDelete');
|
||||
await userDeletionHandler.handleConfirmDelete(callbackQuery);
|
||||
}
|
||||
// Wallet management
|
||||
else if (action === 'add_wallet') {
|
||||
logDebug(action, 'handleAddWallet');
|
||||
await userWalletsHandler.handleAddWallet(callbackQuery);
|
||||
} else if (action === 'top_up_wallet') {
|
||||
logDebug(action, 'handleTopUpWallet');
|
||||
await userWalletsHandler.handleTopUpWallet(callbackQuery);
|
||||
} else if (action === 'wallet_history') {
|
||||
logDebug(action, 'handleWalletHistory');
|
||||
await userWalletsHandler.handleWalletHistory(callbackQuery);
|
||||
} else if (action === 'view_archived_wallets') {
|
||||
logDebug(action, 'handleViewArchivedWallets');
|
||||
await userWalletsHandler.handleViewArchivedWallets(callbackQuery);
|
||||
} else if (action === 'refresh_balance') {
|
||||
logDebug(action, 'handleRefreshBalance');
|
||||
await userWalletsHandler.handleRefreshBalance(callbackQuery);
|
||||
}
|
||||
// Wallet generation
|
||||
else if (action.startsWith('generate_wallet_')) {
|
||||
logDebug(action, 'handleGenerateWallet');
|
||||
await userWalletsHandler.handleGenerateWallet(callbackQuery);
|
||||
}
|
||||
// Shop navigation
|
||||
else if (action === 'shop_start') {
|
||||
logDebug(action, 'showProducts');
|
||||
await userProductHandler.showProducts(msg);
|
||||
} else if (action.startsWith('shop_country_')) {
|
||||
logDebug(action, 'handleCountrySelection');
|
||||
await userProductHandler.handleCountrySelection(callbackQuery);
|
||||
} else if (action.startsWith('shop_city_')) {
|
||||
logDebug(action, 'handleCitySelection');
|
||||
await userProductHandler.handleCitySelection(callbackQuery);
|
||||
} else if (action.startsWith('shop_district_')) {
|
||||
logDebug(action, 'handleDistrictSelection');
|
||||
await userProductHandler.handleDistrictSelection(callbackQuery);
|
||||
} else if (action.startsWith('shop_category_')) {
|
||||
logDebug(action, 'handleCategorySelection');
|
||||
await userProductHandler.handleCategorySelection(callbackQuery);
|
||||
} else if (action.startsWith('shop_product_')) {
|
||||
logDebug(action, 'handleProductSelection');
|
||||
await userProductHandler.handleProductSelection(callbackQuery);
|
||||
} else if (action.startsWith('increase_quantity_')) {
|
||||
logDebug(action, 'handleIncreaseQuantity');
|
||||
await userProductHandler.handleIncreaseQuantity(callbackQuery);
|
||||
} else if (action.startsWith('decrease_quantity_')) {
|
||||
logDebug(action, 'handleDecreaseQuantity');
|
||||
await userProductHandler.handleDecreaseQuantity(callbackQuery);
|
||||
} else if (action.startsWith('buy_product_')) {
|
||||
logDebug(action, 'handleBuyProduct');
|
||||
await userProductHandler.handleBuyProduct(callbackQuery);
|
||||
} else if (action.startsWith('pay_with_')) {
|
||||
logDebug(action, 'handlePay');
|
||||
await userProductHandler.handlePay(callbackQuery);
|
||||
} else if (action.startsWith('list_purchases_')) {
|
||||
logDebug(action, 'handlePurchaseListPage');
|
||||
await userPurchaseHandler.handlePurchaseListPage(callbackQuery);
|
||||
} else if (action.startsWith('view_purchase_')) {
|
||||
logDebug(action, 'viewPurchase');
|
||||
await userPurchaseHandler.viewPurchase(callbackQuery);
|
||||
} else if (action.startsWith('confirm_received_')) {
|
||||
logDebug(action, 'handleConfirmReceived');
|
||||
await userPurchaseHandler.handleConfirmReceived(callbackQuery);
|
||||
}
|
||||
// Admin location management
|
||||
else if (action === 'add_location') {
|
||||
logDebug(action, 'handleAddLocation');
|
||||
await adminLocationHandler.handleAddLocation(callbackQuery);
|
||||
} else if (action === 'view_locations') {
|
||||
logDebug(action, 'handleViewLocations');
|
||||
await adminLocationHandler.handleViewLocations(callbackQuery);
|
||||
} else if (action === 'view_ip') {
|
||||
logDebug(action, 'handleViewIP');
|
||||
await adminLocationHandler.handleViewIP(callbackQuery);
|
||||
} else if (action === 'delete_location') {
|
||||
logDebug(action, 'handleDeleteLocation');
|
||||
await adminLocationHandler.handleDeleteLocation(callbackQuery);
|
||||
} else if (action.startsWith('confirm_delete_location_')) {
|
||||
logDebug(action, 'handleConfirmDelete');
|
||||
await adminLocationHandler.handleConfirmDelete(callbackQuery);
|
||||
} else if (action === 'admin_menu') {
|
||||
logDebug(action, 'backToMenu');
|
||||
await adminLocationHandler.backToMenu(callbackQuery);
|
||||
}
|
||||
|
||||
// Admin product management
|
||||
else if (action === 'manage_products') {
|
||||
logDebug(action, 'handleProductManagement');
|
||||
await productHandler.handleProductManagement(callbackQuery);
|
||||
} else if (action.startsWith('prod_country_')) {
|
||||
logDebug(action, 'handleCountrySelection');
|
||||
await productHandler.handleCountrySelection(callbackQuery);
|
||||
} else if (action.startsWith('prod_city_')) {
|
||||
logDebug(action, 'handleCitySelection');
|
||||
await productHandler.handleCitySelection(callbackQuery);
|
||||
} else if (action.startsWith('prod_district_')) {
|
||||
logDebug(action, 'handleDistrictSelection');
|
||||
await productHandler.handleDistrictSelection(callbackQuery);
|
||||
} else if (action.startsWith('add_category_')) {
|
||||
logDebug(action, 'handleAddCategory');
|
||||
await productHandler.handleAddCategory(callbackQuery);
|
||||
} else if (action.startsWith('edit_category_')) {
|
||||
logDebug(action, 'handleEditCategory');
|
||||
await productHandler.handleEditCategory(callbackQuery);
|
||||
} else if (action.startsWith('prod_category_')) {
|
||||
logDebug(action, 'handleCategorySelection');
|
||||
await productHandler.handleCategorySelection(callbackQuery);
|
||||
} else if (action.startsWith('list_products_')) {
|
||||
logDebug(action, 'handleProductListPage');
|
||||
await productHandler.handleProductListPage(callbackQuery);
|
||||
} else if (action.startsWith('add_product_')) {
|
||||
logDebug(action, 'handleAddProduct');
|
||||
await productHandler.handleAddProduct(callbackQuery);
|
||||
} else if (action.startsWith('view_product_')) {
|
||||
logDebug(action, 'handleViewProduct');
|
||||
await productHandler.handleViewProduct(callbackQuery);
|
||||
} else if (action.startsWith('edit_product_')) {
|
||||
logDebug(action, 'handleProductEdit');
|
||||
await productHandler.handleProductEdit(callbackQuery)
|
||||
} else if (action.startsWith('delete_product_')) {
|
||||
logDebug(action, 'handleProductDelete');
|
||||
await productHandler.handleProductDelete(callbackQuery);
|
||||
} else if (action.startsWith('confirm_delete_product_')) {
|
||||
logDebug(action, 'handleConfirmDelete');
|
||||
await productHandler.handleConfirmDelete(callbackQuery);
|
||||
}
|
||||
// Admin user management
|
||||
else if (action.startsWith('view_user_')) {
|
||||
logDebug(action, 'handleViewUser');
|
||||
await adminUserHandler.handleViewUser(callbackQuery);
|
||||
} else if (action.startsWith('list_users_')) {
|
||||
logDebug(action, 'handleUserListPage');
|
||||
await adminUserHandler.handleUserListPage(callbackQuery);
|
||||
} else if (action.startsWith('delete_user_')) {
|
||||
logDebug(action, 'handleDeleteUser');
|
||||
await adminUserHandler.handleDeleteUser(callbackQuery);
|
||||
} else if (action.startsWith('block_user_')) {
|
||||
logDebug(action, 'handleBlockUser');
|
||||
await adminUserHandler.handleBlockUser(callbackQuery);
|
||||
} else if (action.startsWith('confirm_delete_user_')) {
|
||||
logDebug(action, 'handleConfirmDelete');
|
||||
await adminUserHandler.handleConfirmDelete(callbackQuery);
|
||||
} else if (action.startsWith('confirm_block_user_')) {
|
||||
logDebug(action, 'handleConfirmBlock');
|
||||
await adminUserHandler.handleConfirmBlock(callbackQuery);
|
||||
} else if (action.startsWith('edit_user_balance_')) {
|
||||
logDebug(action, 'handleEditUserBalance');
|
||||
await adminUserHandler.handleEditUserBalance(callbackQuery);
|
||||
}
|
||||
// Admin users location management
|
||||
else if (action.startsWith('edit_user_location_')) {
|
||||
logDebug(action, 'handleEditUserLocation');
|
||||
await adminUserLocationHandler.handleEditUserLocation(callbackQuery);
|
||||
} else if (action.startsWith('edit_user_country_')) {
|
||||
logDebug(action, 'handleEditUserCountry');
|
||||
await adminUserLocationHandler.handleEditUserCountry(callbackQuery);
|
||||
} else if (action.startsWith('edit_user_city_')) {
|
||||
logDebug(action, 'handleEditUserCity');
|
||||
await adminUserLocationHandler.handleEditUserCity(callbackQuery);
|
||||
} else if (action.startsWith('edit_user_district_')) {
|
||||
logDebug(action, 'handleEditUserDistrict');
|
||||
await adminUserLocationHandler.handleEditUserDistrict(callbackQuery)
|
||||
}
|
||||
// Admin Wallet management
|
||||
else if (action.startsWith('wallet_type_')) { // Добавляем обработку выбора типа кошелька
|
||||
logDebug(action, 'handleWalletTypeSelection');
|
||||
await adminWalletsHandler.handleWalletTypeSelection(callbackQuery);
|
||||
} else if (action.startsWith('check_balance_')) {
|
||||
logDebug(action, 'handleCheckCommissionBalance');
|
||||
await adminWalletsHandler.handleCheckCommissionBalance(callbackQuery);
|
||||
} else if (action.startsWith('prev_page_') || action.startsWith('next_page_')) {
|
||||
logDebug(action, 'handlePagination');
|
||||
await adminWalletsHandler.handlePagination(callbackQuery);
|
||||
} else if (action.startsWith('export_csv_')) {
|
||||
logDebug(action, 'handleExportCSV');
|
||||
await adminWalletsHandler.handleExportCSV(callbackQuery);
|
||||
} else if (action === 'back_to_wallet_types') {
|
||||
logDebug(action, 'handleBackToWalletTypes');
|
||||
await adminWalletsHandler.handleBackToWalletTypes(callbackQuery);
|
||||
}
|
||||
// Dump manage
|
||||
else if (action === "export_database") {
|
||||
await adminDumpHandler.handleExportDatabase(callbackQuery);
|
||||
return;
|
||||
} else if (action === "import_database") {
|
||||
await adminDumpHandler.handleImportDatabase(callbackQuery);
|
||||
}
|
||||
|
||||
// Transaction history
|
||||
else if (action.startsWith('view_transaction_history_')) {
|
||||
logDebug(action, 'handleTransactionHistory');
|
||||
const page = parseInt(action.split('_').pop()); // Extract page number
|
||||
await userWalletsHandler.handleTransactionHistory(callbackQuery, page);
|
||||
}
|
||||
|
||||
await callbackRouter.dispatch(callbackQuery);
|
||||
await bot.answerCallbackQuery(callbackQuery.id);
|
||||
} catch (error) {
|
||||
await ErrorHandler.handleError(bot, msg.chat.id, error, 'callback query');
|
||||
await ErrorHandler.handleError(bot, callbackQuery.message.chat.id, error, 'callback query');
|
||||
}
|
||||
});
|
||||
|
||||
// Error handling
|
||||
bot.on('polling_error', ErrorHandler.handlePollingError);
|
||||
|
||||
process.on('unhandledRejection', (error) => {
|
||||
|
||||
36
src/router/callbackRouter.js
Normal file
36
src/router/callbackRouter.js
Normal file
@@ -0,0 +1,36 @@
|
||||
class CallbackRouter {
|
||||
constructor() {
|
||||
this.exactRoutes = new Map();
|
||||
this.prefixRoutes = new Map();
|
||||
}
|
||||
|
||||
registerExact(action, handler) {
|
||||
this.exactRoutes.set(action, handler);
|
||||
}
|
||||
|
||||
registerPrefix(prefix, handler) {
|
||||
this.prefixRoutes.set(prefix, handler);
|
||||
}
|
||||
|
||||
async dispatch(callbackQuery) {
|
||||
const action = callbackQuery.data;
|
||||
|
||||
const exactHandler = this.exactRoutes.get(action);
|
||||
if (exactHandler) {
|
||||
await exactHandler(callbackQuery);
|
||||
return;
|
||||
}
|
||||
|
||||
const prefixes = [...this.prefixRoutes.keys()].sort((a, b) => b.length - a.length);
|
||||
for (const prefix of prefixes) {
|
||||
if (action.startsWith(prefix)) {
|
||||
await this.prefixRoutes.get(prefix)(callbackQuery);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
console.warn(`No handler for callback: ${action}`);
|
||||
}
|
||||
}
|
||||
|
||||
export default new CallbackRouter();
|
||||
27
src/router/messageRouter.js
Normal file
27
src/router/messageRouter.js
Normal file
@@ -0,0 +1,27 @@
|
||||
class MessageRouter {
|
||||
constructor() {
|
||||
this.inputHandlers = [];
|
||||
this.textHandlers = new Map();
|
||||
}
|
||||
|
||||
registerInput(handler) {
|
||||
this.inputHandlers.push(handler);
|
||||
}
|
||||
|
||||
registerText(text, handler) {
|
||||
this.textHandlers.set(text, handler);
|
||||
}
|
||||
|
||||
async dispatch(msg) {
|
||||
for (const handler of this.inputHandlers) {
|
||||
if (await handler(msg)) return;
|
||||
}
|
||||
|
||||
if (msg.text && this.textHandlers.has(msg.text)) {
|
||||
await this.textHandlers.get(msg.text)(msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new MessageRouter();
|
||||
345
src/router/routes.js
Normal file
345
src/router/routes.js
Normal file
@@ -0,0 +1,345 @@
|
||||
import callbackRouter from './callbackRouter.js';
|
||||
import messageRouter from './messageRouter.js';
|
||||
import { isAdmin } from '../middleware/auth.js';
|
||||
|
||||
import userHandler from '../handlers/userHandlers/userHandler.js';
|
||||
import userPurchaseHandler from '../handlers/userHandlers/userPurchaseHandler.js';
|
||||
import userLocationHandler from '../handlers/userHandlers/userLocationHandler.js';
|
||||
import userProductHandler from '../handlers/userHandlers/userProductHandler.js';
|
||||
import userWalletsHandler from '../handlers/userHandlers/wallet/index.js';
|
||||
import userDeletionHandler from '../handlers/userHandlers/userDeletionHandler.js';
|
||||
import adminHandler from '../handlers/adminHandlers/adminHandler.js';
|
||||
import adminUserLocationHandler from '../handlers/adminHandlers/adminUserLocationHandler.js';
|
||||
import adminDumpHandler from '../handlers/adminHandlers/adminDumpHandler.js';
|
||||
import adminLocationHandler from '../handlers/adminHandlers/adminLocationHandler.js';
|
||||
import productHandler from '../handlers/adminHandlers/product/index.js';
|
||||
import adminWalletsHandler from '../handlers/adminHandlers/adminWalletsHandler.js';
|
||||
import adminUserHandler from '../handlers/adminHandlers/adminUserHandler.js';
|
||||
|
||||
const logDebug = (action, functionName) => {
|
||||
console.log(`[DEBUG] Button Press: ${action}`);
|
||||
console.log(`[DEBUG] Calling Function: ${functionName}`);
|
||||
};
|
||||
|
||||
export function registerRoutes() {
|
||||
// === Message Input Handlers (order matters — checked first) ===
|
||||
messageRouter.registerInput(adminLocationHandler.handleLocationInput.bind(adminLocationHandler));
|
||||
messageRouter.registerInput(productHandler.handleCategoryInput.bind(productHandler));
|
||||
messageRouter.registerInput(productHandler.handleProductImport.bind(productHandler));
|
||||
messageRouter.registerInput(productHandler.handleProductEditImport.bind(productHandler));
|
||||
messageRouter.registerInput(adminDumpHandler.handleDumpImport.bind(adminDumpHandler));
|
||||
messageRouter.registerInput(adminUserHandler.handleBonusBalanceInput.bind(adminUserHandler));
|
||||
messageRouter.registerInput(productHandler.handleCategoryUpdate.bind(productHandler));
|
||||
|
||||
// === Text Commands ===
|
||||
messageRouter.registerText('📦 Products', async (msg) => {
|
||||
logDebug(msg.text, 'showProducts');
|
||||
await userProductHandler.showProducts(msg);
|
||||
});
|
||||
messageRouter.registerText('👤 Profile', async (msg) => {
|
||||
logDebug(msg.text, 'showProfile');
|
||||
await userHandler.showProfile(msg);
|
||||
});
|
||||
messageRouter.registerText('💰 Wallets', async (msg) => {
|
||||
logDebug(msg.text, 'showBalance');
|
||||
await userWalletsHandler.showBalance(msg);
|
||||
});
|
||||
messageRouter.registerText('🛍 Purchases', async (msg) => {
|
||||
logDebug(msg.text, 'showPurchases');
|
||||
await userPurchaseHandler.showPurchases(msg);
|
||||
});
|
||||
messageRouter.registerText('📦 Manage Products', async (msg) => {
|
||||
if (!isAdmin(msg.from.id)) return;
|
||||
logDebug(msg.text, 'handleProductManagement');
|
||||
await productHandler.handleProductManagement(msg);
|
||||
});
|
||||
messageRouter.registerText('👥 Manage Users', async (msg) => {
|
||||
if (!isAdmin(msg.from.id)) return;
|
||||
logDebug(msg.text, 'handleUserList');
|
||||
await adminUserHandler.handleUserList(msg);
|
||||
});
|
||||
messageRouter.registerText('📍 Manage Locations', async (msg) => {
|
||||
if (!isAdmin(msg.from.id)) return;
|
||||
logDebug(msg.text, 'handleViewLocations');
|
||||
await adminLocationHandler.handleViewLocations(msg);
|
||||
});
|
||||
messageRouter.registerText('💾 Database Backup', async (msg) => {
|
||||
if (!isAdmin(msg.from.id)) return;
|
||||
logDebug(msg.text, 'handleDump');
|
||||
await adminDumpHandler.handleDump(msg);
|
||||
});
|
||||
messageRouter.registerText('💰 Manage Wallets', async (msg) => {
|
||||
if (!isAdmin(msg.from.id)) return;
|
||||
logDebug(msg.text, 'handleWalletManagement');
|
||||
await adminWalletsHandler.handleWalletManagement(msg);
|
||||
});
|
||||
|
||||
// === Exact Callback Routes ===
|
||||
callbackRouter.registerExact('set_location', async (cq) => {
|
||||
logDebug(cq.data, 'handleSetLocation');
|
||||
await userLocationHandler.handleSetLocation(cq);
|
||||
});
|
||||
callbackRouter.registerExact('back_to_profile', async (cq) => {
|
||||
logDebug(cq.data, 'handleBackToProfile');
|
||||
await userHandler.handleBackToProfile(cq);
|
||||
});
|
||||
callbackRouter.registerExact('back_to_balance', async (cq) => {
|
||||
logDebug(cq.data, 'handleBackToBalance');
|
||||
await userWalletsHandler.handleBackToBalance(cq);
|
||||
});
|
||||
callbackRouter.registerExact('delete_account', async (cq) => {
|
||||
logDebug(cq.data, 'handleDeleteAccount');
|
||||
await userDeletionHandler.handleDeleteAccount(cq);
|
||||
});
|
||||
callbackRouter.registerExact('confirm_delete_account', async (cq) => {
|
||||
logDebug(cq.data, 'handleConfirmDelete');
|
||||
await userDeletionHandler.handleConfirmDelete(cq);
|
||||
});
|
||||
callbackRouter.registerExact('add_wallet', async (cq) => {
|
||||
logDebug(cq.data, 'handleAddWallet');
|
||||
await userWalletsHandler.handleAddWallet(cq);
|
||||
});
|
||||
callbackRouter.registerExact('top_up_wallet', async (cq) => {
|
||||
logDebug(cq.data, 'handleTopUpWallet');
|
||||
await userWalletsHandler.handleTopUpWallet(cq);
|
||||
});
|
||||
callbackRouter.registerExact('wallet_history', async (cq) => {
|
||||
logDebug(cq.data, 'handleWalletHistory');
|
||||
await userWalletsHandler.handleWalletHistory(cq);
|
||||
});
|
||||
callbackRouter.registerExact('view_archived_wallets', async (cq) => {
|
||||
logDebug(cq.data, 'handleViewArchivedWallets');
|
||||
await userWalletsHandler.handleViewArchivedWallets(cq);
|
||||
});
|
||||
callbackRouter.registerExact('refresh_balance', async (cq) => {
|
||||
logDebug(cq.data, 'handleRefreshBalance');
|
||||
await userWalletsHandler.handleRefreshBalance(cq);
|
||||
});
|
||||
callbackRouter.registerExact('shop_start', async (cq) => {
|
||||
logDebug(cq.data, 'showProducts');
|
||||
await userProductHandler.showProducts(cq.message);
|
||||
});
|
||||
callbackRouter.registerExact('add_location', async (cq) => {
|
||||
logDebug(cq.data, 'handleAddLocation');
|
||||
await adminLocationHandler.handleAddLocation(cq);
|
||||
});
|
||||
callbackRouter.registerExact('view_locations', async (cq) => {
|
||||
logDebug(cq.data, 'handleViewLocations');
|
||||
await adminLocationHandler.handleViewLocations(cq);
|
||||
});
|
||||
callbackRouter.registerExact('view_ip', async (cq) => {
|
||||
logDebug(cq.data, 'handleViewIP');
|
||||
await adminLocationHandler.handleViewIP(cq);
|
||||
});
|
||||
callbackRouter.registerExact('delete_location', async (cq) => {
|
||||
logDebug(cq.data, 'handleDeleteLocation');
|
||||
await adminLocationHandler.handleDeleteLocation(cq);
|
||||
});
|
||||
callbackRouter.registerExact('admin_menu', async (cq) => {
|
||||
logDebug(cq.data, 'backToMenu');
|
||||
await adminLocationHandler.backToMenu(cq);
|
||||
});
|
||||
callbackRouter.registerExact('manage_products', async (cq) => {
|
||||
logDebug(cq.data, 'handleProductManagement');
|
||||
await productHandler.handleProductManagement(cq);
|
||||
});
|
||||
callbackRouter.registerExact('export_database', async (cq) => {
|
||||
logDebug(cq.data, 'handleExportDatabase');
|
||||
await adminDumpHandler.handleExportDatabase(cq);
|
||||
});
|
||||
callbackRouter.registerExact('import_database', async (cq) => {
|
||||
logDebug(cq.data, 'handleImportDatabase');
|
||||
await adminDumpHandler.handleImportDatabase(cq);
|
||||
});
|
||||
callbackRouter.registerExact('back_to_wallet_types', async (cq) => {
|
||||
logDebug(cq.data, 'handleBackToWalletTypes');
|
||||
await adminWalletsHandler.handleBackToWalletTypes(cq);
|
||||
});
|
||||
|
||||
// === Prefix Callback Routes ===
|
||||
callbackRouter.registerPrefix('set_country_', async (cq) => {
|
||||
logDebug(cq.data, 'handleSetCountry');
|
||||
await userLocationHandler.handleSetCountry(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('set_city_', async (cq) => {
|
||||
logDebug(cq.data, 'handleSetCity');
|
||||
await userLocationHandler.handleSetCity(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('set_district_', async (cq) => {
|
||||
logDebug(cq.data, 'handleSetDistrict');
|
||||
await userLocationHandler.handleSetDistrict(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('generate_wallet_', async (cq) => {
|
||||
logDebug(cq.data, 'handleGenerateWallet');
|
||||
await userWalletsHandler.handleGenerateWallet(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('shop_country_', async (cq) => {
|
||||
logDebug(cq.data, 'handleCountrySelection');
|
||||
await userProductHandler.handleCountrySelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('shop_city_', async (cq) => {
|
||||
logDebug(cq.data, 'handleCitySelection');
|
||||
await userProductHandler.handleCitySelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('shop_district_', async (cq) => {
|
||||
logDebug(cq.data, 'handleDistrictSelection');
|
||||
await userProductHandler.handleDistrictSelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('shop_category_', async (cq) => {
|
||||
logDebug(cq.data, 'handleCategorySelection');
|
||||
await userProductHandler.handleCategorySelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('shop_product_', async (cq) => {
|
||||
logDebug(cq.data, 'handleProductSelection');
|
||||
await userProductHandler.handleProductSelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('increase_quantity_', async (cq) => {
|
||||
logDebug(cq.data, 'handleIncreaseQuantity');
|
||||
await userProductHandler.handleIncreaseQuantity(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('decrease_quantity_', async (cq) => {
|
||||
logDebug(cq.data, 'handleDecreaseQuantity');
|
||||
await userProductHandler.handleDecreaseQuantity(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('buy_product_', async (cq) => {
|
||||
logDebug(cq.data, 'handleBuyProduct');
|
||||
await userProductHandler.handleBuyProduct(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('pay_with_', async (cq) => {
|
||||
logDebug(cq.data, 'handlePay');
|
||||
await userProductHandler.handlePay(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('list_purchases_', async (cq) => {
|
||||
logDebug(cq.data, 'handlePurchaseListPage');
|
||||
await userPurchaseHandler.handlePurchaseListPage(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('view_purchase_', async (cq) => {
|
||||
logDebug(cq.data, 'viewPurchase');
|
||||
await userPurchaseHandler.viewPurchase(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('confirm_received_', async (cq) => {
|
||||
logDebug(cq.data, 'handleConfirmReceived');
|
||||
await userPurchaseHandler.handleConfirmReceived(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('confirm_delete_location_', async (cq) => {
|
||||
logDebug(cq.data, 'handleConfirmDelete');
|
||||
await adminLocationHandler.handleConfirmDelete(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('prod_country_', async (cq) => {
|
||||
logDebug(cq.data, 'handleCountrySelection');
|
||||
await productHandler.handleCountrySelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('prod_city_', async (cq) => {
|
||||
logDebug(cq.data, 'handleCitySelection');
|
||||
await productHandler.handleCitySelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('prod_district_', async (cq) => {
|
||||
logDebug(cq.data, 'handleDistrictSelection');
|
||||
await productHandler.handleDistrictSelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('add_category_', async (cq) => {
|
||||
logDebug(cq.data, 'handleAddCategory');
|
||||
await productHandler.handleAddCategory(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('edit_category_', async (cq) => {
|
||||
logDebug(cq.data, 'handleEditCategory');
|
||||
await productHandler.handleEditCategory(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('prod_category_', async (cq) => {
|
||||
logDebug(cq.data, 'handleCategorySelection');
|
||||
await productHandler.handleCategorySelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('list_products_', async (cq) => {
|
||||
logDebug(cq.data, 'handleProductListPage');
|
||||
await productHandler.handleProductListPage(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('add_product_', async (cq) => {
|
||||
logDebug(cq.data, 'handleAddProduct');
|
||||
await productHandler.handleAddProduct(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('view_product_', async (cq) => {
|
||||
logDebug(cq.data, 'handleViewProduct');
|
||||
await productHandler.handleViewProduct(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('edit_product_', async (cq) => {
|
||||
logDebug(cq.data, 'handleProductEdit');
|
||||
await productHandler.handleProductEdit(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('delete_product_', async (cq) => {
|
||||
logDebug(cq.data, 'handleProductDelete');
|
||||
await productHandler.handleProductDelete(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('confirm_delete_product_', async (cq) => {
|
||||
logDebug(cq.data, 'handleConfirmDelete');
|
||||
await productHandler.handleConfirmDelete(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('view_user_', async (cq) => {
|
||||
logDebug(cq.data, 'handleViewUser');
|
||||
await adminUserHandler.handleViewUser(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('list_users_', async (cq) => {
|
||||
logDebug(cq.data, 'handleUserListPage');
|
||||
await adminUserHandler.handleUserListPage(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('delete_user_', async (cq) => {
|
||||
logDebug(cq.data, 'handleDeleteUser');
|
||||
await adminUserHandler.handleDeleteUser(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('block_user_', async (cq) => {
|
||||
logDebug(cq.data, 'handleBlockUser');
|
||||
await adminUserHandler.handleBlockUser(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('confirm_delete_user_', async (cq) => {
|
||||
logDebug(cq.data, 'handleConfirmDelete');
|
||||
await adminUserHandler.handleConfirmDelete(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('confirm_block_user_', async (cq) => {
|
||||
logDebug(cq.data, 'handleConfirmBlock');
|
||||
await adminUserHandler.handleConfirmBlock(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('edit_user_balance_', async (cq) => {
|
||||
logDebug(cq.data, 'handleEditUserBalance');
|
||||
await adminUserHandler.handleEditUserBalance(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('edit_user_location_', async (cq) => {
|
||||
logDebug(cq.data, 'handleEditUserLocation');
|
||||
await adminUserLocationHandler.handleEditUserLocation(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('edit_user_country_', async (cq) => {
|
||||
logDebug(cq.data, 'handleEditUserCountry');
|
||||
await adminUserLocationHandler.handleEditUserCountry(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('edit_user_city_', async (cq) => {
|
||||
logDebug(cq.data, 'handleEditUserCity');
|
||||
await adminUserLocationHandler.handleEditUserCity(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('edit_user_district_', async (cq) => {
|
||||
logDebug(cq.data, 'handleEditUserDistrict');
|
||||
await adminUserLocationHandler.handleEditUserDistrict(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('wallet_type_', async (cq) => {
|
||||
logDebug(cq.data, 'handleWalletTypeSelection');
|
||||
await adminWalletsHandler.handleWalletTypeSelection(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('check_balance_', async (cq) => {
|
||||
logDebug(cq.data, 'handleCheckCommissionBalance');
|
||||
await adminWalletsHandler.handleCheckCommissionBalance(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('prev_page_', async (cq) => {
|
||||
logDebug(cq.data, 'handlePagination');
|
||||
await adminWalletsHandler.handlePagination(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('next_page_', async (cq) => {
|
||||
logDebug(cq.data, 'handlePagination');
|
||||
await adminWalletsHandler.handlePagination(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('export_csv_', async (cq) => {
|
||||
logDebug(cq.data, 'handleExportCSV');
|
||||
await adminWalletsHandler.handleExportCSV(cq);
|
||||
});
|
||||
callbackRouter.registerPrefix('view_transaction_history_', async (cq) => {
|
||||
logDebug(cq.data, 'handleTransactionHistory');
|
||||
const page = parseInt(cq.data.split('_').pop());
|
||||
await userWalletsHandler.handleTransactionHistory(cq, page);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user