Skip to content

Commit a4617c8

Browse files
authored
Merge pull request #140 from topcoder-platform/PM-3575_handle-ba-admin-user
PM-3575 - use m2m to BA api to fetch user's billing accounts
2 parents cfbfac7 + dea070b commit a4617c8

2 files changed

Lines changed: 26 additions & 30 deletions

File tree

src/api/admin/admin.service.ts

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,9 @@ import { ResponseDto } from 'src/dto/api-response.dto';
1313
import { PaymentStatus } from 'src/dto/payment.dto';
1414
import { WinningAuditDto, AuditPayoutDto } from './dto/audit.dto';
1515
import { WinningUpdateRequestDto } from './dto/winnings.dto';
16-
import { TopcoderChallengesService } from 'src/shared/topcoder/challenges.service';
17-
import { Logger, getBaClient } from 'src/shared/global';
16+
import { Logger } from 'src/shared/global';
1817
import { WinningRequestDto } from 'src/dto/winning.dto';
19-
20-
function formatDate(date = new Date()) {
21-
const pad = (n, z = 2) => String(n).padStart(z, '0');
22-
23-
return (
24-
`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
25-
`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.` +
26-
`${pad(date.getMilliseconds(), 3)}`
27-
);
28-
}
18+
import { BillingAccountsService } from 'src/shared/topcoder/billing-accounts.service';
2919

3020
/**
3121
* The admin winning service.
@@ -41,20 +31,9 @@ export class AdminService {
4131
constructor(
4232
private readonly prisma: PrismaService,
4333
private readonly paymentsService: PaymentsService,
44-
private readonly tcChallengesService: TopcoderChallengesService,
34+
private readonly baService: BillingAccountsService,
4535
) {}
4636

47-
async getBillingAccountsForUser(userId: string): Promise<string[]> {
48-
const baPrisma = getBaClient();
49-
const baRows = await baPrisma.billingAccountAccess.findMany({
50-
where: {
51-
userId,
52-
}
53-
});
54-
55-
return baRows.map(r => `${r.billingAccountId}`);
56-
}
57-
5837
async applyBaAdminUserFilters(
5938
userId: string,
6039
isBaAdmin?: boolean,
@@ -66,7 +45,7 @@ export class AdminService {
6645

6746
return {
6847
...filters,
69-
billingAccounts: await this.getBillingAccountsForUser(userId),
48+
billingAccounts: await this.baService.getBillingAccountsForUser(userId),
7049
};
7150
}
7251

@@ -109,15 +88,15 @@ export class AdminService {
10988
},
11089
select: {
11190
billing_account: true,
112-
}
113-
});;
91+
},
92+
});
11493

11594
if (!payments || payments.length === 0) {
11695
// nothing to check
11796
return;
11897
}
11998

120-
const allowedBAs = await this.getBillingAccountsForUser(userId);
99+
const allowedBAs = await this.baService.getBillingAccountsForUser(userId);
121100
const paymentBAs = payments
122101
.map((p) => p.billing_account)
123102
.filter((b) => b !== null && b !== undefined);
@@ -173,8 +152,6 @@ export class AdminService {
173152
throw new NotFoundException('failed to get current payments');
174153
}
175154

176-
177-
178155
let releaseDate;
179156
if (body.paymentStatus) {
180157
releaseDate = await this.getPaymentReleaseDateByWinningsId(winningsId);

src/shared/topcoder/billing-accounts.service.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,23 @@ export class BillingAccountsService {
159159
}
160160
}
161161
}
162+
163+
async getBillingAccountsForUser(userId: string): Promise<string[]> {
164+
this.logger.log(`Fetching billing accounts for user '${userId}'`);
165+
166+
try {
167+
return await this.m2MService
168+
.m2mFetch<
169+
{ tcBillingAccountId: string }[]
170+
>(`${TOPCODER_API_V6_BASE_URL}/billing-accounts/users/${userId}`)
171+
.then((r) => r.map((b) => `${b.tcBillingAccountId}`));
172+
} catch (err: any) {
173+
this.logger.error(
174+
err.response?.data?.result?.content ??
175+
`Failed to fetch billing accounts for user '${userId}'!`,
176+
err,
177+
);
178+
throw new Error('Failed to fetch billing acccounts!');
179+
}
180+
}
162181
}

0 commit comments

Comments
 (0)