-
Notifications
You must be signed in to change notification settings - Fork 0
PM-1201 - sync payments with legacy #60
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AdminController } from './admin.controller'; | ||
import { AdminService } from './admin.service'; | ||
import { TaxFormRepository } from '../repository/taxForm.repo'; | ||
import { PaymentMethodRepository } from '../repository/paymentMethod.repo'; | ||
import { WinningsRepository } from '../repository/winnings.repo'; | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { TopcoderModule } from 'src/shared/topcoder/topcoder.module'; | ||
import { PaymentsModule } from 'src/shared/payments'; | ||
|
||
@Module({ | ||
imports: [TopcoderModule, PaymentsModule], | ||
controllers: [AdminController], | ||
providers: [ | ||
AdminService, | ||
TaxFormRepository, | ||
PaymentMethodRepository, | ||
WinningsRepository, | ||
], | ||
providers: [AdminService, WinningsRepository], | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
export class AdminModule {} |
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 |
---|---|---|
|
@@ -6,16 +6,18 @@ import { | |
Logger, | ||
} from '@nestjs/common'; | ||
|
||
import { PrismaPromise } from '@prisma/client'; | ||
import { Prisma } from '@prisma/client'; | ||
import { PrismaService } from 'src/shared/global/prisma.service'; | ||
import { PaymentsService } from 'src/shared/payments'; | ||
|
||
import { TaxFormRepository } from '../repository/taxForm.repo'; | ||
import { PaymentMethodRepository } from '../repository/paymentMethod.repo'; | ||
import { ResponseDto } from 'src/dto/api-response.dto'; | ||
import { PaymentStatus } from 'src/dto/payment.dto'; | ||
import { WinningAuditDto, AuditPayoutDto } from './dto/audit.dto'; | ||
import { WinningUpdateRequestDto } from './dto/winnings.dto'; | ||
import { | ||
AdminPaymentUpdateData, | ||
TopcoderChallengesService, | ||
} from 'src/shared/topcoder/challenges.service'; | ||
|
||
/** | ||
* The admin winning service. | ||
|
@@ -30,11 +32,14 @@ export class AdminService { | |
*/ | ||
constructor( | ||
private readonly prisma: PrismaService, | ||
private readonly taxFormRepo: TaxFormRepository, | ||
private readonly paymentMethodRepo: PaymentMethodRepository, | ||
private readonly paymentsService: PaymentsService, | ||
private readonly tcChallengesService: TopcoderChallengesService, | ||
) {} | ||
|
||
private getWinningById(winningId: string) { | ||
return this.prisma.winnings.findFirst({ where: { winning_id: winningId } }); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private getPaymentsByWinningsId(winningsId: string, paymentId?: string) { | ||
return this.prisma.payment.findMany({ | ||
where: { | ||
|
@@ -79,7 +84,9 @@ export class AdminService { | |
releaseDate = await this.getPaymentReleaseDateByWinningsId(winningsId); | ||
} | ||
|
||
const transactions: PrismaPromise<any>[] = []; | ||
const transactions: (( | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tx: Prisma.TransactionClient, | ||
) => Promise<unknown>)[] = []; | ||
const now = new Date().getTime(); | ||
payments.forEach((payment) => { | ||
if ( | ||
|
@@ -108,8 +115,8 @@ export class AdminService { | |
if (sinceRelease < 12) { | ||
errMessage = `Cannot put a processing payment back to owed, unless it's been processing for at least 12 hours. Currently it's only been ${sinceRelease.toFixed(1)} hours`; | ||
} else { | ||
transactions.push( | ||
this.markPaymentReleaseAsFailedByWinningsId(winningsId), | ||
transactions.push((tx) => | ||
this.markPaymentReleaseAsFailedByWinningsId(winningsId, tx), | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} else { | ||
|
@@ -137,30 +144,32 @@ export class AdminService { | |
throw new BadRequestException(errMessage); | ||
} | ||
|
||
transactions.push( | ||
transactions.push((tx) => | ||
this.updatePaymentStatus( | ||
userId, | ||
winningsId, | ||
payment.payment_id, | ||
payment.payment_status, | ||
body.paymentStatus, | ||
version, | ||
version++, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tx, | ||
), | ||
); | ||
version += 1; | ||
|
||
paymentStatus = body.paymentStatus as PaymentStatus; | ||
|
||
if (body.paymentStatus === PaymentStatus.OWED) { | ||
needsReconciliation = true; | ||
} | ||
|
||
if (payment.installment_number === 1) { | ||
transactions.push( | ||
transactions.push((tx) => | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.addAudit( | ||
userId, | ||
winningsId, | ||
`Modified payment status from ${payment.payment_status} to ${body.paymentStatus}`, | ||
body.auditNote, | ||
tx, | ||
), | ||
); | ||
} | ||
|
@@ -186,24 +195,25 @@ export class AdminService { | |
); | ||
} | ||
|
||
transactions.push( | ||
transactions.push((tx) => | ||
this.updateReleaseDate( | ||
userId, | ||
winningsId, | ||
payment.payment_id, | ||
newReleaseDate, | ||
version, | ||
version++, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tx, | ||
), | ||
); | ||
version += 1; | ||
|
||
if (payment.installment_number === 1) { | ||
transactions.push( | ||
transactions.push((tx) => | ||
this.addAudit( | ||
userId, | ||
winningsId, | ||
`Modified release date from ${payment.release_date?.toISOString()} to ${newReleaseDate.toISOString()}`, | ||
body.auditNote, | ||
tx, | ||
), | ||
); | ||
} | ||
|
@@ -218,7 +228,7 @@ export class AdminService { | |
) { | ||
// ideally we should be maintaining the original split of the payment amount between installments - but we aren't really using splits anymore | ||
if (payment.installment_number === 1) { | ||
transactions.push( | ||
transactions.push((tx) => | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.updatePaymentAmount( | ||
userId, | ||
winningsId, | ||
|
@@ -227,20 +237,22 @@ export class AdminService { | |
body.paymentAmount, | ||
body.paymentAmount, | ||
version, | ||
tx, | ||
), | ||
); | ||
|
||
transactions.push( | ||
transactions.push((tx) => | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.addAudit( | ||
userId, | ||
winningsId, | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
`Modified payment amount from ${payment.total_amount} to ${body.paymentAmount.toFixed(2)}`, | ||
body.auditNote, | ||
tx, | ||
), | ||
); | ||
} else { | ||
transactions.push( | ||
transactions.push((tx) => | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.updatePaymentAmount( | ||
userId, | ||
winningsId, | ||
|
@@ -249,15 +261,43 @@ export class AdminService { | |
0, | ||
body.paymentAmount, | ||
version, | ||
tx, | ||
), | ||
); | ||
} | ||
} | ||
}); | ||
|
||
if (transactions.length > 0) { | ||
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. FYI - needed to update the way the DBtransaction is handled from batch transactions to one big transaction in order to be able to fail the transaction if the legacy update failed (this is happening only for admin updates only, for withdraw, we're just logging the error). |
||
await this.prisma.$transaction(transactions); | ||
} | ||
transactions.push(async () => { | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const winning = await this.getWinningById(winningsId); | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!winning) { | ||
this.logger.error( | ||
`Error updating legacy system for winning ${winningsId}. Winning not found!`, | ||
); | ||
throw new Error( | ||
`Error updating legacy system for winning ${winningsId}. Winning not found!`, | ||
); | ||
} | ||
|
||
const payoutData: AdminPaymentUpdateData = { | ||
userId: +userId, | ||
status: body.paymentStatus, | ||
amount: body.paymentAmount, | ||
releaseDate: body.releaseDate, | ||
}; | ||
|
||
await this.tcChallengesService.updateLegacyPayments( | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
winning.external_id as string, | ||
payoutData, | ||
); | ||
}); | ||
|
||
// Run all transaction tasks in a single prisma transaction | ||
await this.prisma.$transaction(async (tx) => { | ||
for (const transaction of transactions) { | ||
await transaction(tx); | ||
} | ||
}); | ||
|
||
if (needsReconciliation) { | ||
const winning = await this.prisma.winnings.findFirst({ | ||
|
@@ -320,8 +360,11 @@ export class AdminService { | |
return paymentReleases?.release_date; | ||
} | ||
|
||
private markPaymentReleaseAsFailedByWinningsId(winningsId: string) { | ||
return this.prisma.payment_releases.updateMany({ | ||
private markPaymentReleaseAsFailedByWinningsId( | ||
winningsId: string, | ||
tx?: Prisma.TransactionClient, | ||
) { | ||
return (tx ?? this.prisma).payment_releases.updateMany({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where: { | ||
payment_release_associations: { | ||
some: { | ||
|
@@ -346,19 +389,22 @@ export class AdminService { | |
oldPaymentStatus: string | null, | ||
newPaymentStatus: PaymentStatus, | ||
currentVersion: number, | ||
tx?: Prisma.TransactionClient, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
let setDatePaidNull = false; | ||
if ([ | ||
PaymentStatus.PAID, | ||
PaymentStatus.PROCESSING, | ||
PaymentStatus.RETURNED, | ||
PaymentStatus.FAILED, | ||
].includes(oldPaymentStatus as PaymentStatus) && | ||
if ( | ||
[ | ||
PaymentStatus.PAID, | ||
PaymentStatus.PROCESSING, | ||
PaymentStatus.RETURNED, | ||
PaymentStatus.FAILED, | ||
].includes(oldPaymentStatus as PaymentStatus) && | ||
newPaymentStatus === PaymentStatus.OWED | ||
) { | ||
setDatePaidNull = true; | ||
} | ||
return this.prisma.payment.update({ | ||
|
||
return (tx ?? this.prisma).payment.update({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where: { | ||
payment_id: paymentId, | ||
winnings_id: winningsId, | ||
|
@@ -379,8 +425,9 @@ export class AdminService { | |
winningsId: string, | ||
action: string, | ||
auditNote?: string, | ||
tx?: Prisma.TransactionClient, | ||
) { | ||
return this.prisma.audit.create({ | ||
return (tx ?? this.prisma).audit.create({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: { | ||
user_id: userId, | ||
winnings_id: winningsId, | ||
|
@@ -396,8 +443,9 @@ export class AdminService { | |
paymentId: string, | ||
newReleaseDate: Date, | ||
currentVersion: number, | ||
tx?: Prisma.TransactionClient, | ||
) { | ||
return this.prisma.payment.update({ | ||
return (tx ?? this.prisma).payment.update({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where: { | ||
payment_id: paymentId, | ||
winnings_id: winningsId, | ||
|
@@ -427,8 +475,9 @@ export class AdminService { | |
grossAmount: number, | ||
totalAmount: number, | ||
currentVersion: number, | ||
tx?: Prisma.TransactionClient, | ||
) { | ||
return this.prisma.payment.update({ | ||
return (tx ?? this.prisma).payment.update({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where: { | ||
payment_id: paymentId, | ||
winnings_id: winningsId, | ||
|
@@ -461,11 +510,12 @@ export class AdminService { | |
*/ | ||
async getWinningAudit( | ||
winningId: string, | ||
tx?: Prisma.TransactionClient, | ||
): Promise<ResponseDto<WinningAuditDto[]>> { | ||
const result = new ResponseDto<WinningAuditDto[]>(); | ||
|
||
try { | ||
const audits = await this.prisma.audit.findMany({ | ||
const audits = await (tx ?? this.prisma).audit.findMany({ | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where: { | ||
winnings_id: { | ||
equals: winningId, | ||
|
@@ -501,11 +551,14 @@ export class AdminService { | |
*/ | ||
async getWinningAuditPayout( | ||
winningId: string, | ||
tx?: Prisma.TransactionClient, | ||
vas3a marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): Promise<ResponseDto<AuditPayoutDto[]>> { | ||
const result = new ResponseDto<AuditPayoutDto[]>(); | ||
|
||
try { | ||
const paymentReleases = await this.prisma.payment_releases.findMany({ | ||
const paymentReleases = await ( | ||
tx ?? this.prisma | ||
).payment_releases.findMany({ | ||
where: { | ||
payment_release_associations: { | ||
some: { | ||
|
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
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.