Skip to content

Commit b33c287

Browse files
authored
Merge pull request #52 from topcoder-platform/PM-1221_update-wallet-details
PM-1221 - update wallet details endpoint
2 parents aec5eec + c00e966 commit b33c287

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

src/api/wallet/wallet.controller.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import { Role } from 'src/core/auth/auth.constants';
1010
import { Roles, User } from 'src/core/auth/decorators';
1111
import { UserInfo } from 'src/dto/user.type';
1212
import { ResponseDto, ResponseStatusType } from 'src/dto/api-response.dto';
13-
import { WalletDetailDto } from 'src/dto/wallet.dto';
13+
import {
14+
WalletDetailDto,
15+
walletDetailResponseExample,
16+
} from 'src/dto/wallet.dto';
1417

1518
import { WalletService } from './wallet.service';
1619

@@ -30,6 +33,10 @@ export class WalletController {
3033
status: 200,
3134
description: 'Get wallet detail successfully.',
3235
type: ResponseDto<WalletDetailDto>,
36+
example: {
37+
data: walletDetailResponseExample,
38+
status: 'success',
39+
},
3340
})
3441
@HttpCode(HttpStatus.OK)
3542
async getWallet(

src/api/wallet/wallet.service.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ResponseDto } from 'src/dto/api-response.dto';
77
import { WinningsType } from 'src/dto/winning.dto';
88
import { TaxFormRepository } from '../repository/taxForm.repo';
99
import { PaymentMethodRepository } from '../repository/paymentMethod.repo';
10+
import { TrolleyService } from 'src/shared/global/trolley.service';
1011

1112
/**
1213
* The winning service.
@@ -23,8 +24,23 @@ export class WalletService {
2324
private readonly prisma: PrismaService,
2425
private readonly taxFormRepo: TaxFormRepository,
2526
private readonly paymentMethodRepo: PaymentMethodRepository,
27+
private readonly trolleyService: TrolleyService,
2628
) {}
2729

30+
async getPaymentTaxDetails(userId: string) {
31+
const recipient = await this.prisma.trolley_recipient.findFirst({
32+
where: { user_id: userId },
33+
});
34+
35+
if (!recipient) {
36+
return;
37+
}
38+
39+
return await this.trolleyService.getRecipientTaxDetails(
40+
recipient.trolley_id,
41+
);
42+
}
43+
2844
/**
2945
* Get wallet detail.
3046
* @param userId the request userId
@@ -43,7 +59,9 @@ export class WalletService {
4359
await this.paymentMethodRepo.getConnectedPaymentMethod(userId),
4460
);
4561

46-
const winningTotals: WalletDetailDto = {
62+
const taxWithholdingDetails = await this.getPaymentTaxDetails(userId);
63+
64+
result.data = {
4765
account: {
4866
balances: [
4967
{
@@ -68,9 +86,8 @@ export class WalletService {
6886
taxForm: {
6987
isSetupComplete: hasActiveTaxForm,
7088
},
89+
...(taxWithholdingDetails ?? {}),
7190
};
72-
73-
result.data = winningTotals;
7491
} catch (error) {
7592
this.logger.error('Getting winnings audit failed', error);
7693
const message = 'Searching winnings failed. ' + error;

src/dto/wallet.dto.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
export const walletDetailResponseExample = {
2+
account: {
3+
balances: [
4+
{
5+
type: 'PAYMENT',
6+
amount: 1000,
7+
unit: 'currency',
8+
},
9+
],
10+
},
11+
withdrawalMethod: {
12+
isSetupComplete: true,
13+
},
14+
taxForm: {
15+
isSetupComplete: true,
16+
},
17+
estimatedFees: '0',
18+
primaryCurrency: 'USD',
19+
taxWithholdingPercentage: '0',
20+
};
21+
122
export class WalletDetailDto {
223
account: {
324
balances: {
@@ -12,4 +33,7 @@ export class WalletDetailDto {
1233
taxForm: {
1334
isSetupComplete: boolean;
1435
};
36+
primaryCurrency?: string | null;
37+
estimatedFees?: string | null;
38+
taxWithholdingPercentage?: string | null;
1539
}

src/shared/global/trolley.service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import url from 'url';
22
import crypto from 'crypto';
33
import trolley from 'trolleyhq';
4+
import { pick } from 'lodash';
45
import { Injectable, Logger } from '@nestjs/common';
56
import { ENV_CONFIG } from 'src/config';
67

78
const TROLLEY_ACCESS_KEY = ENV_CONFIG.TROLLEY_ACCESS_KEY;
89
const TROLLEY_SECRET_KEY = ENV_CONFIG.TROLLEY_SECRET_KEY;
910
const TROLLEY_WIDGET_BASE_URL = ENV_CONFIG.TROLLEY_WIDGET_BASE_URL;
1011

12+
export interface RecipientTaxDetails {
13+
primaryCurrency: string | null;
14+
estimatedFees: string | null;
15+
taxWithholdingPercentage: string | null;
16+
}
17+
1118
const client = trolley({
1219
key: TROLLEY_ACCESS_KEY,
1320
secret: TROLLEY_SECRET_KEY,
@@ -167,4 +174,22 @@ export class TrolleyService {
167174
throw new Error('Failed to process trolley payment batch!');
168175
}
169176
}
177+
178+
async getRecipientTaxDetails(
179+
recipientId: string,
180+
): Promise<RecipientTaxDetails | void> {
181+
try {
182+
const recipient = await this.client.recipient.find(recipientId);
183+
return pick(recipient, [
184+
'estimatedFees',
185+
'primaryCurrency',
186+
'taxWithholdingPercentage',
187+
]) as RecipientTaxDetails;
188+
} catch (error) {
189+
this.logger.error(
190+
'Failed to load recipient tax details from trolley!',
191+
error,
192+
);
193+
}
194+
}
170195
}

0 commit comments

Comments
 (0)