-
Notifications
You must be signed in to change notification settings - Fork 0
PM-1110 report for payment load #69
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
Open
vas3a
wants to merge
3
commits into
dev
Choose a base branch
from
PM-1110_report-for-payment-load
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Controller, Get, Query } from '@nestjs/common'; | ||
import { | ||
ApiTags, | ||
ApiBearerAuth, | ||
ApiOperation, | ||
ApiResponse, | ||
ApiQuery, | ||
} from '@nestjs/swagger'; | ||
import { ReportsService } from './reports.service'; | ||
import { ResponseDto } from 'src/dto/api-response.dto'; | ||
import { | ||
PaymentsReportQueryDto, | ||
PaymentsReportResponse, | ||
} from 'src/dto/reports.dto'; | ||
|
||
@ApiTags('Reports') | ||
@Controller('/reports') | ||
@ApiBearerAuth() | ||
export class ReportsController { | ||
constructor(private readonly reportsService: ReportsService) {} | ||
|
||
@Get('/payments') | ||
@ApiOperation({ | ||
summary: 'Export search winnings result in csv file format', | ||
description: 'Roles: Payment Admin, Payment Editor, Payment Viewer', | ||
}) | ||
@ApiQuery({ | ||
type: PaymentsReportQueryDto, | ||
}) | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Export winnings successfully.', | ||
type: ResponseDto<PaymentsReportResponse>, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
async getPaymentsReport(@Query() query: PaymentsReportQueryDto) { | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const report = await this.reportsService.getPaymentsReport(query); | ||
return report; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ReportsController } from './reports.controller'; | ||
import { ReportsService } from './reports.service'; | ||
import { TopcoderModule } from 'src/shared/topcoder/topcoder.module'; | ||
|
||
@Module({ | ||
imports: [TopcoderModule], | ||
controllers: [ReportsController], | ||
providers: [ReportsService], | ||
}) | ||
export class ReportsModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Prisma, winnings_category } from '@prisma/client'; | ||
import { isEmpty, uniq } from 'lodash'; | ||
import { PaymentsReportQueryDto } from 'src/dto/reports.dto'; | ||
import { Logger } from 'src/shared/global'; | ||
import { PrismaService } from 'src/shared/global/prisma.service'; | ||
import { BASIC_MEMBER_FIELDS } from 'src/shared/topcoder'; | ||
import { TopcoderChallengesService } from 'src/shared/topcoder/challenges.service'; | ||
import { TopcoderMembersService } from 'src/shared/topcoder/members.service'; | ||
|
||
@Injectable() | ||
export class ReportsService { | ||
private readonly logger = new Logger(ReportsService.name); | ||
|
||
constructor( | ||
private readonly prisma: PrismaService, | ||
private readonly membersService: TopcoderMembersService, | ||
private readonly challengeService: TopcoderChallengesService, | ||
) {} | ||
|
||
private async buildPaymentReportQueryFilters( | ||
filters: PaymentsReportQueryDto, | ||
) { | ||
const queryFilters: Prisma.paymentWhereInput = {}; | ||
|
||
if (filters.billingAccountIds) { | ||
Object.assign(queryFilters, { | ||
billing_account: { in: [...filters.billingAccountIds] }, | ||
}); | ||
} | ||
|
||
if (filters.handles) { | ||
const userIdsMap = await this.membersService.getMembersInfoByHandle( | ||
filters.handles, | ||
); | ||
|
||
if (!isEmpty(userIdsMap)) { | ||
Object.assign(queryFilters, { | ||
winnings: { | ||
winner_id: { | ||
in: Object.values(userIdsMap).map((u) => `${u.userId ?? ''}`), | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
if (filters.challengeName) { | ||
const challenges = await this.challengeService.searchByName( | ||
filters.challengeName, | ||
); | ||
|
||
Object.assign(queryFilters, { | ||
winnings: { | ||
...queryFilters.winnings, | ||
external_id: { in: challenges.map((c: { id: string }) => c.id) }, | ||
}, | ||
}); | ||
} | ||
|
||
if (filters.startDate || filters.endDate) { | ||
Object.assign(queryFilters, { | ||
created_at: { | ||
...(filters.startDate && { gte: filters.startDate }), | ||
...(filters.endDate && { lte: filters.endDate }), | ||
}, | ||
}); | ||
} | ||
|
||
if (filters.minPaymentAmount || filters.maxPaymentAmount) { | ||
Object.assign(queryFilters, { | ||
total_amount: { | ||
...(filters.minPaymentAmount && { gte: filters.minPaymentAmount }), | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...(filters.maxPaymentAmount && { lte: filters.maxPaymentAmount }), | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}); | ||
} | ||
|
||
return queryFilters; | ||
} | ||
|
||
async getPaymentsReport(filters: PaymentsReportQueryDto) { | ||
this.logger.debug('Starting getPaymentsReport with filters:', filters); | ||
|
||
const queryFilters = await this.buildPaymentReportQueryFilters(filters); | ||
|
||
const payments = await this.prisma.payment.findMany({ | ||
where: { | ||
...queryFilters, | ||
}, | ||
select: { | ||
payment_id: true, | ||
created_at: true, | ||
billing_account: true, | ||
payment_status: true, | ||
challenge_fee: true, | ||
total_amount: true, | ||
winnings: { | ||
select: { | ||
external_id: true, | ||
winner_id: true, | ||
category: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
this.logger.debug(`Fetched ${payments.length} payments from the database`); | ||
|
||
const userIds = uniq(payments.map((p) => p.winnings.winner_id as string)); | ||
const challengeIds = uniq( | ||
payments.map((p) => p.winnings.external_id as string).filter(Boolean), | ||
); | ||
|
||
this.logger.debug(`Extracted ${userIds.length} unique user IDs`); | ||
this.logger.debug(`Extracted ${challengeIds.length} unique challenge IDs`); | ||
|
||
const [membersMap, challengeNamesMap] = await Promise.all([ | ||
this.membersService.getMembersInfoByUserId(userIds, BASIC_MEMBER_FIELDS), | ||
this.challengeService.getChallengesNameByChallengeIds(challengeIds), | ||
]); | ||
|
||
this.logger.debug('Fetched member information and challenge names'); | ||
|
||
const result = payments.map((payment) => ({ | ||
billingAccountId: payment.billing_account, | ||
challengeName: | ||
challengeNamesMap[payment.winnings.external_id as string] ?? '', | ||
challengeId: payment.winnings.external_id as string, | ||
paymentDate: payment.created_at, | ||
paymentId: payment.payment_id, | ||
paymentStatus: payment.payment_status, | ||
winnerId: payment.winnings.winner_id, | ||
winnerHandle: membersMap[payment.winnings.winner_id]?.handle ?? '', | ||
winnerFirstName: membersMap[payment.winnings.winner_id]?.firstName ?? '', | ||
winnerLastName: membersMap[payment.winnings.winner_id]?.lastName ?? '', | ||
isTask: payment.winnings.category === winnings_category.TASK_PAYMENT, | ||
challengeFee: payment.challenge_fee, | ||
paymentAmount: payment.total_amount, | ||
})); | ||
|
||
this.logger.debug('Mapped payments to the final report format'); | ||
|
||
return result; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Transform } from 'class-transformer'; | ||
import { | ||
IsOptional, | ||
IsString, | ||
IsNotEmpty, | ||
IsNumber, | ||
IsDateString, | ||
} from 'class-validator'; | ||
|
||
const transformArray = ({ value }: { value: string }) => | ||
Array.isArray(value) ? value : [value]; | ||
|
||
export class PaymentsReportQueryDto { | ||
@ApiProperty({ | ||
description: | ||
'List of billing account IDs associated with the payments to retrieve', | ||
example: ['80001012'], | ||
}) | ||
@IsOptional() | ||
@IsString({ each: true }) | ||
@IsNotEmpty({ each: true }) | ||
@Transform(transformArray) | ||
billingAccountIds?: string[]; | ||
|
||
@ApiProperty({ | ||
description: 'Challenge name to search for', | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
example: ['Task Payment for member'], | ||
}) | ||
@IsOptional() | ||
@IsString() | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@IsNotEmpty() | ||
challengeName?: string; | ||
|
||
@ApiProperty({ | ||
description: 'List of challenge IDs', | ||
example: ['e74c3e37-73c9-474e-a838-a38dd4738906'], | ||
}) | ||
@IsOptional() | ||
@IsString({ each: true }) | ||
@IsNotEmpty({ each: true }) | ||
@Transform(transformArray) | ||
challengeIds?: string[]; | ||
|
||
@ApiProperty({ | ||
description: 'Start date for the report query in ISO format', | ||
example: '2023-01-01T00:00:00.000Z', | ||
}) | ||
@IsOptional() | ||
@IsDateString() | ||
startDate?: Date; | ||
|
||
@ApiProperty({ | ||
description: 'End date for the report query in ISO format', | ||
example: '2023-01-31T23:59:59.000Z', | ||
}) | ||
@IsOptional() | ||
@IsDateString() | ||
endDate?: Date; | ||
|
||
@ApiProperty({ | ||
description: 'List of user handles', | ||
example: ['user_01', 'user_02'], | ||
}) | ||
@IsOptional() | ||
@IsString({ each: true }) | ||
@IsNotEmpty({ each: true }) | ||
@Transform(transformArray) | ||
handles?: string[]; | ||
|
||
@ApiProperty({ | ||
description: 'Minimum payment amount for filtering the report', | ||
example: 100, | ||
}) | ||
@IsOptional() | ||
@IsNumber() | ||
@Transform(({ value }) => parseFloat(value)) | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
minPaymentAmount?: number; | ||
|
||
@ApiProperty({ | ||
description: 'Maximum payment amount for filtering the report', | ||
example: 1000, | ||
}) | ||
@IsOptional() | ||
@IsNumber() | ||
@Transform(({ value }) => parseFloat(value)) | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
maxPaymentAmount?: number; | ||
} | ||
|
||
export class PaymentsReportResponse { | ||
billingAccountId: string; | ||
challengeName: string; | ||
challengeId: string; | ||
paymentDate: string; | ||
paymentId: string; | ||
paymentStatus: string; | ||
winnerId: string; | ||
winnerHandle: string; | ||
winnerFirstName: string; | ||
winnerLastName: string; | ||
isTask: boolean; | ||
challengeFee: number; | ||
paymentAmount: number; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.