forked from Stellar-split/split-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
58 lines (49 loc) · 1.41 KB
/
Copy pathroute.ts
File metadata and controls
58 lines (49 loc) · 1.41 KB
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
53
54
55
56
57
58
import { NextRequest, NextResponse } from "next/server";
import { splitClient } from "@/lib/stellar";
const DEFAULT_LIMIT = 10;
interface HistoryResponse {
payments: Array<{
payer: string;
amount: bigint;
timestamp?: number;
}>;
nextCursor: string | null;
}
export async function GET(
request: NextRequest,
{ params }: { params: { id: string } }
) {
const { searchParams } = request.nextUrl;
const cursor = searchParams.get("cursor");
const limitParam = searchParams.get("limit");
const limit = Math.min(
Math.max(1, parseInt(limitParam ?? String(DEFAULT_LIMIT), 10) || DEFAULT_LIMIT),
50,
);
try {
const invoice = await splitClient.getInvoice(params.id);
// Parse cursor to get starting index
const startIdx = cursor ? parseInt(cursor, 10) : 0;
// Get the requested slice of payments
const endIdx = startIdx + limit;
const paymentsSlice = invoice.payments.slice(startIdx, endIdx);
// Determine if there are more payments
const nextCursor = endIdx < invoice.payments.length ? String(endIdx) : null;
return NextResponse.json(
{
payments: paymentsSlice,
nextCursor,
} as HistoryResponse,
{
headers: {
"Cache-Control": "private, no-store",
},
}
);
} catch (error) {
return NextResponse.json(
{ error: "Failed to fetch invoice history" },
{ status: 500 }
);
}
}