Files
tg_tinder_bot/src/handlers/likeBackHandler.ts

77 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import TelegramBot from 'node-telegram-bot-api';
import { ProfileService } from '../services/profileService';
import { MatchingService } from '../services/matchingService';
export class LikeBackHandler {
private bot: TelegramBot;
private profileService: ProfileService;
private matchingService: MatchingService;
constructor(bot: TelegramBot) {
this.bot = bot;
this.profileService = new ProfileService();
this.matchingService = new MatchingService();
}
// Функция для обработки обратного лайка из уведомления
async handleLikeBack(chatId: number, telegramId: string, targetUserId: string): Promise<void> {
try {
// Получаем информацию о пользователях
const [userId, targetProfile] = await Promise.all([
this.profileService.getUserIdByTelegramId(telegramId),
this.profileService.getProfileByUserId(targetUserId)
]);
if (!userId || !targetProfile) {
await this.bot.sendMessage(chatId, '❌ Не удалось найти профиль');
return;
}
// Проверяем, есть ли уже свайп
const existingSwipe = await this.matchingService.getSwipeBetweenUsers(userId, targetUserId);
if (existingSwipe) {
await this.bot.sendMessage(chatId, '❓ Вы уже оценили этот профиль ранее.');
return;
}
// Создаем свайп (лайк)
const result = await this.matchingService.createSwipe(userId, targetUserId, 'like');
if (result.isMatch) {
// Это матч!
await this.bot.sendMessage(
chatId,
'🎉 *Поздравляем! Это взаимно!*\n\n' +
`Вы и *${targetProfile.name}* понравились друг другу!\n` +
'Теперь вы можете начать общение.',
{
parse_mode: 'Markdown',
reply_markup: {
inline_keyboard: [
[{ text: '💬 Начать общение', callback_data: `start_chat:${targetUserId}` }],
[{ text: '👀 Посмотреть профиль', callback_data: `view_profile:${targetUserId}` }]
]
}
}
);
} else {
await this.bot.sendMessage(
chatId,
'❤️ Вам понравился профиль ' + targetProfile.name + '!\n\n' +
'Если вы также понравитесь этому пользователю, будет создан матч.',
{
reply_markup: {
inline_keyboard: [
[{ text: '🔍 Продолжить поиск', callback_data: 'start_browsing' }]
]
}
}
);
}
} catch (error) {
console.error('Error in handleLikeBack:', error);
await this.bot.sendMessage(chatId, '❌ Произошла ошибка при обработке лайка');
}
}
}