product deletion
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import db from '../config/database.js';
|
import db from '../config/database.js';
|
||||||
import config from '../config/config.js';
|
import config from '../config/config.js';
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
|
import User from "../models/User.js";
|
||||||
|
|
||||||
export default class AdminProductHandler {
|
export default class AdminProductHandler {
|
||||||
constructor(bot) {
|
constructor(bot) {
|
||||||
@@ -676,4 +677,107 @@ Coordinates: ${product.hidden_coordinates}
|
|||||||
await this.bot.sendMessage(chatId, 'Error loading product details. Please try again.');
|
await this.bot.sendMessage(chatId, 'Error loading product details. Please try again.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async handleProductDelete(callbackQuery) {
|
||||||
|
if (!this.isAdmin(callbackQuery.from.id)) return;
|
||||||
|
|
||||||
|
const productId = callbackQuery.data.replace('delete_product_', '');
|
||||||
|
const chatId = callbackQuery.message.chat.id;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const product = await db.getAsync(
|
||||||
|
`SELECT p.*, c.name as category_name, s.name as subcategory_name,
|
||||||
|
l.country, l.city, l.district
|
||||||
|
FROM products p
|
||||||
|
JOIN categories c ON p.category_id = c.id
|
||||||
|
JOIN subcategories s ON p.subcategory_id = s.id
|
||||||
|
JOIN locations l ON p.location_id = l.id
|
||||||
|
WHERE p.id = ?`,
|
||||||
|
[productId]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!product) {
|
||||||
|
throw new Error('Product not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const keyboard = {
|
||||||
|
inline_keyboard: [
|
||||||
|
[
|
||||||
|
{text: '✅ Confirm Delete', callback_data: `confirm_delete_product_${productId}`},
|
||||||
|
{text: '❌ Cancel', callback_data: `prod_subcategory_${product.location_id}_${product.category_id}_${product.subcategory_id}`}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.bot.editMessageText(
|
||||||
|
`⚠️ Are you sure you want to delete product\n\nThis action cannot be undone!`,
|
||||||
|
{
|
||||||
|
chat_id: chatId,
|
||||||
|
message_id: callbackQuery.message.message_id,
|
||||||
|
reply_markup: keyboard,
|
||||||
|
parse_mode: 'HTML'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in handleDeleteUser:', error);
|
||||||
|
await this.bot.sendMessage(chatId, 'Error processing delete request. Please try again.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleConfirmDelete(callbackQuery) {
|
||||||
|
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 db.getAsync(
|
||||||
|
`SELECT p.*, c.name as category_name, s.name as subcategory_name,
|
||||||
|
l.country, l.city, l.district
|
||||||
|
FROM products p
|
||||||
|
JOIN categories c ON p.category_id = c.id
|
||||||
|
JOIN subcategories s ON p.subcategory_id = s.id
|
||||||
|
JOIN locations l ON p.location_id = l.id
|
||||||
|
WHERE p.id = ?`,
|
||||||
|
[productId]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!product) {
|
||||||
|
throw new Error('Product not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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");
|
||||||
|
console.error('Error deleting product:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
const keyboard = {
|
||||||
|
inline_keyboard: [
|
||||||
|
[{
|
||||||
|
text: '« Back',
|
||||||
|
callback_data: `prod_subcategory_${product.location_id}_${product.category_id}_${product.subcategory_id}`
|
||||||
|
}]
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.bot.editMessageText(
|
||||||
|
`✅ Product has been successfully deleted.`,
|
||||||
|
{
|
||||||
|
chat_id: chatId,
|
||||||
|
message_id: callbackQuery.message.message_id,
|
||||||
|
reply_markup: keyboard
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in handleConfirmDelete:', error);
|
||||||
|
await this.bot.sendMessage(chatId, 'Error deleting product. Please try again.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -278,6 +278,12 @@ bot.on('callback_query', async (callbackQuery) => {
|
|||||||
} else if (action.startsWith('view_product_')) {
|
} else if (action.startsWith('view_product_')) {
|
||||||
logDebug(action, 'handleViewProduct');
|
logDebug(action, 'handleViewProduct');
|
||||||
await adminProductHandler.handleViewProduct(callbackQuery);
|
await adminProductHandler.handleViewProduct(callbackQuery);
|
||||||
|
} else if (action.startsWith('delete_product_')) {
|
||||||
|
logDebug(action, 'handleViewProduct');
|
||||||
|
await adminProductHandler.handleProductDelete(callbackQuery);
|
||||||
|
} else if (action.startsWith('confirm_delete_product_')) {
|
||||||
|
logDebug(action, 'handleConfirmDelete');
|
||||||
|
await adminProductHandler.handleConfirmDelete(callbackQuery);
|
||||||
}
|
}
|
||||||
// Admin user management
|
// Admin user management
|
||||||
else if (action.startsWith('view_user_')) {
|
else if (action.startsWith('view_user_')) {
|
||||||
|
|||||||
Reference in New Issue
Block a user