feat(market): add Minha Intenções tab with panel, hook, and badge

- Introduced "Minhas Intenções" tab visible only to logged-in users.
- Integrated MyPurchaseIntentionsPanel and useMyPurchaseIntentions hook.
- Added tab badge showing count of pending intentions.
- Extended session API to include user’s main character for downstream features.
This commit is contained in:
Evandro Fonseca Junior committed 2025-11-07 17:25:51 -03:00
1 parent f35fee944d
commit 7f42def478
55 files changed
+4906 -95

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
/**
* GET /api/chats/unread-count
* Retorna contagem de usuários únicos com mensagens não vistas
*/
import { NextRequest, NextResponse } from "next/server";
import { requireAuth } from "@/lib/auth/middleware";
import { connectMongoDB } from "@/lib/mongodb/connection";
import { Chat } from "@/lib/mongodb/models/Chat";
import { secureLogger } from "@/lib/secure-logger";
export async function GET(request: NextRequest) {
try {
const authResult = await requireAuth(request);
if (authResult instanceof NextResponse) return authResult;
const auth = authResult;
await connectMongoDB();
// Buscar todos os chats do usuário
const chats = await Chat.find({
$or: [{ buyer_id: auth.userId }, { seller_id: auth.userId }],
is_active: true,
});
// Calcular contagem de usuários únicos com mensagens não vistas
const uniqueUsersWithUnread = new Set<number>();
chats.forEach((chat) => {
let unreadCount = 0;
let otherUserId: number;
if (chat.buyer_id === auth.userId) {
unreadCount = chat.buyer_unread_count;
otherUserId = chat.seller_id;
} else {
unreadCount = chat.seller_unread_count;
otherUserId = chat.buyer_id;
}
// Se há mensagens não vistas, adicionar o outro usuário ao Set
if (unreadCount > 0) {
uniqueUsersWithUnread.add(otherUserId);
}
});
return NextResponse.json(
{
success: true,
count: uniqueUsersWithUnread.size,
totalUnreadMessages: chats.reduce((total, chat) => {
if (chat.buyer_id === auth.userId) {
return total + chat.buyer_unread_count;
}
return total + chat.seller_unread_count;
}, 0),
},
{ status: 200 }
);
} catch (error) {
secureLogger.error("❌ Erro ao buscar contagem de chats não lidos", {
error: error instanceof Error ? error.message : "Erro desconhecido",
stack: error instanceof Error ? error.stack : undefined,
});
return NextResponse.json(
{ success: false, error: "Erro ao buscar contagem" },
{ status: 500 }
);
}
}