Skip to content

Commit

Permalink
Merge pull request #365 from modern-agile-team/feat/#349/Building_a_R…
Browse files Browse the repository at this point in the history
…ank_System

Feat/#349/building a rank system
  • Loading branch information
2swo authored Mar 17, 2024
2 parents e8cc9b8 + 370bfd7 commit 4b226b9
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/total-count/repositories/total-count.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ export class TotalCountRepository {
},
);
}

getMentorBoardAndReviewAndBadgeCount(userId: number) {
return this.entityManager.findOne(TotalCount, {
where: { userId },
select: ['mentorBoardCount', 'reviewCount', 'badgeCount'],
});
}
}
6 changes: 6 additions & 0 deletions src/total-count/services/total-count.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ export class TotalCountService {

console.log('7일 카운트 초기화 성공');
}

getMentorBoardAndReviewAndBadgeCount(userId: number) {
return this.totalCountRepository.getMentorBoardAndReviewAndBadgeCount(
userId,
);
}
}
7 changes: 7 additions & 0 deletions src/users/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ApiUpdateUserIntro } from '@src/users/swagger-decorators/patch-user-int
import { ApiPostUserIntro } from '@src/users/swagger-decorators/upload-user-Intro-decorators';
import { UserIntro } from '@src/entities/UserIntro';
import { ParsePositiveIntPipe } from '@src/common/pipes/parse-positive-int.pipe';
import { ApiPostUserRank } from '../swagger-decorators/get-user-rank-decorator';

@Controller('user')
@UseInterceptors(ClassSerializerInterceptor)
Expand Down Expand Up @@ -114,4 +115,10 @@ export class UserController {
getUserBadge(@Param('userId') userId: number) {
return this.userBadgeService.checkUserBadges(userId);
}

@ApiPostUserRank()
@Post(':userId/rank')
getUserRank(@Param('userId') userId: number) {
return this.userRankingService.checkUserRank(userId);
}
}
7 changes: 7 additions & 0 deletions src/users/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,11 @@ export class UserRepository {
.withDeleted()
.getOne();
}

async updateMyRank(userId: number, rank: number): Promise<number> {
const user = await this.getUser(userId);
user.rank = rank;
await this.entityManager.save(User, user);
return rank;
}
}
40 changes: 39 additions & 1 deletion src/users/services/user-ranking.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Cron } from '@nestjs/schedule';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { UserRankingRepository } from '@src/users/repositories/user-ranking.repository';
import { MentorReviewChecklistCountsService } from '@src/total-count/services/mentor-review-checklist-counts.service';
import { TotalCountService } from '@src/total-count/services/total-count.service';
import { UserRepository } from '../repositories/user.repository';

@Injectable()
export class UserRankingService {
constructor(private readonly userRankingRepository: UserRankingRepository) {}
constructor(
private readonly userRankingRepository: UserRankingRepository,
private readonly mentorReviewCountService: MentorReviewChecklistCountsService,
private readonly totalCountService: TotalCountService,
private readonly userRepository: UserRepository,
) {}

async getUserRanking() {
try {
Expand Down Expand Up @@ -108,4 +116,34 @@ export class UserRankingService {
);
}
}

async checkUserRank(userId: number): Promise<any> {
// 내 랭크 확인
const myRank = await this.userRepository.getUserRank(userId);

// 유저의 리뷰 카운트 불러오기
const mentorReviewCount =
await this.mentorReviewCountService.findOneMentorReviewChecklistCountOrFail(
userId,
);

const baseRank = 15; //기본 점수

//유저 리뷰 카운트에 따라 랭크 +- 점수부여
const rank =
mentorReviewCount.isGoodWorkCount * 8 +
mentorReviewCount.isClearCount * 8 +
mentorReviewCount.isQuickCount * 8 +
mentorReviewCount.isAccurateCount * 8 +
mentorReviewCount.isKindnessCount * 8 +
mentorReviewCount.isInformativeCount * 8 +
mentorReviewCount.isUnderstandWellCount * 8 -
mentorReviewCount.isBadCount * 5 -
mentorReviewCount.isStuffyCount * 5 +
baseRank;

const newRank = await this.userRepository.updateMyRank(userId, rank);

return [{ myRank: myRank }, { newRank: newRank }];
}
}
67 changes: 67 additions & 0 deletions src/users/swagger-decorators/get-user-rank-decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { applyDecorators } from '@nestjs/common';
import { ApiParam, ApiOperation, ApiResponse } from '@nestjs/swagger';

export function ApiPostUserRank() {
return applyDecorators(
ApiOperation({
summary: '유저의 기존 랭크를 검사하고, 새로운 랭크 정보를 가져오는 API',
description: '기존 랭크를 검사하고, 새로운 랭크 정보를 가져오는 API',
}),

ApiResponse({
status: 201,
description:
'유저의 기존 랭크를 검사하고, 유저의 랭크 정보를 성공적으로 최신화했을 경우',
content: {
JSON: {
examples: {
test1: {
value: {
data: [
{
myRank: 155,
},
{
newRank: 155,
},
],
statusCode: 201,
message: '기존의 랭크와 새로운 랭크의 변동이 없는 경우',
},
description: '랭크에 변동이 없는 경우',
},
test2: {
value: {
data: [
{
myRank: 155,
},
{
newRank: 351,
},
],
statusCode: 201,
message: '기존의 랭크와 새로운 랭크의 변동이 있는 경우',
},
description: '랭크의 변동이 있는 경우',
},
},
},
},
}),

ApiResponse({
status: 500,
description: '서버 에러',
content: {
JSON: {
example: {
statusCode: 500,
message: 'DB혹은 서버쪽 에러입니다.',
},
},
},
}),
ApiParam({ name: 'userId', example: 4 }),
);
}

0 comments on commit 4b226b9

Please sign in to comment.