@@ -11,15 +11,14 @@ type GetListBorrowsQuery = QueryParams<
1111 library_id ?: string
1212 status ?: 'active' | 'overdue' | 'returned' | 'lost'
1313 user_id ?: string
14+ returned_at ?: string
15+ lost_at ?: string
1416 }
1517>
1618type GetListBorrowsResponse = Promise < ResList < Borrow > >
1719
1820export const getListBorrows = async (
19- {
20- status,
21- ...query
22- } : GetListBorrowsQuery & { returned_at ?: string ; lost_at ?: string } ,
21+ { status, ...query } : GetListBorrowsQuery ,
2322 init ?: RequestInit
2423) : GetListBorrowsResponse => {
2524 const url = new URL ( BORROW_URL )
@@ -50,19 +49,52 @@ export const getListBorrows = async (
5049 return response . json ( )
5150}
5251
53- type GetBorrowQuery = Pick < Borrow , 'id' >
52+ export type GetBorrowQuery = Pick < Borrow , 'id' > &
53+ Pick <
54+ GetListBorrowsQuery ,
55+ | 'book_id'
56+ | 'subscription_id'
57+ | 'user_id'
58+ | 'library_id'
59+ | 'borrowed_at'
60+ | 'due_at'
61+ | 'returned_at'
62+ | 'lost_at'
63+ | 'is_active'
64+ | 'is_expired'
65+ | 'sort_by'
66+ | 'sort_in'
67+ | 'status'
68+ >
5469type GetBorrowResponse = Promise < ResSingle < BorrowDetail > >
5570export const getBorrow = async (
56- query : GetBorrowQuery ,
71+ { id , status , ... query } : GetBorrowQuery ,
5772 init ?: RequestInit
5873) : GetBorrowResponse => {
59- const url = new URL ( `${ BORROW_URL } /${ query . id } ` )
74+ const url = new URL ( `${ BORROW_URL } /${ id } ` )
6075 const headers = new Headers ( init ?. headers )
6176 headers . set ( 'Content-Type' , 'application/json' )
6277 init = {
6378 ...init ,
6479 headers,
6580 }
81+ Object . entries ( query ) . forEach ( ( [ key , value ] ) => {
82+ if ( value ) {
83+ url . searchParams . append ( key , String ( value ) )
84+ }
85+ } )
86+
87+ if ( status ) {
88+ if ( status === 'active' ) {
89+ url . searchParams . append ( 'is_active' , 'true' )
90+ } else if ( status === 'overdue' ) {
91+ url . searchParams . append ( 'is_overdue' , 'true' )
92+ } else if ( status === 'returned' ) {
93+ url . searchParams . append ( 'is_returned' , 'true' )
94+ } else if ( status === 'lost' ) {
95+ url . searchParams . append ( 'is_lost' , 'true' )
96+ }
97+ }
6698 const response = await fetch ( url . toString ( ) , init )
6799 if ( ! response . ok ) {
68100 const e = await response . json ( )
0 commit comments