Skip to content

Commit

Permalink
Finalize claim reward endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Alputer committed Jan 26, 2024
1 parent efb6846 commit dc9ee76
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/controllers/leaderboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { LeaderboardService } from '../services';
export class LeaderboardController {
constructor(private readonly leaderboardService: LeaderboardService) {}



@UseGuards(AuthGuard)
@ApiResponse({
status: 200,
Expand All @@ -24,10 +26,10 @@ export class LeaderboardController {
description: 'Internal server error, contact with backend team.',
})
@Get('/tournament/my-rank')
public async enterTournament(
public async getMyTournamentRank(
@Req() req: IAuthorizedRequest,
): Promise<number> {
const user = req.user;
return await this.leaderboardService.getMyRank(user.username);
return await this.leaderboardService.getRankOfUserByUsername(user.username);
}
}
15 changes: 15 additions & 0 deletions src/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ export class UserRepository {
}
}

public async claimReward(username: string, coinCount: number): Promise<void> {
const updateCommand = new UpdateItemCommand({
TableName: this.tableName,
Key: {
username: { S: username },
},
UpdateExpression:
'SET coins = coins + :coinsVal, claimedReward = :claimedRewardVal',
ExpressionAttributeValues: {
':coinsVal': { N: String(coinCount) },
':claimedRewardVal': { BOOL: true },
},
});
}

public getTableName(): string {
return this.tableName;
}
Expand Down
10 changes: 8 additions & 2 deletions src/services/leaderboard.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { TournamentGroupRepository, UserRepository } from '../repositories';
import { SortOption } from '../enums';
import { IUser } from '../interfaces';

@Injectable()
export class LeaderboardService {
Expand All @@ -9,21 +10,26 @@ export class LeaderboardService {
public readonly userRepository: UserRepository,
) {}

public async getMyRank(username: string): Promise<number> {
public async getRankOfUserByUsername(username: string): Promise<number> {
const user = await this.userRepository.findUserByUsername(username);

if (user.currGroupId === '') {
throw new BadRequestException(
'User did not participate in any tournament yet',
);
}

return await this.getRankOfUserByUserObject(user);
}

public async getRankOfUserByUserObject(user: IUser): Promise<number> {
const members =
await this.tournamentGroupRepository.getGroupMembersByGroupId(
user.currGroupId,
SortOption.NO_SORT,
);
const userScore = members.findIndex(
(item) => item.username === username,
(item) => item.username === user.username,
).tournamentScore;

let rank = 1; // calculate the rank of user in the group
Expand Down
26 changes: 24 additions & 2 deletions src/services/tournament.service.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import {
BadRequestException,
Inject,
Injectable,
InternalServerErrorException,
forwardRef,
} from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { Tournament, TournamentGroup } from '../entities';
import {
TournamentRepository,
TournamentGroupRepository,
UserRepository,
} from '../repositories';
import { UserService } from '.';
import { LeaderboardService, UserService } from '.';

@Injectable()
export class TournamentService {
constructor(
private readonly userService: UserService,
@Inject(forwardRef(() => LeaderboardService))
private readonly leaderboardService: LeaderboardService,
private readonly userRepository: UserRepository,
private readonly tournamentRepository: TournamentRepository,
private readonly tournamentGroupRepository: TournamentGroupRepository,
) {}
Expand Down Expand Up @@ -115,6 +121,22 @@ export class TournamentService {
throw new BadRequestException('You already claimed your reward');
}

//find rank and update
const rank = await this.leaderboardService.getRankOfUserByUserObject(user);

if (rank === 1) {
await this.userRepository.claimReward(user.username, 5000);
}
if (rank === 2) {
await this.userRepository.claimReward(user.username, 3000);
}
if (rank === 3) {
await this.userRepository.claimReward(user.username, 2000);
}
if (rank >= 4 && rank <= 10) {
await this.userRepository.claimReward(user.username, 1000);
}
if (rank > 10) {
await this.userRepository.claimReward(user.username, 0);
}
}
}

0 comments on commit dc9ee76

Please sign in to comment.