11import { NextRequest , NextResponse } from "next/server" ;
2- import { splitClient } from "@/lib/stellar" ;
3-
4- const PAGE_SIZE = 20 ;
2+ import { fetchInvoicesForAddress } from "@/lib/stellar/horizonServer" ;
53
64/**
75 * GET /api/invoices?cursor=<id>&limit=20&publicKey=<address>&q=<query>
@@ -19,9 +17,9 @@ const PAGE_SIZE = 20;
1917export async function GET ( request : NextRequest ) {
2018 const { searchParams } = request . nextUrl ;
2119 const publicKey = searchParams . get ( "publicKey" ) ;
22- const cursorParam = searchParams . get ( "cursor" ) ;
20+ const cursor = searchParams . get ( "cursor" ) ;
2321 const limitParam = searchParams . get ( "limit" ) ;
24- const q = searchParams . get ( "q" ) ?. trim ( ) . toLowerCase ( ) || "" ;
22+ const q = searchParams . get ( "q" ) ?? undefined ;
2523
2624 if ( ! publicKey ) {
2725 return NextResponse . json (
@@ -30,66 +28,12 @@ export async function GET(request: NextRequest) {
3028 ) ;
3129 }
3230
33- const limit = Math . min (
34- Math . max ( 1 , parseInt ( limitParam ?? String ( PAGE_SIZE ) , 10 ) || PAGE_SIZE ) ,
35- 50 ,
36- ) ;
37-
38- // Determine starting invoice id (cursor is the last id we already returned)
39- const startId = cursorParam ? parseInt ( cursorParam , 10 ) + 1 : 1 ;
40-
41- const results = [ ] ;
42- let lastCheckedId = startId - 1 ;
43-
44- for ( let id = startId ; results . length < limit ; id ++ ) {
45- // Safety cap — don't scan more than limit*10 ids in a single request
46- if ( id > startId + limit * 10 ) break ;
47-
48- lastCheckedId = id ;
49-
50- try {
51- const inv = await splitClient . getInvoice ( String ( id ) ) ;
52- const mine =
53- inv . creator === publicKey ||
54- inv . recipients . some ( ( r ) => r . address === publicKey ) ;
55-
56- if ( mine ) {
57- if ( q ) {
58- const memo = ( inv as any ) . memo as string | undefined ;
59- const matchesQuery =
60- ( inv . title || "" ) . toLowerCase ( ) . startsWith ( q ) ||
61- ( memo || "" ) . toLowerCase ( ) . startsWith ( q ) ;
62- if ( matchesQuery ) {
63- results . push ( inv ) ;
64- }
65- } else {
66- results . push ( inv ) ;
67- }
68- }
69- } catch {
70- // splitClient throws when invoice id does not exist — treat as end of list
71- return NextResponse . json (
72- { invoices : results , nextCursor : null } ,
73- {
74- headers : {
75- "Cache-Control" : "private, no-store" ,
76- } ,
77- } ,
78- ) ;
79- }
80- }
81-
82- // If we filled the page we don't know yet whether there are more — return the
83- // id of the last invoice we fetched so the client can continue from there.
84- const nextCursor =
85- results . length === limit ? String ( results [ results . length - 1 ] . id ) : null ;
31+ const limit = limitParam ? parseInt ( limitParam , 10 ) : undefined ;
32+ const result = await fetchInvoicesForAddress ( publicKey , { cursor, limit, q } ) ;
8633
87- return NextResponse . json (
88- { invoices : results , nextCursor } ,
89- {
90- headers : {
91- "Cache-Control" : "private, no-store" ,
92- } ,
34+ return NextResponse . json ( result , {
35+ headers : {
36+ "Cache-Control" : "private, no-store" ,
9337 } ,
94- ) ;
38+ } ) ;
9539}
0 commit comments