parent
057d1536bb
commit
eea5d9b9e7
@ -821,51 +821,82 @@ export default class AdminProductHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async handleViewProduct(callbackQuery) {
|
static async handleViewProduct(callbackQuery) {
|
||||||
|
if (!this.isAdmin(callbackQuery.from.id)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const chatId = callbackQuery.message.chat.id;
|
const chatId = callbackQuery.message.chat.id;
|
||||||
|
const messageId = callbackQuery.message.message_id;
|
||||||
const productId = callbackQuery.data.replace('view_product_', '');
|
const productId = callbackQuery.data.replace('view_product_', '');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const product = await ProductService.getDetailedProductById(productId);
|
const product = await ProductService.getDetailedProductById(productId)
|
||||||
|
|
||||||
if (!product) {
|
if (!product) {
|
||||||
throw new Error('Product not found');
|
throw new Error('Product not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const location = await LocationService.getLocationById(product.location_id);
|
||||||
|
|
||||||
|
if (!location) {
|
||||||
|
throw new Error('Location not found');
|
||||||
|
}
|
||||||
|
|
||||||
const message = `
|
const message = `
|
||||||
📦 Product Details:
|
📦 Product Details:
|
||||||
|
|
||||||
Name: ${product.name}
|
Name: ${product.name}
|
||||||
Price: $${product.price}
|
Price: $${product.price}
|
||||||
Description: ${product.description}
|
Description: ${product.description}
|
||||||
Stock: ${product.quantity_in_stock}
|
Stock: ${product.quantity_in_stock}
|
||||||
Location: ${product.country}, ${product.city}, ${product.district}
|
Location: ${location.country}, ${location.city}, ${location.district}
|
||||||
Category: ${product.category_name}
|
Category: ${product.category_name}
|
||||||
|
Subcategory: ${product.subcategory_name}
|
||||||
🔒 Private Information:
|
|
||||||
${product.private_data}
|
🔒 Private Information:
|
||||||
Hidden Location: ${product.hidden_description}
|
${product.private_data}
|
||||||
Coordinates: ${product.hidden_coordinates}
|
Hidden Location: ${product.hidden_description}
|
||||||
`;
|
Coordinates: ${product.hidden_coordinates}
|
||||||
|
`;
|
||||||
|
|
||||||
const keyboard = {
|
const keyboard = {
|
||||||
inline_keyboard: [
|
inline_keyboard: [
|
||||||
[
|
[
|
||||||
{ text: '✏️ Edit', callback_data: `edit_product_${productId}` },
|
{text: '✏️ Edit', callback_data: `edit_product_${productId}`},
|
||||||
{ text: '❌ Delete', callback_data: `delete_product_${productId}` }
|
{text: '❌ Delete', callback_data: `delete_product_${productId}`}
|
||||||
],
|
],
|
||||||
[{ text: '« Back', callback_data: `prod_category_${product.location_id}_${product.category_id}` }]
|
[{
|
||||||
|
text: '« Back',
|
||||||
|
callback_data: `prod_subcategory_${product.location_id}_${product.category_id}_${product.subcategory_id}`
|
||||||
|
}]
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
await bot.editMessageText(
|
let photoMessage;
|
||||||
message,
|
let hiddenPhotoMessage;
|
||||||
{
|
|
||||||
chat_id: chatId,
|
// Send product photos
|
||||||
message_id: callbackQuery.message.message_id,
|
if (product.photo_url) {
|
||||||
reply_markup: keyboard,
|
try {
|
||||||
parse_mode: 'HTML'
|
photoMessage = await bot.sendPhoto(chatId, product.photo_url, {caption: 'Public photo'});
|
||||||
|
} catch (e) {
|
||||||
|
photoMessage = await bot.sendPhoto(chatId, "./corrupt-photo.jpg", {caption: 'Public photo'})
|
||||||
}
|
}
|
||||||
);
|
}
|
||||||
|
if (product.hidden_photo_url) {
|
||||||
|
try {
|
||||||
|
hiddenPhotoMessage = await bot.sendPhoto(chatId, product.hidden_photo_url, {caption: 'Hidden photo'});
|
||||||
|
} catch (e) {
|
||||||
|
hiddenPhotoMessage = await bot.sendPhoto(chatId, "./corrupt-photo.jpg", {caption: 'Hidden photo'})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
userStates.set(chatId, {
|
||||||
|
msgToDelete: [photoMessage.message_id, hiddenPhotoMessage.message_id]
|
||||||
|
})
|
||||||
|
|
||||||
|
await bot.deleteMessage(chatId, messageId);
|
||||||
|
await bot.sendMessage(chatId, message, {reply_markup: keyboard});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in handleViewProduct:', error);
|
console.error('Error in handleViewProduct:', error);
|
||||||
await bot.sendMessage(chatId, 'Error loading product details. Please try again.');
|
await bot.sendMessage(chatId, 'Error loading product details. Please try again.');
|
||||||
|
@ -291,46 +291,78 @@ export default class UserProductHandler {
|
|||||||
|
|
||||||
static async handleProductSelection(callbackQuery) {
|
static async handleProductSelection(callbackQuery) {
|
||||||
const chatId = callbackQuery.message.chat.id;
|
const chatId = callbackQuery.message.chat.id;
|
||||||
|
const messageId = callbackQuery.message.message_id;
|
||||||
const productId = callbackQuery.data.replace('shop_product_', '');
|
const productId = callbackQuery.data.replace('shop_product_', '');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const product = await ProductService.getDetailedProductById(productId);
|
const product = await ProductService.getDetailedProductById(productId);
|
||||||
|
|
||||||
if (!product) {
|
if (!product) {
|
||||||
throw new Error('Product not found');
|
throw new Error('Product not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete the previous message
|
||||||
|
await bot.deleteMessage(chatId, messageId);
|
||||||
|
|
||||||
const message = `
|
const message = `
|
||||||
📦 Product Details:
|
📦 ${product.name}
|
||||||
|
|
||||||
Name: ${product.name}
|
💰 Price: $${product.price}
|
||||||
Price: $${product.price}
|
📝 Description: ${product.description}
|
||||||
Description: ${product.description}
|
📦 Available: ${product.quantity_in_stock} pcs
|
||||||
Stock: ${product.quantity_in_stock}
|
|
||||||
Location: ${product.country}, ${product.city}, ${product.district}
|
Category: ${product.category_name}
|
||||||
Category: ${product.category_name}
|
Subcategory: ${product.subcategory_name}
|
||||||
|
`;
|
||||||
🔒 Private Information:
|
|
||||||
${product.private_data}
|
let photoMessageId = null;
|
||||||
Hidden Location: ${product.hidden_description}
|
|
||||||
Coordinates: ${product.hidden_coordinates}
|
// First send the photo if it exists
|
||||||
`;
|
let photoMessage;
|
||||||
|
if (product.photo_url) {
|
||||||
|
try {
|
||||||
|
photoMessage = await bot.sendPhoto(chatId, product.photo_url, {caption: 'Public photo'});
|
||||||
|
} catch (e) {
|
||||||
|
photoMessage = await bot.sendPhoto(chatId, "./corrupt-photo.jpg", {caption: 'Public photo'})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const keyboard = {
|
const keyboard = {
|
||||||
inline_keyboard: [
|
inline_keyboard: [
|
||||||
[{ text: '« Back', callback_data: `shop_category_${product.location_id}_${product.category_id}` }]
|
[{text: '🛒 Buy Now', callback_data: `buy_product_${productId}`}],
|
||||||
|
[
|
||||||
|
{
|
||||||
|
text: '➖',
|
||||||
|
callback_data: `decrease_quantity_${productId}`,
|
||||||
|
callback_game: {} // Initially disabled as quantity starts at 1
|
||||||
|
},
|
||||||
|
{text: '1', callback_data: 'current_quantity'},
|
||||||
|
{
|
||||||
|
text: '➕',
|
||||||
|
callback_data: `increase_quantity_${productId}`,
|
||||||
|
callback_game: product.quantity_in_stock <= 1 ? {} : null // Disabled if stock is 1 or less
|
||||||
|
}
|
||||||
|
],
|
||||||
|
[{
|
||||||
|
text: `« Back to ${product.subcategory_name}`,
|
||||||
|
callback_data: `shop_subcategory_${product.location_id}_${product.category_id}_${product.subcategory_id}_${photoMessageId}`
|
||||||
|
}]
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
await bot.editMessageText(
|
// Then send the message with controls
|
||||||
message,
|
await bot.sendMessage(chatId, message, {
|
||||||
{
|
reply_markup: keyboard,
|
||||||
chat_id: chatId,
|
parse_mode: 'HTML'
|
||||||
message_id: callbackQuery.message.message_id,
|
});
|
||||||
reply_markup: keyboard,
|
|
||||||
parse_mode: 'HTML'
|
// Store the current quantity and photo message ID in user state
|
||||||
}
|
userStates.set(chatId, {
|
||||||
);
|
action: 'buying_product',
|
||||||
|
productId,
|
||||||
|
quantity: 1,
|
||||||
|
photoMessageId
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error in handleProductSelection:', error);
|
console.error('Error in handleProductSelection:', error);
|
||||||
await bot.sendMessage(chatId, 'Error loading product details. Please try again.');
|
await bot.sendMessage(chatId, 'Error loading product details. Please try again.');
|
||||||
|
@ -12,10 +12,9 @@ class ProductService {
|
|||||||
|
|
||||||
static async getDetailedProductById(productId) {
|
static async getDetailedProductById(productId) {
|
||||||
return await db.getAsync(
|
return await db.getAsync(
|
||||||
`SELECT p.*, c.name as category_name, l.country, l.city, l.district
|
`SELECT p.*, c.name as category_name
|
||||||
FROM products p
|
FROM products p
|
||||||
JOIN categories c ON p.category_id = c.id
|
JOIN categories c ON p.category_id = c.id
|
||||||
JOIN locations l ON p.location_id = l.id
|
|
||||||
WHERE p.id = ?`,
|
WHERE p.id = ?`,
|
||||||
[productId]
|
[productId]
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user