diff --git a/src/handlers/adminHandlers/adminLocationHandler.js b/src/handlers/adminHandlers/adminLocationHandler.js index 1f4585b..88d4572 100644 --- a/src/handlers/adminHandlers/adminLocationHandler.js +++ b/src/handlers/adminHandlers/adminLocationHandler.js @@ -227,7 +227,7 @@ export default class AdminLocationHandler { const keyboard = { inline_keyboard: locations.map(loc => [{ text: `${loc.country} > ${loc.city} > ${loc.district} (P:${loc.product_count} C:${loc.category_count})`, - callback_data: `confirm_delete_location_${loc.country}_${loc.city}_${loc.district}` + callback_data: `confirm_delete_location_${loc.id}` // Используем ID локации вместо строки }]) }; @@ -254,23 +254,27 @@ export default class AdminLocationHandler { } const chatId = callbackQuery.message.chat.id; - const [country, city, district] = callbackQuery.data - .replace('confirm_delete_location_', '') - .split('_'); + const locationId = callbackQuery.data.replace('confirm_delete_location_', ''); try { + const location = await db.getAsync('SELECT * FROM locations WHERE id = ?', [locationId]); + + if (!location) { + throw new Error('Location not found'); + } + await db.runAsync('BEGIN TRANSACTION'); const result = await db.runAsync( - 'DELETE FROM locations WHERE country = ? AND city = ? AND district = ?', - [country, city, district] + 'DELETE FROM locations WHERE id = ?', + [locationId] ); await db.runAsync('COMMIT'); if (result.changes > 0) { await bot.editMessageText( - `✅ Location deleted successfully!\n\nCountry: ${country}\nCity: ${city}\nDistrict: ${district}`, + `✅ Location deleted successfully!\n\nCountry: ${location.country}\nCity: ${location.city}\nDistrict: ${location.district}`, { chat_id: chatId, message_id: callbackQuery.message.message_id, diff --git a/src/handlers/adminHandlers/adminProductHandler.js b/src/handlers/adminHandlers/adminProductHandler.js index e604f0f..263cb97 100644 --- a/src/handlers/adminHandlers/adminProductHandler.js +++ b/src/handlers/adminHandlers/adminProductHandler.js @@ -890,7 +890,6 @@ export default class AdminProductHandler { const locationId = product.location_id; const categoryId = product.category_id; - const subcategoryId = product.subcategory_id; const sampleProduct = { name: product.name, @@ -911,7 +910,6 @@ export default class AdminProductHandler { action: 'edit_product', locationId, categoryId, - subcategoryId, productId }); @@ -923,13 +921,13 @@ export default class AdminProductHandler { inline_keyboard: [[ { text: '❌ Cancel', - callback_data: `prod_subcategory_${locationId}_${categoryId}_${subcategoryId}` + callback_data: `prod_category_${locationId}_${categoryId}` // Возвращаемся к списку товаров в категории } ]] } }); } catch (error) { - console.error('Error in handleViewProduct:', error); + console.error('Error in handleProductEdit:', error); await bot.sendMessage(chatId, 'Error loading product details. Please try again.'); } } @@ -980,36 +978,36 @@ export default class AdminProductHandler { if (!this.isAdmin(callbackQuery.from.id)) { return; } - + const productId = callbackQuery.data.replace('confirm_delete_product_', ''); const chatId = callbackQuery.message.chat.id; - + try { const product = await ProductService.getDetailedProductById(productId); - + if (!product) { throw new Error('Product not found'); } - + + const locationId = product.location_id; + const categoryId = product.category_id; + try { await db.runAsync('BEGIN TRANSACTION'); await db.runAsync('DELETE FROM products WHERE id=?', [productId.toString()]); await db.runAsync('COMMIT'); } catch (e) { - await db.runAsync("ROLLBACK"); + await db.runAsync('ROLLBACK'); console.error('Error deleting product:', e); throw e; } - + const keyboard = { inline_keyboard: [ - [{ - text: '« Back', - callback_data: `prod_subcategory_${product.location_id}_${product.category_id}_${product.subcategory_id}` - }] + [{ text: '« Back', callback_data: `prod_category_${locationId}_${categoryId}` }] // Возвращаемся к списку товаров в категории ] }; - + await bot.editMessageText( `✅ Product has been successfully deleted.`, {