Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/api/challenges/challenges.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { includes, isEmpty, find, camelCase, groupBy, orderBy } from 'lodash';
import { Injectable } from '@nestjs/common';
import { ENV_CONFIG } from 'src/config';
import { Logger } from 'src/shared/global';
import { Challenge, ChallengeResource, ResourceRole } from './models';
import {
Challenge,
ChallengeResource,
ChallengeSubmission,
ResourceRole,
} from './models';
import { BillingAccountsService } from 'src/shared/topcoder/billing-accounts.service';
import { TopcoderM2MService } from 'src/shared/topcoder/topcoder-m2m.service';
import { ChallengeStatuses } from 'src/dto/challenge.dto';
Expand Down Expand Up @@ -36,7 +41,6 @@ export class ChallengesService {

try {
const challenge = await this.m2MService.m2mFetch<Challenge>(requestUrl);
this.logger.log(JSON.stringify(challenge, null, 2));
return challenge;
} catch (e) {
this.logger.error(
Expand All @@ -46,6 +50,24 @@ export class ChallengesService {
}
}

async getChallengeSubmissionsCount(challengeId: string) {
const requestUrl = `${TOPCODER_API_V6_BASE_URL}/submissions?challengeId=${challengeId}&perPage=9999`;

try {
const submissions =
await this.m2MService.m2mFetch<ChallengeSubmission[]>(requestUrl);
const uniqueSubmissions = Object.fromEntries(
submissions.map((s) => [s.memberId, s]),
);
return Object.keys(uniqueSubmissions).length;
} catch (e) {
this.logger.error(
`Challenge submissions couldn't be fetched for challenge ${challengeId}!`,
e,
);
}
}

async getChallengeResources(challengeId: string) {
try {
const resources = await this.m2MService.m2mFetch<ChallengeResource[]>(
Expand Down Expand Up @@ -149,6 +171,8 @@ export class ChallengesService {
// generate reviewer payments
const firstPlacePrize = placementPrizes?.[0]?.value ?? 0;
const challengeReviewer = find(reviewers, { isMemberReview: true });
const numOfSubmissions =
(await this.getChallengeSubmissionsCount(challenge.id)) ?? 1;

if (challengeReviewer && challengeResources.reviewer) {
challengeResources.reviewer?.forEach((reviewer) => {
Expand All @@ -160,7 +184,7 @@ export class ChallengesService {
(challengeReviewer.baseCoefficient ?? 0) * firstPlacePrize +
(challengeReviewer.incrementalCoefficient ?? 0) *
firstPlacePrize *
challenge.numOfSubmissions,
numOfSubmissions,
),
type: WinningsCategory.REVIEW_BOARD_PAYMENT,
});
Expand Down Expand Up @@ -246,9 +270,16 @@ export class ChallengesService {
}

await Promise.all(
payments.map((p) =>
this.winningsService.createWinningWithPayments(p, userId),
),
payments.map(async (p) => {
try {
await this.winningsService.createWinningWithPayments(p, userId);
} catch (e) {
this.logger.log(
`Failed to create winnings payment for user ${p.winnerId}!`,
e,
);
}
}),
);

this.logger.log('Task Completed. locking consumed budget', baValidation);
Expand Down
6 changes: 6 additions & 0 deletions src/api/challenges/models/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,9 @@ export interface ResourceRole {
id: string;
name: string;
}

export interface ChallengeSubmission {
id: string;
memberId: string;
type: 'CONTEST_SUBMISSION';
}
6 changes: 3 additions & 3 deletions src/api/winnings/winnings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class WinningsService {
}

this.logger.debug(
`Member info retrieved successfully for user handle: ${userId}`,
`Member info retrieved successfully for user: ${userId}`,
{ member },
);

Expand Down Expand Up @@ -142,7 +142,7 @@ export class WinningsService {
const result = new ResponseDto<string>();

this.logger.debug(
`Creating winning with payments for user ${userId}`,
`Creating winning with payments for user ${body.winnerId}`,
body,
);

Expand Down Expand Up @@ -186,7 +186,7 @@ export class WinningsService {
);
const isIdentityVerified =
await this.identityVerificationRepo.completedIdentityVerification(
userId,
body.winnerId,
);

for (const detail of body.details || []) {
Expand Down