- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
Fix transaction #104
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
Fix transaction #104
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 | 
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { payment_method_status, user_payment_methods } from '@prisma/client'; | ||
| import { | ||
| payment_method_status, | ||
| Prisma, | ||
| user_payment_methods, | ||
| } from '@prisma/client'; | ||
| import { PrismaService } from 'src/shared/global/prisma.service'; | ||
|  | ||
| @Injectable() | ||
|  | @@ -14,14 +18,16 @@ export class PaymentMethodRepository { | |
| */ | ||
| async getConnectedPaymentMethod( | ||
| userId: string, | ||
| tx?: Prisma.TransactionClient, | ||
| ): Promise<user_payment_methods | null> { | ||
| const connectedUserPaymentMethod = | ||
| await this.prisma.user_payment_methods.findFirst({ | ||
| where: { | ||
| user_id: userId, | ||
| status: payment_method_status.CONNECTED, | ||
| }, | ||
| }); | ||
| const connectedUserPaymentMethod = await ( | ||
| tx || this.prisma | ||
| 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. [💡  | ||
| ).user_payment_methods.findFirst({ | ||
| where: { | ||
| user_id: userId, | ||
| status: payment_method_status.CONNECTED, | ||
| }, | ||
| }); | ||
|  | ||
| return connectedUserPaymentMethod; | ||
| } | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { tax_form_status } from '@prisma/client'; | ||
| import { Prisma, tax_form_status } from '@prisma/client'; | ||
| import { PrismaService } from 'src/shared/global/prisma.service'; | ||
|  | ||
| @Injectable() | ||
|  | @@ -12,8 +12,11 @@ export class TaxFormRepository { | |
| * @param userId user id | ||
| * @returns true if user has active tax form | ||
| */ | ||
| async hasActiveTaxForm(userId: string): Promise<boolean> { | ||
| const count = await this.prisma.user_tax_form_associations.count({ | ||
| async hasActiveTaxForm( | ||
| userId: string, | ||
| tx?: Prisma.TransactionClient, | ||
| ): Promise<boolean> { | ||
| const count = await (tx || this.prisma).user_tax_form_associations.count({ | ||
| 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. [💡  | ||
| where: { | ||
| user_id: userId, | ||
| tax_form_status: tax_form_status.ACTIVE, | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -88,8 +88,13 @@ export class WinningsService { | |
| } | ||
| } | ||
|  | ||
| private async setPayrollPaymentMethod(userId: string) { | ||
| const payrollPaymentMethod = await this.prisma.payment_method.findFirst({ | ||
| private async setPayrollPaymentMethod( | ||
| userId: string, | ||
| tx?: Prisma.TransactionClient, | ||
| 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 payrollPaymentMethod = await ( | ||
| tx || this.prisma | ||
| ).payment_method.findFirst({ | ||
| where: { | ||
| payment_method_type: 'Wipro Payroll', | ||
| }, | ||
|  | @@ -101,7 +106,7 @@ export class WinningsService { | |
| } | ||
|  | ||
| if ( | ||
| await this.prisma.user_payment_methods.findFirst({ | ||
| await (tx || this.prisma).user_payment_methods.findFirst({ | ||
| 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. [❗❗  | ||
| where: { | ||
| user_id: userId, | ||
| payment_method_id: payrollPaymentMethod.payment_method_id, | ||
|  | @@ -180,13 +185,18 @@ export class WinningsService { | |
|  | ||
| const hasActiveTaxForm = await this.taxFormRepo.hasActiveTaxForm( | ||
| body.winnerId, | ||
| tx, | ||
| 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 hasConnectedPaymentMethod = Boolean( | ||
| await this.paymentMethodRepo.getConnectedPaymentMethod(body.winnerId), | ||
| await this.paymentMethodRepo.getConnectedPaymentMethod( | ||
| body.winnerId, | ||
| tx, | ||
| 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 isIdentityVerified = | ||
| await this.identityVerificationRepo.completedIdentityVerification( | ||
| body.winnerId, | ||
| tx, | ||
| 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. [ | ||
| ); | ||
|  | ||
| for (const detail of body.details || []) { | ||
|  | @@ -213,7 +223,7 @@ export class WinningsService { | |
| `Payroll payment detected. Setting payment status to PAID for user ${body.winnerId}`, | ||
| ); | ||
| paymentModel.payment_status = PaymentStatus.PAID; | ||
| await this.setPayrollPaymentMethod(body.winnerId); | ||
| await this.setPayrollPaymentMethod(body.winnerId, tx); | ||
| 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. [❗❗  | ||
| } | ||
|  | ||
| winningModel.payment.create.push(paymentModel); | ||
|  | @@ -223,7 +233,7 @@ export class WinningsService { | |
| } | ||
|  | ||
| this.logger.debug('Attempting to create winning with nested payments.'); | ||
| const createdWinning = await this.prisma.winnings.create({ | ||
| const createdWinning = await tx.winnings.create({ | ||
| 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. [❗❗  | ||
| data: winningModel 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 documenting the
txparameter to clarify its purpose and usage. This will improve the maintainability and readability of the function, especially for developers unfamiliar with transaction handling.