diff --git a/src/api/repository/identity-verification.repo.ts b/src/api/repository/identity-verification.repo.ts index 4c1fd92..6985aa6 100644 --- a/src/api/repository/identity-verification.repo.ts +++ b/src/api/repository/identity-verification.repo.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { verification_status } from '@prisma/client'; +import { Prisma, verification_status } from '@prisma/client'; import { PrismaService } from 'src/shared/global/prisma.service'; @Injectable() @@ -12,14 +12,18 @@ export class IdentityVerificationRepository { * @param userId - The unique identifier of the user. * @returns A promise that resolves to `true` if the user has at least one active identity verification association, otherwise `false`. */ - async completedIdentityVerification(userId: string): Promise { - const count = - await this.prisma.user_identity_verification_associations.count({ - where: { - user_id: userId, - verification_status: verification_status.ACTIVE, - }, - }); + async completedIdentityVerification( + userId: string, + tx?: Prisma.TransactionClient, + ): Promise { + const count = await ( + tx || this.prisma + ).user_identity_verification_associations.count({ + where: { + user_id: userId, + verification_status: verification_status.ACTIVE, + }, + }); return count > 0; } diff --git a/src/api/repository/paymentMethod.repo.ts b/src/api/repository/paymentMethod.repo.ts index bf61af1..92a292f 100644 --- a/src/api/repository/paymentMethod.repo.ts +++ b/src/api/repository/paymentMethod.repo.ts @@ -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 { - const connectedUserPaymentMethod = - await this.prisma.user_payment_methods.findFirst({ - where: { - user_id: userId, - status: payment_method_status.CONNECTED, - }, - }); + const connectedUserPaymentMethod = await ( + tx || this.prisma + ).user_payment_methods.findFirst({ + where: { + user_id: userId, + status: payment_method_status.CONNECTED, + }, + }); return connectedUserPaymentMethod; } diff --git a/src/api/repository/taxForm.repo.ts b/src/api/repository/taxForm.repo.ts index 59df1db..fb39884 100644 --- a/src/api/repository/taxForm.repo.ts +++ b/src/api/repository/taxForm.repo.ts @@ -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 { - const count = await this.prisma.user_tax_form_associations.count({ + async hasActiveTaxForm( + userId: string, + tx?: Prisma.TransactionClient, + ): Promise { + const count = await (tx || this.prisma).user_tax_form_associations.count({ where: { user_id: userId, tax_form_status: tax_form_status.ACTIVE, diff --git a/src/api/winnings/winnings.service.ts b/src/api/winnings/winnings.service.ts index b4b969a..32e70e3 100644 --- a/src/api/winnings/winnings.service.ts +++ b/src/api/winnings/winnings.service.ts @@ -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, + ) { + 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({ 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, ); const hasConnectedPaymentMethod = Boolean( - await this.paymentMethodRepo.getConnectedPaymentMethod(body.winnerId), + await this.paymentMethodRepo.getConnectedPaymentMethod( + body.winnerId, + tx, + ), ); const isIdentityVerified = await this.identityVerificationRepo.completedIdentityVerification( body.winnerId, + tx, ); 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); } 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({ data: winningModel as any, });