Skip to content

Commit 9dfe7b3

Browse files
committed
PM-2087 - use Topcoder v6 APIs
1 parent 53b095d commit 9dfe7b3

File tree

6 files changed

+10
-139
lines changed

6 files changed

+10
-139
lines changed

src/api/admin/admin.service.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ 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 {
17-
AdminPaymentUpdateData,
18-
TopcoderChallengesService,
19-
} from 'src/shared/topcoder/challenges.service';
16+
import { TopcoderChallengesService } from 'src/shared/topcoder/challenges.service';
2017
import { Logger } from 'src/shared/global';
2118

2219
/**
@@ -307,30 +304,6 @@ export class AdminService {
307304
}
308305
});
309306

310-
transactions.push(async () => {
311-
const winning = await this.getWinningById(winningsId);
312-
if (!winning) {
313-
this.logger.error(
314-
`Error updating legacy system for winning ${winningsId}. Winning not found!`,
315-
);
316-
throw new Error(
317-
`Error updating legacy system for winning ${winningsId}. Winning not found!`,
318-
);
319-
}
320-
321-
const payoutData: AdminPaymentUpdateData = {
322-
userId: +winning.winner_id,
323-
status: body.paymentStatus,
324-
amount: body.paymentAmount,
325-
releaseDate: body.releaseDate,
326-
};
327-
328-
await this.tcChallengesService.updateLegacyPayments(
329-
winning.external_id as string,
330-
payoutData,
331-
);
332-
});
333-
334307
// Run all transaction tasks in a single prisma transaction
335308
await this.prisma.$transaction(async (tx) => {
336309
for (const transaction of transactions) {

src/api/withdrawal/withdrawal.service.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import {
1212
} from '@prisma/client';
1313
import { TrolleyService } from 'src/shared/global/trolley.service';
1414
import { PaymentsService } from 'src/shared/payments';
15-
import {
16-
TopcoderChallengesService,
17-
WithdrawUpdateData,
18-
} from 'src/shared/topcoder/challenges.service';
15+
import { TopcoderChallengesService } from 'src/shared/topcoder/challenges.service';
1916
import { TopcoderMembersService } from 'src/shared/topcoder/members.service';
2017
import { BasicMemberInfo, BASIC_MEMBER_FIELDS } from 'src/shared/topcoder';
2118
import { Logger } from 'src/shared/global';
@@ -35,16 +32,6 @@ interface ReleasableWinningRow {
3532
datePaid: Date;
3633
}
3734

38-
function formatDate(date = new Date()) {
39-
const pad = (n, z = 2) => String(n).padStart(z, '0');
40-
41-
return (
42-
`${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` +
43-
`${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}.` +
44-
`${pad(date.getMilliseconds(), 3)}`
45-
);
46-
}
47-
4835
@Injectable()
4936
export class WithdrawalService {
5037
private readonly logger = new Logger(WithdrawalService.name);
@@ -367,26 +354,6 @@ export class WithdrawalService {
367354
this.logger.error(errorMsg, error);
368355
throw new Error(errorMsg);
369356
}
370-
371-
try {
372-
for (const winning of winnings) {
373-
const payoutData: WithdrawUpdateData = {
374-
userId: +userId,
375-
status: 'Paid',
376-
datePaid: formatDate(new Date()),
377-
};
378-
379-
await this.tcChallengesService.updateLegacyPayments(
380-
winning.externalId as string,
381-
payoutData,
382-
);
383-
}
384-
} catch (error) {
385-
this.logger.error(
386-
`Failed to update legacy payment while withdrawing for challenge ${error?.message ?? error}`,
387-
error,
388-
);
389-
}
390357
});
391358
} catch (error) {
392359
if (error.code === 'P2010' && error.meta?.code === '55P03') {

src/config/config.env.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export class ConfigEnv {
2222
@IsString()
2323
TOPCODER_API_BASE_URL!: string;
2424

25+
@IsString()
26+
TOPCODER_API_V5_BASE_URL!: string;
27+
2528
@IsString()
2629
AUTH0_M2M_AUDIENCE!: string;
2730

src/shared/topcoder/bus.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ENV_CONFIG } from 'src/config';
33
import { TopcoderM2MService } from './topcoder-m2m.service';
44
import { Logger } from '../global';
55

6-
const { TOPCODER_API_BASE_URL } = ENV_CONFIG;
6+
const { TOPCODER_API_V5_BASE_URL: TC_API_BASE } = ENV_CONFIG;
77

88
@Injectable()
99
export class TopcoderBusService {
@@ -43,7 +43,7 @@ export class TopcoderBusService {
4343

4444
try {
4545
const headers = await this.getHeaders();
46-
const response = await fetch(`${TOPCODER_API_BASE_URL}/bus/events`, {
46+
const response = await fetch(`${TC_API_BASE}/bus/events`, {
4747
method: 'POST',
4848
headers,
4949
body: JSON.stringify({
Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { Injectable } from '@nestjs/common';
22
import { TopcoderM2MService } from './topcoder-m2m.service';
3-
import { ENV_CONFIG } from 'src/config';
4-
import { payment_status } from '@prisma/client';
53
import { Logger } from 'src/shared/global';
64

7-
const { TOPCODER_API_BASE_URL } = ENV_CONFIG;
8-
95
export interface WithdrawUpdateData {
106
userId: number;
117
status: string;
@@ -18,77 +14,9 @@ export interface AdminPaymentUpdateData {
1814
amount: number;
1915
releaseDate: string;
2016
}
21-
22-
const mapStatus = (payoutData: WithdrawUpdateData | AdminPaymentUpdateData) => {
23-
return {
24-
...payoutData,
25-
status: {
26-
[payment_status.CANCELLED]: 'Cancelled',
27-
[payment_status.FAILED]: 'Failed',
28-
[payment_status.ON_HOLD]: 'OnHold',
29-
[payment_status.ON_HOLD_ADMIN]: 'OnHoldAdmin',
30-
[payment_status.OWED]: 'Owed',
31-
[payment_status.PAID]: 'Paid',
32-
[payment_status.PROCESSING]: 'Processing',
33-
[payment_status.RETURNED]: 'Returned',
34-
}[payoutData.status],
35-
};
36-
};
37-
3817
@Injectable()
3918
export class TopcoderChallengesService {
4019
private readonly logger = new Logger(TopcoderChallengesService.name);
4120

4221
constructor(private readonly m2MService: TopcoderM2MService) {}
43-
44-
async updateLegacyPayments(
45-
challengeId: string,
46-
payoutData: WithdrawUpdateData | AdminPaymentUpdateData,
47-
) {
48-
const requestData = mapStatus(payoutData);
49-
50-
let m2mToken: string | undefined;
51-
try {
52-
m2mToken = await this.m2MService.getToken();
53-
} catch (e) {
54-
this.logger.error(
55-
'Failed to fetch m2m token for fetching member details!',
56-
e.message ?? e,
57-
);
58-
}
59-
const requestUrl = `${TOPCODER_API_BASE_URL}/challenges/${challengeId}/legacy-payment`;
60-
61-
this.logger.debug(
62-
`Updating legacy payment for challenge ${challengeId} with data: ${JSON.stringify(requestData, null, 2)}`,
63-
);
64-
65-
try {
66-
const response = await fetch(requestUrl, {
67-
method: 'PATCH',
68-
body: JSON.stringify(requestData),
69-
headers: {
70-
Authorization: `Bearer ${m2mToken}`,
71-
'Content-Type': 'application/json',
72-
},
73-
});
74-
75-
const jsonResponse: { [key: string]: string } = await response.json();
76-
77-
if (response.status > 299) {
78-
throw new Error(jsonResponse.message ?? JSON.stringify(jsonResponse));
79-
}
80-
81-
this.logger.debug(
82-
`Response from updating legacy payment for challenge ${challengeId}: ${JSON.stringify(jsonResponse, null, 2)}`,
83-
);
84-
85-
return jsonResponse;
86-
} catch (e) {
87-
this.logger.error(
88-
`Failed to update legacy payment for challenge ${challengeId}! Error: ${e?.message ?? e}`,
89-
e,
90-
);
91-
throw e;
92-
}
93-
}
9422
}

src/shared/topcoder/members.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TopcoderM2MService } from './topcoder-m2m.service';
55
import { ENV_CONFIG } from 'src/config';
66
import { Logger } from 'src/shared/global';
77

8-
const { TOPCODER_API_BASE_URL } = ENV_CONFIG;
8+
const { TOPCODER_API_BASE_URL: TC_API_BASE } = ENV_CONFIG;
99

1010
@Injectable()
1111
export class TopcoderMembersService {
@@ -27,7 +27,7 @@ export class TopcoderMembersService {
2727

2828
// Split the unique user IDs into chunks of 100 to comply with API request limits
2929
const requests = chunk(uniqUserIds, 30).map((chunk) => {
30-
const requestUrl = `${TOPCODER_API_BASE_URL}/members?${chunk.map((id) => `userIds[]=${id}`).join('&')}&fields=handle,userId`;
30+
const requestUrl = `${TC_API_BASE}/members?${chunk.map((id) => `userIds[]=${id}`).join('&')}&fields=handle,userId`;
3131
return fetch(requestUrl).then(
3232
async (response) =>
3333
(await response.json()) as { handle: string; userId: string },
@@ -77,7 +77,7 @@ export class TopcoderMembersService {
7777
e.message ?? e,
7878
);
7979
}
80-
const requestUrl = `${TOPCODER_API_BASE_URL}/members/${handle}${fields ? `?fields=${fields.join(',')}` : ''}`;
80+
const requestUrl = `${TC_API_BASE}/members/${handle}${fields ? `?fields=${fields.join(',')}` : ''}`;
8181

8282
try {
8383
const response = await fetch(requestUrl, {

0 commit comments

Comments
 (0)