Skip to content

Commit

Permalink
feat: ✨ leaderboard 에 coalitionId filter 기능 추가
Browse files Browse the repository at this point in the history
- close #378
  • Loading branch information
jpham005 committed Oct 29, 2023
1 parent eee9b1d commit 0a38202
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 242 deletions.
16 changes: 6 additions & 10 deletions app/src/api/cursusUser/cursusUser.cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable } from '@nestjs/common';
import {
CacheUtilRankingService,
type GetRankArgs,
type GetRankingArgs,
type RankCache,
} from 'src/cache/cache.util.ranking.service';
import {
Expand Down Expand Up @@ -41,26 +43,20 @@ export class CursusUserCacheService {
}

async getUserRank(
keyBase: UserRankingKey,
userId: number,
promo?: number,
cacheArgs: Omit<GetRankArgs, 'dateTemplate'>,
): Promise<RankCache | undefined> {
return await this.cacheUtilRankingService.getRawRank({
keyBase,
userId,
dateTemplate: DateTemplate.TOTAL,
promo,
...cacheArgs,
});
}

async getUserRanking(
keyBase: UserRankingKey,
promo?: number,
cacheArgs: Omit<GetRankingArgs, 'dateTemplate'>,
): Promise<RankCache[] | undefined> {
return await this.cacheUtilRankingService.getRawRanking({
keyBase,
dateTemplate: DateTemplate.TOTAL,
promo,
...cacheArgs,
});
}
}
16 changes: 6 additions & 10 deletions app/src/api/experienceUser/experienceUser.cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable } from '@nestjs/common';
import {
CacheUtilRankingService,
type GetRankArgs,
type GetRankingArgs,
type RankCache,
type RankingSupportedDateTemplate,
} from 'src/cache/cache.util.ranking.service';
Expand All @@ -20,26 +22,20 @@ export class ExperienceUserCacheService {
) {}

async getExpIncreamentRank(
dateTemplate: ExpIncreamentRankingCacheSupportedDateTemplate,
userId: number,
promo?: number,
cacheArgs: Omit<GetRankArgs, 'keyBase'>,
): Promise<RankCache | undefined> {
return await this.cacheUtilRankingService.getRank({
keyBase: EXP_INCREAMENT_RANKING,
dateTemplate,
userId,
promo,
...cacheArgs,
});
}

async getExpIncreamentRanking(
dateTemplate: ExpIncreamentRankingCacheSupportedDateTemplate,
promo?: number,
cacheArgs: Omit<GetRankingArgs, 'keyBase'>,
): Promise<RankCache[] | undefined> {
return await this.cacheUtilRankingService.getRanking({
keyBase: EXP_INCREAMENT_RANKING,
dateTemplate,
promo,
...cacheArgs,
});
}
}
32 changes: 10 additions & 22 deletions app/src/api/scaleTeam/scaleTeam.cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,29 @@ export class ScaleTeamCacheService {
) {}

async getEvalCountRank(
dateTemplate: EvalCountRankingSupportedDateTemplate,
userId: number,
promo?: number,
cacheArgs: Omit<GetRankArgs, 'keyBase'>,
): Promise<RankCache | undefined> {
const args: GetRankArgs = {
keyBase: EVAL_COUNT_RANKING,
userId,
dateTemplate,
promo,
...cacheArgs,
};

if (dateTemplate === DateTemplate.TOTAL) {
if (cacheArgs.dateTemplate === DateTemplate.TOTAL) {
return await this.cacheUtilRankingService.getRawRank(args);
}

return await this.cacheUtilRankingService.getRank(args);
}

async getEvalCountRanking(
dateTemplate: EvalCountRankingSupportedDateTemplate,
promo?: number,
cacheArgs: Omit<GetRankingArgs, 'keyBase'>,
): Promise<RankCache[] | undefined> {
const args: GetRankingArgs = {
keyBase: EVAL_COUNT_RANKING,
dateTemplate,
promo,
...cacheArgs,
};

if (dateTemplate === DateTemplate.TOTAL) {
if (cacheArgs.dateTemplate === DateTemplate.TOTAL) {
return await this.cacheUtilRankingService.getRawRanking(args);
}

Expand All @@ -87,26 +81,20 @@ export class ScaleTeamCacheService {
}

async getCommentRank(
dateTemplate: AverageReviewLengthRankingSupportedDateTemplate,
userId: number,
promo?: number,
cacheArgs: Omit<GetRankArgs, 'keyBase'>,
): Promise<RankCache | undefined> {
return await this.cacheUtilRankingService.getRank({
keyBase: COMMENT_RANKING,
userId,
dateTemplate,
promo,
...cacheArgs,
});
}

async getCommentRanking(
dateTemplate: AverageReviewLengthRankingSupportedDateTemplate,
promo?: number,
cacheArgs: Omit<GetRankingArgs, 'keyBase'>,
): Promise<RankCache[] | undefined> {
return await this.cacheUtilRankingService.getRanking({
keyBase: COMMENT_RANKING,
dateTemplate,
promo,
...cacheArgs,
});
}
}
16 changes: 6 additions & 10 deletions app/src/api/score/score.cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Injectable } from '@nestjs/common';
import {
CacheUtilRankingService,
type GetRankArgs,
type GetRankingArgs,
type RankCache,
type RankingSupportedDateTemplate,
} from 'src/cache/cache.util.ranking.service';
Expand All @@ -27,26 +29,20 @@ export class ScoreCacheService {
) {}

async getScoreRank(
dateTemplate: ScoreRankingSupportedDateTemplate,
userId: number,
promo?: number,
cacheArgs: Omit<GetRankArgs, 'keyBase'>,
): Promise<RankCache | undefined> {
return await this.cacheUtilRankingService.getRank({
keyBase: SCORE_RANKING,
userId,
dateTemplate,
promo,
...cacheArgs,
});
}

async getScoreRanking(
dateTemplate: ScoreRankingSupportedDateTemplate,
promo?: number,
cacheArgs: Omit<GetRankingArgs, 'keyBase'>,
): Promise<RankCache[] | undefined> {
return await this.cacheUtilRankingService.getRanking({
keyBase: SCORE_RANKING,
dateTemplate,
promo,
...cacheArgs,
});
}

Expand Down
29 changes: 25 additions & 4 deletions app/src/cache/cache.util.ranking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type GetRankingArgs = {
keyBase: string;
dateTemplate: RankingSupportedDateTemplate;
promo?: number;
coalitionId?: number;
};

export type GetRankArgs = GetRankingArgs & { userId: number };
Expand Down Expand Up @@ -76,6 +77,7 @@ export class CacheUtilRankingService {
userId,
dateTemplate,
promo,
coalitionId,
}: GetRankArgs): Promise<RankCache | undefined> {
const key = this.cacheUtilService.buildKey(
keyBase,
Expand All @@ -91,18 +93,22 @@ export class CacheUtilRankingService {
return undefined;
}

if (!isPromoMatch({ promo, rankCache })) {
if (
!isPromoMatch({ promo, rankCache }) ||
!isCoalitionMatch({ coalitionId, rankCache })
) {
return {
...rankCache,
rank: 0,
};
}

if (promo) {
if (promo || coalitionId) {
const rankingCache = await this.getRawRanking({
keyBase,
dateTemplate,
promo,
coalitionId,
});

return rankingCache?.find(
Expand All @@ -117,6 +123,7 @@ export class CacheUtilRankingService {
keyBase,
dateTemplate,
promo,
coalitionId,
}: GetRankingArgs): Promise<RankCache[] | undefined> {
const key = this.cacheUtilService.buildKey(
keyBase,
Expand All @@ -131,9 +138,13 @@ export class CacheUtilRankingService {
return undefined;
}

if (promo) {
if (promo || coalitionId) {
return rankingCache
.filter((rankCache) => isPromoMatch({ promo, rankCache }))
.filter(
(rankCache) =>
isPromoMatch({ promo, rankCache }) &&
isCoalitionMatch({ coalitionId, rankCache }),
)
.reduce(
({ filtered, prevRank, prevValue }, currRankCache, index) => {
if (index === 0) {
Expand Down Expand Up @@ -401,3 +412,13 @@ const isPromoMatch = ({
// todo: nullable schema type 에 대한 처리를 한 후 수정해야 합니다.
return !promo || rankCache.promo === promo;
};

const isCoalitionMatch = ({
coalitionId,
rankCache,
}: {
coalitionId?: number;
rankCache: RankCache;
}): boolean => {
return !coalitionId || rankCache.coalition?.id === coalitionId;
};
12 changes: 6 additions & 6 deletions app/src/page/home/user/home.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ export class HomeUserService {
}

async walletRanking(limit: number): Promise<UserRank[]> {
const cachedRanking = await this.cursusUserCacheService.getUserRanking(
USER_WALLET_RANKING,
);
const cachedRanking = await this.cursusUserCacheService.getUserRanking({
keyBase: USER_WALLET_RANKING,
});

const walletRanking = cachedRanking
? cachedRanking.slice(0, limit)
Expand All @@ -155,9 +155,9 @@ export class HomeUserService {
}

async correctionPointRanking(limit: number): Promise<UserRank[]> {
const cachedRanking = await this.cursusUserCacheService.getUserRanking(
USER_CORRECTION_POINT_RANKING,
);
const cachedRanking = await this.cursusUserCacheService.getUserRanking({
keyBase: USER_CORRECTION_POINT_RANKING,
});

return cachedRanking
? cachedRanking.slice(0, limit)
Expand Down
22 changes: 11 additions & 11 deletions app/src/page/leaderboard/comment/leaderboard.comment.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { HttpExceptionFilter } from 'src/http-exception.filter';
import { GetLeaderboardElementArgs } from '../common/dtos/leaderboard.dto.getLeaderboardElemenetArgs';
import { LeaderboardElementDateRanged } from '../common/models/leaderboard.model';
import { LeaderboardUtilService } from '../util/leaderboard.util.service';
import { LeaderboardCommentService } from './leaderboard.comment.service';
import { LeaderboardComment } from './models/leaderboard.comment.model';

Expand All @@ -19,6 +20,7 @@ import { LeaderboardComment } from './models/leaderboard.comment.model';
export class LeaderboardCommentResolver {
constructor(
private readonly leaderboardCommentService: LeaderboardCommentService,
private readonly leaderboardUtilService: LeaderboardUtilService,
) {}

@Query((_returns) => LeaderboardComment)
Expand All @@ -28,8 +30,8 @@ export class LeaderboardCommentResolver {

@ResolveField((_returns) => LeaderboardElementDateRanged)
async byDateTemplate(
@MyUserId() myUserId: number,
@Args() { pageNumber, pageSize, promo }: GetLeaderboardElementArgs,
@MyUserId() userId: number,
@Args() getLeaderboardElementArgs: GetLeaderboardElementArgs,
@Args() { dateTemplate }: DateTemplateArgs,
): Promise<LeaderboardElementDateRanged> {
if (
Expand All @@ -42,14 +44,12 @@ export class LeaderboardCommentResolver {
throw new UnsupportedDateTemplate();
}

return await this.leaderboardCommentService.rankingByDateTemplate({
dateTemplate,
userId: myUserId,
paginationIndexArgs: {
pageNumber,
pageSize,
},
promo,
});
return await this.leaderboardCommentService.rankingByDateTemplate(
this.leaderboardUtilService.toLeaderboardServiceArgs({
userId,
getLeaderboardElementArgs,
dateTemplate,
}),
);
}
}
11 changes: 7 additions & 4 deletions app/src/page/leaderboard/comment/leaderboard.comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ export class LeaderboardCommentService {
userId,
paginationIndexArgs,
promo,
coalitionId,
}: RankingByDateTemplateArgs<AverageReviewLengthRankingSupportedDateTemplate>): Promise<LeaderboardElementDateRanged> {
const rank = await this.scaleTeamCacheService.getCommentRank(
const rank = await this.scaleTeamCacheService.getCommentRank({
dateTemplate,
userId,
promo,
);
coalitionId,
});

const ranking = await this.scaleTeamCacheService.getCommentRanking(
const ranking = await this.scaleTeamCacheService.getCommentRanking({
dateTemplate,
promo,
);
coalitionId,
});

assertExist(ranking);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ export class GetLeaderboardElementArgs extends PaginationIndexArgs {
@IsOptional()
@Field({ nullable: true })
promo?: number;

@Min(1)
@Max(1000)
@IsOptional()
@Field({ nullable: true })
coalitionId?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type RankingByDateTemplateArgs<
> = {
userId: number;
promo?: number;
coalitionId?: number;
paginationIndexArgs: PaginationIndexArgs;
dateTemplate: SupportDateTemplate;
};
Loading

0 comments on commit 0a38202

Please sign in to comment.