-
Notifications
You must be signed in to change notification settings - Fork 1
Export optimization (PS-530) #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { | |
| HttpCode, | ||
| HttpStatus, | ||
| BadRequestException, | ||
| InternalServerErrorException, | ||
| } from '@nestjs/common'; | ||
| import { | ||
| ApiOperation, | ||
|
|
@@ -37,6 +38,8 @@ import { AccessControlService } from 'src/shared/access-control'; | |
| @Controller('/admin') | ||
| @ApiBearerAuth() | ||
| export class AdminController { | ||
| private static readonly EXPORT_BATCH_SIZE = 1000; | ||
|
|
||
| constructor( | ||
| private readonly adminService: AdminService, | ||
| private readonly winningsRepo: WinningsRepository, | ||
|
|
@@ -113,22 +116,53 @@ export class AdminController { | |
| @Header('Content-Type', 'text/csv') | ||
| @Header('Content-Disposition', 'attachment; filename="winnings.csv"') | ||
| async exportWinnings(@Body() body: WinningRequestDto, @User() user: any) { | ||
| const filters = | ||
| const baseFilters = | ||
| await this.accessControlService.applyFilters<WinningRequestDto>( | ||
| user.id, | ||
| user.roles, | ||
| { | ||
| ...body, | ||
| limit: 999, | ||
| limit: undefined, | ||
| offset: undefined, | ||
| }, | ||
| ); | ||
| const result = await this.winningsRepo.searchWinnings(filters); | ||
|
|
||
| const winnings: SearchWinningResult['winnings'] = []; | ||
| let offset = 0; | ||
|
|
||
| while (true) { | ||
| const result = await this.winningsRepo.searchWinnings( | ||
| { | ||
| ...baseFilters, | ||
| limit: AdminController.EXPORT_BATCH_SIZE, | ||
| offset, | ||
| }, | ||
| { | ||
| includeCount: false, | ||
| includePayoutStatus: false, | ||
| }, | ||
| ); | ||
|
|
||
| if (result.error || !result.data) { | ||
| throw new InternalServerErrorException( | ||
| result.error?.message ?? 'Export winnings failed', | ||
| ); | ||
| } | ||
|
|
||
| winnings.push(...result.data.winnings); | ||
|
|
||
| if (result.data.winnings.length < AdminController.EXPORT_BATCH_SIZE) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| break; | ||
| } | ||
|
|
||
| offset += AdminController.EXPORT_BATCH_SIZE; | ||
| } | ||
|
|
||
| const handles = await this.tcMembersService.getHandlesByUserIds( | ||
| result.data.winnings.map((d) => d.winnerId), | ||
| winnings.map((d) => d.winnerId), | ||
| ); | ||
|
|
||
| const csvRes = result.data.winnings.map((item) => { | ||
| const csvRes = winnings.map((item) => { | ||
| const payment = | ||
| item.details && item.details.length > 0 ? item.details[0] : null; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,11 @@ import { Logger } from 'src/shared/global'; | |
|
|
||
| const ONE_DAY = 24 * 60 * 60 * 1000; | ||
|
|
||
| interface SearchWinningsOptions { | ||
| includeCount?: boolean; | ||
| includePayoutStatus?: boolean; | ||
| } | ||
|
|
||
| @Injectable() | ||
| export class WinningsRepository { | ||
| private readonly logger = new Logger(WinningsRepository.name); | ||
|
|
@@ -162,8 +167,11 @@ export class WinningsRepository { | |
| */ | ||
| async searchWinnings( | ||
| searchProps: WinningRequestDto, | ||
| options: SearchWinningsOptions = {}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| ): Promise<ResponseDto<SearchWinningResult>> { | ||
| const result = new ResponseDto<SearchWinningResult>(); | ||
| const includeCount = options.includeCount ?? true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| const includePayoutStatus = options.includePayoutStatus ?? true; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
|
|
||
| try { | ||
| let winnerIds: string[] | undefined; | ||
|
|
@@ -202,7 +210,9 @@ export class WinningsRepository { | |
| !winnerIds && !!externalIds?.length, | ||
| ); | ||
|
|
||
| const winnings = await this.prisma.winnings.findMany({ | ||
| const limit = searchProps.limit ?? 10; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| const offset = searchProps.offset ?? 0; | ||
| const winningsPromise = this.prisma.winnings.findMany({ | ||
| where: queryWhere, | ||
| include: { | ||
| payment: { | ||
|
|
@@ -218,15 +228,22 @@ export class WinningsRepository { | |
| origin: true, | ||
| }, | ||
| orderBy, | ||
| skip: searchProps.offset, | ||
| take: searchProps.limit, | ||
| skip: offset, | ||
| take: limit, | ||
| }); | ||
|
|
||
| const count = await this.prisma.winnings.count({ where: queryWhere }); | ||
| const [winnings, count] = includeCount | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| ? await Promise.all([ | ||
| winningsPromise, | ||
| this.prisma.winnings.count({ where: queryWhere }), | ||
| ]) | ||
| : [await winningsPromise, 0]; | ||
|
|
||
| const usersPayoutStatusMap = winnings?.length | ||
| ? await this.getUsersPayoutStatusForWinnings(winnings) | ||
| : ({} as { [key: string]: payment_status }); | ||
| const usersPayoutStatusMap: Record<string, unknown> = | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [💡 |
||
| includePayoutStatus && winnings?.length | ||
| ? await this.getUsersPayoutStatusForWinnings(winnings) | ||
| : {}; | ||
| const totalItems = includeCount ? count : winnings.length; | ||
|
|
||
| result.data = { | ||
| winnings: winnings.map((item) => ({ | ||
|
|
@@ -257,13 +274,15 @@ export class WinningsRepository { | |
| item.payment?.[0].updated_at ?? | ||
| undefined) as Date, | ||
| releaseDate: item.payment?.[0]?.release_date as Date, | ||
| paymentStatus: usersPayoutStatusMap[item.winner_id], | ||
| paymentStatus: usersPayoutStatusMap[ | ||
| item.winner_id | ||
| ] as WinningDto['paymentStatus'], | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| })), | ||
| pagination: { | ||
| totalItems: count, | ||
| totalPages: Math.ceil(count / searchProps.limit), | ||
| pageSize: searchProps.limit, | ||
| currentPage: Math.ceil(searchProps.offset / searchProps.limit) + 1, | ||
| totalItems, | ||
| totalPages: Math.ceil(totalItems / limit), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [❗❗ |
||
| pageSize: limit, | ||
| currentPage: Math.ceil(offset / limit) + 1, | ||
| }, | ||
| }; | ||
| // response.data = winnings as any | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[⚠️
maintainability]Consider logging the error details before throwing the
InternalServerErrorException. This can help with debugging and monitoring by providing more context about the failure.