@@ -21,6 +21,11 @@ import { Logger } from 'src/shared/global';
2121
2222const ONE_DAY = 24 * 60 * 60 * 1000 ;
2323
24+ interface SearchWinningsOptions {
25+ includeCount ?: boolean ;
26+ includePayoutStatus ?: boolean ;
27+ }
28+
2429@Injectable ( )
2530export class WinningsRepository {
2631 private readonly logger = new Logger ( WinningsRepository . name ) ;
@@ -162,8 +167,11 @@ export class WinningsRepository {
162167 */
163168 async searchWinnings (
164169 searchProps : WinningRequestDto ,
170+ options : SearchWinningsOptions = { } ,
165171 ) : Promise < ResponseDto < SearchWinningResult > > {
166172 const result = new ResponseDto < SearchWinningResult > ( ) ;
173+ const includeCount = options . includeCount ?? true ;
174+ const includePayoutStatus = options . includePayoutStatus ?? true ;
167175
168176 try {
169177 let winnerIds : string [ ] | undefined ;
@@ -202,7 +210,9 @@ export class WinningsRepository {
202210 ! winnerIds && ! ! externalIds ?. length ,
203211 ) ;
204212
205- const winnings = await this . prisma . winnings . findMany ( {
213+ const limit = searchProps . limit ?? 10 ;
214+ const offset = searchProps . offset ?? 0 ;
215+ const winningsPromise = this . prisma . winnings . findMany ( {
206216 where : queryWhere ,
207217 include : {
208218 payment : {
@@ -218,15 +228,22 @@ export class WinningsRepository {
218228 origin : true ,
219229 } ,
220230 orderBy,
221- skip : searchProps . offset ,
222- take : searchProps . limit ,
231+ skip : offset ,
232+ take : limit ,
223233 } ) ;
224234
225- const count = await this . prisma . winnings . count ( { where : queryWhere } ) ;
235+ const [ winnings , count ] = includeCount
236+ ? await Promise . all ( [
237+ winningsPromise ,
238+ this . prisma . winnings . count ( { where : queryWhere } ) ,
239+ ] )
240+ : [ await winningsPromise , 0 ] ;
226241
227- const usersPayoutStatusMap = winnings ?. length
228- ? await this . getUsersPayoutStatusForWinnings ( winnings )
229- : ( { } as { [ key : string ] : payment_status } ) ;
242+ const usersPayoutStatusMap : Record < string , unknown > =
243+ includePayoutStatus && winnings ?. length
244+ ? await this . getUsersPayoutStatusForWinnings ( winnings )
245+ : { } ;
246+ const totalItems = includeCount ? count : winnings . length ;
230247
231248 result . data = {
232249 winnings : winnings . map ( ( item ) => ( {
@@ -257,13 +274,15 @@ export class WinningsRepository {
257274 item . payment ?. [ 0 ] . updated_at ??
258275 undefined ) as Date ,
259276 releaseDate : item . payment ?. [ 0 ] ?. release_date as Date ,
260- paymentStatus : usersPayoutStatusMap [ item . winner_id ] ,
277+ paymentStatus : usersPayoutStatusMap [
278+ item . winner_id
279+ ] as WinningDto [ 'paymentStatus' ] ,
261280 } ) ) ,
262281 pagination : {
263- totalItems : count ,
264- totalPages : Math . ceil ( count / searchProps . limit ) ,
265- pageSize : searchProps . limit ,
266- currentPage : Math . ceil ( searchProps . offset / searchProps . limit ) + 1 ,
282+ totalItems,
283+ totalPages : Math . ceil ( totalItems / limit ) ,
284+ pageSize : limit ,
285+ currentPage : Math . ceil ( offset / limit ) + 1 ,
267286 } ,
268287 } ;
269288 // response.data = winnings as any
0 commit comments