File size: 972 Bytes
a061734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
 * Messaging Routes
 * 
 * Full messaging API for the frontend
 */

import { NextRequest, NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import {
    getConversations,
    getChatHistory,
    sendMessage,
    markMessagesAsRead,
    getUnreadCount,
    pollNewMessages
} from "@/lib/db/queries/messages";

// GET /api/messaging - Get conversations list
export async function GET(request: NextRequest) {
    try {
        const user = await getCurrentUser(request);
        if (!user) {
            return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
        }

        const conversations = await getConversations(user.id);
        const unreadCount = await getUnreadCount(user.id);

        return NextResponse.json({ conversations, unreadCount });
    } catch (error) {
        console.error("Messaging error:", error);
        return NextResponse.json({ error: "Internal server error" }, { status: 500 });
    }
}