Skip to content

Commit 668d89f

Browse files
authored
Merge pull request #104 from topcoder-platform/fix-transaction
Fix transaction
2 parents 62b571b + d7d3012 commit 668d89f

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

src/api/repository/identity-verification.repo.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2-
import { verification_status } from '@prisma/client';
2+
import { Prisma, verification_status } from '@prisma/client';
33
import { PrismaService } from 'src/shared/global/prisma.service';
44

55
@Injectable()
@@ -12,14 +12,18 @@ export class IdentityVerificationRepository {
1212
* @param userId - The unique identifier of the user.
1313
* @returns A promise that resolves to `true` if the user has at least one active identity verification association, otherwise `false`.
1414
*/
15-
async completedIdentityVerification(userId: string): Promise<boolean> {
16-
const count =
17-
await this.prisma.user_identity_verification_associations.count({
18-
where: {
19-
user_id: userId,
20-
verification_status: verification_status.ACTIVE,
21-
},
22-
});
15+
async completedIdentityVerification(
16+
userId: string,
17+
tx?: Prisma.TransactionClient,
18+
): Promise<boolean> {
19+
const count = await (
20+
tx || this.prisma
21+
).user_identity_verification_associations.count({
22+
where: {
23+
user_id: userId,
24+
verification_status: verification_status.ACTIVE,
25+
},
26+
});
2327

2428
return count > 0;
2529
}

src/api/repository/paymentMethod.repo.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { Injectable } from '@nestjs/common';
2-
import { payment_method_status, user_payment_methods } from '@prisma/client';
2+
import {
3+
payment_method_status,
4+
Prisma,
5+
user_payment_methods,
6+
} from '@prisma/client';
37
import { PrismaService } from 'src/shared/global/prisma.service';
48

59
@Injectable()
@@ -14,14 +18,16 @@ export class PaymentMethodRepository {
1418
*/
1519
async getConnectedPaymentMethod(
1620
userId: string,
21+
tx?: Prisma.TransactionClient,
1722
): Promise<user_payment_methods | null> {
18-
const connectedUserPaymentMethod =
19-
await this.prisma.user_payment_methods.findFirst({
20-
where: {
21-
user_id: userId,
22-
status: payment_method_status.CONNECTED,
23-
},
24-
});
23+
const connectedUserPaymentMethod = await (
24+
tx || this.prisma
25+
).user_payment_methods.findFirst({
26+
where: {
27+
user_id: userId,
28+
status: payment_method_status.CONNECTED,
29+
},
30+
});
2531

2632
return connectedUserPaymentMethod;
2733
}

src/api/repository/taxForm.repo.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@nestjs/common';
2-
import { tax_form_status } from '@prisma/client';
2+
import { Prisma, tax_form_status } from '@prisma/client';
33
import { PrismaService } from 'src/shared/global/prisma.service';
44

55
@Injectable()
@@ -12,8 +12,11 @@ export class TaxFormRepository {
1212
* @param userId user id
1313
* @returns true if user has active tax form
1414
*/
15-
async hasActiveTaxForm(userId: string): Promise<boolean> {
16-
const count = await this.prisma.user_tax_form_associations.count({
15+
async hasActiveTaxForm(
16+
userId: string,
17+
tx?: Prisma.TransactionClient,
18+
): Promise<boolean> {
19+
const count = await (tx || this.prisma).user_tax_form_associations.count({
1720
where: {
1821
user_id: userId,
1922
tax_form_status: tax_form_status.ACTIVE,

src/api/winnings/winnings.service.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ export class WinningsService {
8888
}
8989
}
9090

91-
private async setPayrollPaymentMethod(userId: string) {
92-
const payrollPaymentMethod = await this.prisma.payment_method.findFirst({
91+
private async setPayrollPaymentMethod(
92+
userId: string,
93+
tx?: Prisma.TransactionClient,
94+
) {
95+
const payrollPaymentMethod = await (
96+
tx || this.prisma
97+
).payment_method.findFirst({
9398
where: {
9499
payment_method_type: 'Wipro Payroll',
95100
},
@@ -101,7 +106,7 @@ export class WinningsService {
101106
}
102107

103108
if (
104-
await this.prisma.user_payment_methods.findFirst({
109+
await (tx || this.prisma).user_payment_methods.findFirst({
105110
where: {
106111
user_id: userId,
107112
payment_method_id: payrollPaymentMethod.payment_method_id,
@@ -180,13 +185,18 @@ export class WinningsService {
180185

181186
const hasActiveTaxForm = await this.taxFormRepo.hasActiveTaxForm(
182187
body.winnerId,
188+
tx,
183189
);
184190
const hasConnectedPaymentMethod = Boolean(
185-
await this.paymentMethodRepo.getConnectedPaymentMethod(body.winnerId),
191+
await this.paymentMethodRepo.getConnectedPaymentMethod(
192+
body.winnerId,
193+
tx,
194+
),
186195
);
187196
const isIdentityVerified =
188197
await this.identityVerificationRepo.completedIdentityVerification(
189198
body.winnerId,
199+
tx,
190200
);
191201

192202
for (const detail of body.details || []) {
@@ -213,7 +223,7 @@ export class WinningsService {
213223
`Payroll payment detected. Setting payment status to PAID for user ${body.winnerId}`,
214224
);
215225
paymentModel.payment_status = PaymentStatus.PAID;
216-
await this.setPayrollPaymentMethod(body.winnerId);
226+
await this.setPayrollPaymentMethod(body.winnerId, tx);
217227
}
218228

219229
winningModel.payment.create.push(paymentModel);
@@ -223,7 +233,7 @@ export class WinningsService {
223233
}
224234

225235
this.logger.debug('Attempting to create winning with nested payments.');
226-
const createdWinning = await this.prisma.winnings.create({
236+
const createdWinning = await tx.winnings.create({
227237
data: winningModel as any,
228238
});
229239

0 commit comments

Comments
 (0)