From 5be08445755c46314d3d7adeeba3a78a34354f33 Mon Sep 17 00:00:00 2001 From: Alp Date: Sat, 27 Jan 2024 00:58:44 +0300 Subject: [PATCH] Finalize claim reward endpoint --- src/controllers/leaderboard.controller.ts | 2 +- src/repositories/user.repository.ts | 15 +++++++++++++ src/services/leaderboard.service.ts | 10 +++++++-- src/services/tournament.service.ts | 26 +++++++++++++++++++++-- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/controllers/leaderboard.controller.ts b/src/controllers/leaderboard.controller.ts index 37ae91e..355f518 100644 --- a/src/controllers/leaderboard.controller.ts +++ b/src/controllers/leaderboard.controller.ts @@ -28,6 +28,6 @@ export class LeaderboardController { @Req() req: IAuthorizedRequest, ): Promise { const user = req.user; - return await this.leaderboardService.getMyRank(user.username); + return await this.leaderboardService.getRankOfUserByUsername(user.username); } } diff --git a/src/repositories/user.repository.ts b/src/repositories/user.repository.ts index 608ea95..090b49b 100644 --- a/src/repositories/user.repository.ts +++ b/src/repositories/user.repository.ts @@ -206,6 +206,21 @@ export class UserRepository { } } + public async claimReward(username: string, coinCount: number): Promise { + 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; } diff --git a/src/services/leaderboard.service.ts b/src/services/leaderboard.service.ts index 81a3f4f..be063e3 100644 --- a/src/services/leaderboard.service.ts +++ b/src/services/leaderboard.service.ts @@ -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 { @@ -9,7 +10,7 @@ export class LeaderboardService { public readonly userRepository: UserRepository, ) {} - public async getMyRank(username: string): Promise { + public async getRankOfUserByUsername(username: string): Promise { const user = await this.userRepository.findUserByUsername(username); if (user.currGroupId === '') { @@ -17,13 +18,18 @@ export class LeaderboardService { 'User did not participate in any tournament yet', ); } + + return await this.getRankOfUserByUserObject(user); + } + + public async getRankOfUserByUserObject(user: IUser): Promise { 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 diff --git a/src/services/tournament.service.ts b/src/services/tournament.service.ts index e76adff..69cc76b 100644 --- a/src/services/tournament.service.ts +++ b/src/services/tournament.service.ts @@ -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, ) {} @@ -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); + } } }