Skip to content

Commit f27f1f7

Browse files
committed
PM-1099 - PR feedback: more error handling
1 parent 7849165 commit f27f1f7

File tree

7 files changed

+33
-28
lines changed

7 files changed

+33
-28
lines changed

src/api/repository/paymentMethod.repo.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export class PaymentMethodRepository {
77
constructor(private readonly prisma: PrismaService) {}
88

99
/**
10-
* Check user has verified payment method
10+
* Get the user's connected payment method (if there is one)
1111
*
1212
* @param userId user id
1313
* @param tx transaction
1414
*/
15-
async hasVerifiedPaymentMethod(
15+
async getConnectedPaymentMethod(
1616
userId: string,
1717
): Promise<user_payment_methods | null> {
1818
const connectedUserPaymentMethod =

src/api/wallet/wallet.service.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ export class WalletService {
3939
const winnings = await this.getWinningsTotalsByWinnerID(userId);
4040

4141
const hasActiveTaxForm = await this.taxFormRepo.hasActiveTaxForm(userId);
42-
const hasVerifiedPaymentMethod =
43-
await this.paymentMethodRepo.hasVerifiedPaymentMethod(userId);
42+
const hasVerifiedPaymentMethod = Boolean(
43+
await this.paymentMethodRepo.getConnectedPaymentMethod(userId),
44+
);
4445

4546
const winningTotals: WalletDetailDto = {
4647
account: {
@@ -62,10 +63,10 @@ export class WalletService {
6263
],
6364
},
6465
withdrawalMethod: {
65-
isSetupComplete: Boolean(hasVerifiedPaymentMethod),
66+
isSetupComplete: hasVerifiedPaymentMethod,
6667
},
6768
taxForm: {
68-
isSetupComplete: Boolean(hasActiveTaxForm),
69+
isSetupComplete: hasActiveTaxForm,
6970
},
7071
};
7172

src/api/webhooks/trolley/handlers/payment.handler.ts

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class PaymentHandler {
3535
this.logger.error(
3636
`No valid winning IDs found in the externalId: ${payload.externalId}`,
3737
);
38+
throw new Error('No valid winning IDs found in the externalId!');
3839
}
3940

4041
if (payload.status !== PaymentProcessedEventStatus.PROCESSED) {

src/api/winnings/winnings.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class WinningsService {
7474
body.winnerId,
7575
);
7676
const hasPaymentMethod =
77-
await this.paymentMethodRepo.hasVerifiedPaymentMethod(body.winnerId);
77+
await this.paymentMethodRepo.getConnectedPaymentMethod(body.winnerId);
7878

7979
for (const detail of body.details || []) {
8080
const paymentModel = {
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { ArrayNotEmpty, IsArray, IsNotEmpty, IsString } from 'class-validator';
2+
import { ArrayNotEmpty, IsArray, IsNotEmpty, IsUUID } from 'class-validator';
33

44
export class WithdrawRequestDto {
55
@ApiProperty({
@@ -8,7 +8,7 @@ export class WithdrawRequestDto {
88
})
99
@IsArray()
1010
@ArrayNotEmpty()
11-
@IsString({ each: true })
11+
@IsUUID('4',{ each: true })
1212
@IsNotEmpty({ each: true })
13-
paymentIds: string[];
13+
winningsIds: string[];
1414
}

src/api/withdrawal/withdrawal.controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class WithdrawalController {
5454
await this.withdrawalService.withdraw(
5555
user.id,
5656
user.handle,
57-
body.paymentIds,
57+
body.winningsIds,
5858
);
5959
result.status = ResponseStatusType.SUCCESS;
6060
return result;

src/api/withdrawal/withdrawal.service.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ export class WithdrawalService {
100100
);
101101
}
102102

103-
const hasVerifiedPaymentMethod =
104-
await this.paymentMethodRepo.hasVerifiedPaymentMethod(userId);
103+
const connectedPaymentMethod =
104+
await this.paymentMethodRepo.getConnectedPaymentMethod(userId);
105105

106-
if (!hasVerifiedPaymentMethod) {
106+
if (!connectedPaymentMethod) {
107107
throw new Error(
108108
'Please add a payment method before making a withdrawal.',
109109
);
@@ -154,7 +154,7 @@ export class WithdrawalService {
154154
user_id: userId,
155155
total_net_amount: totalAmount,
156156
status: payment_status.PROCESSING,
157-
payment_method_id: hasVerifiedPaymentMethod.payment_method_id,
157+
payment_method_id: connectedPaymentMethod.payment_method_id,
158158
payee_id: recipient.trolley_id,
159159
external_transaction_id: paymentBatch.id,
160160
payment_release_associations: {
@@ -166,26 +166,29 @@ export class WithdrawalService {
166166
},
167167
},
168168
});
169+
170+
try {
171+
// generate quote
172+
await this.trolleyService.client.batch.generateQuote(paymentBatch.id);
173+
174+
// trigger trolley payment (batch) process
175+
await this.trolleyService.client.batch.startProcessing(
176+
paymentBatch.id,
177+
);
178+
} catch (error) {
179+
this.logger.error(
180+
`Failed to process trolley payment batch: ${error.message}`,
181+
);
182+
throw new Error('Failed to process trolley payment batch!');
183+
}
184+
169185
this.logger.log(
170186
`Payment release created successfully. ID: ${paymentRelease.payment_release_id}`,
171187
);
172188
} catch (error) {
173189
this.logger.error(`Failed to create payment release: ${error.message}`);
174190
throw new Error('Failed to create db entry for payment release!');
175191
}
176-
177-
try {
178-
// generate quote
179-
await this.trolleyService.client.batch.generateQuote(paymentBatch.id);
180-
181-
// trigger trolley payment (batch) process
182-
await this.trolleyService.client.batch.startProcessing(paymentBatch.id);
183-
} catch (error) {
184-
this.logger.error(
185-
`Failed to process trolley payment batch: ${error.message}`,
186-
);
187-
throw new Error('Failed to process trolley payment batch!');
188-
}
189192
});
190193
}
191194
}

0 commit comments

Comments
 (0)