File size: 1,817 Bytes
6a059d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { NextRequest, NextResponse } from "next/server"
import { getCached, setCached, CACHE_KEYS, CACHE_TTL, hashQueryString } from "@/lib/redis"

export async function GET(request: NextRequest) {
    try {
        // Get query string and hash it for cache key
        const queryString = request.nextUrl.search || ""
        const queryHash = hashQueryString(queryString)
        const cacheKey = CACHE_KEYS.ADMIN_DOCUMENTS(queryHash)

        // Try to get from cache first
        const cached = await getCached(cacheKey)
        if (cached) {
            return NextResponse.json(cached, {
                headers: { "X-Cache": "HIT" },
            })
        }

        // Fetch from backend
        const apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000"
        const userToken = request.headers.get("X-Session-Token") || null
        const headers: Record<string, string> = {}
        if (userToken) {
            headers["X-Session-Token"] = userToken
        }

        const response = await fetch(`${apiBaseUrl}/api/admin/documents${queryString}`, { headers })

        if (!response.ok) {
            return NextResponse.json(
                { error: "Failed to fetch documents" },
                { status: response.status }
            )
        }

        const documents = await response.json()

        // Cache the result
        await setCached(cacheKey, documents, CACHE_TTL.ADMIN_DOCUMENTS)

        return NextResponse.json(documents, {
            headers: { "X-Cache": "MISS" },
        })
    } catch (error: any) {
        console.error("Documents cache error:", error)
        return NextResponse.json(
            { error: error.message || "Internal server error" },
            { status: 500 }
        )
    }
}