Update Detailed Product Viev
This commit is contained in:
@@ -821,82 +821,51 @@ 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: ${location.country}, ${location.city}, ${location.district}
|
Location: ${product.country}, ${product.city}, ${product.district}
|
||||||
Category: ${product.category_name}
|
Category: ${product.category_name}
|
||||||
Subcategory: ${product.subcategory_name}
|
|
||||||
|
🔒 Private Information:
|
||||||
🔒 Private Information:
|
${product.private_data}
|
||||||
${product.private_data}
|
Hidden Location: ${product.hidden_description}
|
||||||
Hidden Location: ${product.hidden_description}
|
Coordinates: ${product.hidden_coordinates}
|
||||||
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}`
|
|
||||||
}]
|
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
let photoMessage;
|
await bot.editMessageText(
|
||||||
let hiddenPhotoMessage;
|
message,
|
||||||
|
{
|
||||||
// Send product photos
|
chat_id: chatId,
|
||||||
if (product.photo_url) {
|
message_id: callbackQuery.message.message_id,
|
||||||
try {
|
reply_markup: keyboard,
|
||||||
photoMessage = await bot.sendPhoto(chatId, product.photo_url, {caption: 'Public photo'});
|
parse_mode: 'HTML'
|
||||||
} 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,78 +291,46 @@ 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.name}
|
📦 Product Details:
|
||||||
|
|
||||||
💰 Price: $${product.price}
|
Name: ${product.name}
|
||||||
📝 Description: ${product.description}
|
Price: $${product.price}
|
||||||
📦 Available: ${product.quantity_in_stock} pcs
|
Description: ${product.description}
|
||||||
|
Stock: ${product.quantity_in_stock}
|
||||||
Category: ${product.category_name}
|
Location: ${product.country}, ${product.city}, ${product.district}
|
||||||
Subcategory: ${product.subcategory_name}
|
Category: ${product.category_name}
|
||||||
`;
|
|
||||||
|
🔒 Private Information:
|
||||||
let photoMessageId = null;
|
${product.private_data}
|
||||||
|
Hidden Location: ${product.hidden_description}
|
||||||
// First send the photo if it exists
|
Coordinates: ${product.hidden_coordinates}
|
||||||
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: '🛒 Buy Now', callback_data: `buy_product_${productId}`}],
|
[{ text: '« Back', callback_data: `shop_category_${product.location_id}_${product.category_id}` }]
|
||||||
[
|
|
||||||
{
|
|
||||||
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}`
|
|
||||||
}]
|
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
// Then send the message with controls
|
await bot.editMessageText(
|
||||||
await bot.sendMessage(chatId, message, {
|
message,
|
||||||
reply_markup: keyboard,
|
{
|
||||||
parse_mode: 'HTML'
|
chat_id: chatId,
|
||||||
});
|
message_id: callbackQuery.message.message_id,
|
||||||
|
reply_markup: keyboard,
|
||||||
// Store the current quantity and photo message ID in user state
|
parse_mode: 'HTML'
|
||||||
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,9 +12,10 @@ 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
|
`SELECT p.*, c.name as category_name, l.country, l.city, l.district
|
||||||
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]
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user