mass refactor

This commit is contained in:
2025-09-18 08:31:14 +09:00
parent 856bf3ca2a
commit bdd7d0424f
58 changed files with 3009 additions and 291 deletions

View File

@@ -24,8 +24,8 @@ export class ChatService {
SELECT
m.*,
CASE
WHEN m.user1_id = $1 THEN m.user2_id
ELSE m.user1_id
WHEN m.user_id_1 = $1 THEN m.user_id_2
ELSE m.user_id_1
END as other_user_id,
p.name as other_user_name,
p.photos as other_user_photos,
@@ -42,8 +42,8 @@ export class ChatService {
FROM matches m
LEFT JOIN profiles p ON (
CASE
WHEN m.user1_id = $1 THEN p.user_id = m.user2_id
ELSE p.user_id = m.user1_id
WHEN m.user_id_1 = $1 THEN p.user_id = m.user_id_2
ELSE p.user_id = m.user_id_1
END
)
LEFT JOIN messages msg ON msg.id = (
@@ -52,10 +52,10 @@ export class ChatService {
ORDER BY created_at DESC
LIMIT 1
)
WHERE (m.user1_id = $1 OR m.user2_id = $1)
AND m.status = 'active'
WHERE (m.user_id_1 = $1 OR m.user_id_2 = $1)
AND m.is_active = true
ORDER BY
CASE WHEN msg.created_at IS NULL THEN m.matched_at ELSE msg.created_at END DESC
CASE WHEN msg.created_at IS NULL THEN m.created_at ELSE msg.created_at END DESC
`, [userId]);
return result.rows.map((row: any) => ({
@@ -91,7 +91,6 @@ export class ChatService {
senderId: row.sender_id,
content: row.content,
messageType: row.message_type,
fileId: row.file_id,
isRead: row.is_read,
createdAt: new Date(row.created_at)
})).reverse(); // Возвращаем в хронологическом порядке
@@ -106,8 +105,7 @@ export class ChatService {
matchId: string,
senderTelegramId: string,
content: string,
messageType: 'text' | 'photo' | 'video' | 'voice' | 'sticker' | 'gif' = 'text',
fileId?: string
messageType: 'text' | 'photo' | 'video' | 'voice' | 'sticker' | 'gif' = 'text'
): Promise<Message | null> {
try {
// Получаем senderId по telegramId
@@ -119,7 +117,7 @@ export class ChatService {
// Проверяем, что матч активен и пользователь является участником
const matchResult = await query(`
SELECT * FROM matches
WHERE id = $1 AND (user1_id = $2 OR user2_id = $2) AND status = 'active'
WHERE id = $1 AND (user_id_1 = $2 OR user_id_2 = $2) AND is_active = true
`, [matchId, senderId]);
if (matchResult.rows.length === 0) {
@@ -130,9 +128,9 @@ export class ChatService {
// Создаем сообщение
await query(`
INSERT INTO messages (id, match_id, sender_id, content, message_type, file_id, is_read, created_at)
VALUES ($1, $2, $3, $4, $5, $6, false, CURRENT_TIMESTAMP)
`, [messageId, matchId, senderId, content, messageType, fileId]);
INSERT INTO messages (id, match_id, sender_id, content, message_type, is_read, created_at)
VALUES ($1, $2, $3, $4, $5, false, CURRENT_TIMESTAMP)
`, [messageId, matchId, senderId, content, messageType]);
// Обновляем время последнего сообщения в матче
await query(`
@@ -157,7 +155,6 @@ export class ChatService {
senderId: row.sender_id,
content: row.content,
messageType: row.message_type,
fileId: row.file_id,
isRead: row.is_read,
createdAt: new Date(row.created_at)
});
@@ -197,11 +194,11 @@ export class ChatService {
SELECT
m.*,
CASE
WHEN m.user1_id = $2 THEN m.user2_id
ELSE m.user1_id
WHEN m.user_id_1 = $2 THEN m.user_id_2
ELSE m.user_id_1
END as other_user_id
FROM matches m
WHERE m.id = $1 AND (m.user1_id = $2 OR m.user2_id = $2) AND m.status = 'active'
WHERE m.id = $1 AND (m.user_id_1 = $2 OR m.user_id_2 = $2) AND m.is_active = true
`, [matchId, userId]);
if (result.rows.length === 0) {
@@ -234,7 +231,7 @@ export class ChatService {
// Проверяем, что пользователь является участником матча
const matchResult = await query(`
SELECT * FROM matches
WHERE id = $1 AND (user1_id = $2 OR user2_id = $2) AND status = 'active'
WHERE id = $1 AND (user_id_1 = $2 OR user_id_2 = $2) AND is_active = true
`, [matchId, userId]);
if (matchResult.rows.length === 0) {
@@ -244,9 +241,11 @@ export class ChatService {
// Помечаем матч как неактивный
await query(`
UPDATE matches
SET status = 'unmatched'
SET is_active = false,
unmatched_at = NOW(),
unmatched_by = $2
WHERE id = $1
`, [matchId]);
`, [matchId, userId]);
return true;
} catch (error) {