Files
telegram-shop/src/handlers/userHandlers/userPurchaseHandler.js
Artyom Ashirov 5d4f56e265 refactoring
2024-11-23 05:03:30 +03:00

161 lines
5.3 KiB
JavaScript

import config from "../../config/config.js";
import PurchaseService from "../../services/purchaseService.js";
import UserService from "../../services/userService.js";
import bot from "../../context/bot.js";
import ProductService from "../../services/productService.js";
export default class UserPurchaseHandler {
static async viewPurchasePage(userId, page) {
try {
const limit = 10;
const offset = (page || 0) * limit;
const previousPage = page > 0 ? page - 1 : 0;
const nextPage = page + 1;
const purchases = await PurchaseService.getPurchasesByUserId(userId, limit, offset);
if ((purchases.length === 0) && (page == 0)) {
return {
text: 'You haven\'t made any purchases yet.',
markup: [[
{text: '🛍 Browse Products', callback_data: 'shop_start'}
]]
}
}
if ((purchases.length === 0) && (page > 0)) {
return await this.viewPurchasePage(userId, previousPage);
}
const keyboard = {
inline_keyboard: [
...purchases.map(item => [{
text: `${item.product_name} [${new Date(item.purchase_date).toLocaleString()}]`,
callback_data: `view_purchase_${item.id}`
}]),
]
};
keyboard.inline_keyboard.push([
{text: `«`, callback_data: `list_purchases_${previousPage}`},
{text: `»`, callback_data: `list_purchases_${nextPage}`},
]);
keyboard.inline_keyboard.push([
{text: '🛍 Browse Products', callback_data: 'shop_start'}
]);
return {
text: `📦 Select purchase to view detailed information:`,
markup: keyboard
}
} catch (error) {
console.error('Error in showPurchases:', error);
return {text: 'Error loading purchase history. Please try again.'};
}
}
static async handlePurchaseListPage(callbackQuery) {
const telegramId = callbackQuery.from.id;
const chatId = callbackQuery.message.chat.id;
const page = callbackQuery.data.replace('list_purchases_', '');
try {
const user = await UserService.getUserByTelegramId(telegramId);
if (!user) {
await bot.sendMessage(chatId, 'User not found.');
return;
}
const {text, markup} = await this.viewPurchasePage(user.id, parseInt(page));
await bot.editMessageText(text, {
chat_id: chatId,
message_id: callbackQuery.message.message_id,
reply_markup: markup,
parse_mode: 'Markdown',
});
} catch (e) {
return;
}
}
static async showPurchases(msg) {
const chatId = msg.chat.id;
const telegramId = msg.from.id;
try {
const user = await UserService.getUserByTelegramId(telegramId);
if (!user) {
await bot.sendMessage(chatId, 'User not found.');
return;
}
const {text, markup} = await this.viewPurchasePage(user.id, 0);
await bot.sendMessage(chatId, text, {reply_markup: markup, parse_mode: 'Markdown'});
} catch (error) {
console.error('Error in handleSubcategorySelection:', error);
await bot.sendMessage(chatId, 'Error loading products. Please try again.');
}
}
static async viewPurchase(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const purchaseId = callbackQuery.data.replace('view_purchase_', '');
const purchase = await PurchaseService.getPurchaseById(purchaseId);
if (!purchase) {
await bot.sendMessage(chatId, "No such purchase");
return;
}
const product = await ProductService.getProductById(purchase.product_id)
if (!product) {
await bot.sendMessage(chatId, "No such product");
return;
}
let hiddenPhotoMessage;
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'})
}
}
const message = `
📦 Purchase Details:
Name: ${purchase.product_name}
Quantity: ${purchase.quantity}
Total: $${purchase.total_price}
Location: ${purchase.country}, ${purchase.city}
Payment: ${purchase.wallet_type}
Date: ${new Date(purchase.purchase_date).toLocaleString()}
🔒 Private Information:
${product.private_data}
Hidden Location: ${product.hidden_description}
Coordinates: ${product.hidden_coordinates}
`;
const keyboard = {
inline_keyboard: [
[{text: "I've got it!", callback_data: "Asdasdasd"}],
[{text: "Contact support", url: config.SUPPORT_LINK}]
]
};
await bot.sendMessage(chatId, message, {reply_markup: keyboard});
await bot.deleteMessage(chatId, callbackQuery.message.message_id);
}
}