Skip to content

Commit

Permalink
feat: ✨ winCountPerCoalition μΆ”κ°€
Browse files Browse the repository at this point in the history
- close #321

fix: πŸ› 0 점일 λ•Œ null 둜 λ°˜ν™˜λ˜λ˜ 둜직 μˆ˜μ •

- close #325
  • Loading branch information
niamu01 committed Sep 22, 2023
1 parent 00166d8 commit afeb26d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 33 deletions.
4 changes: 1 addition & 3 deletions app/src/api/scaleTeam/scaleTeam.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ export class ScaleTeamService {
)
.addFields({
value: {
$round: {
$first: '$scale_teams.value',
},
$ifNull: [{ $round: { $first: '$scale_teams.value' } }, 0],
},
})
.append(addRank())
Expand Down
2 changes: 1 addition & 1 deletion app/src/api/score/score.cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable } from '@nestjs/common';
import {
CacheUtilRankingService,
type RankingSupportedDateTemplate,
type RankCache,
type RankingSupportedDateTemplate,
} from 'src/cache/cache.util.ranking.service';
import { CacheUtilService } from 'src/cache/cache.util.service';
import { DateTemplate } from 'src/dateRange/dtos/dateRange.dto';
Expand Down
90 changes: 66 additions & 24 deletions app/src/api/score/score.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ export class ScoreService {
return await aggregate
.append(
lookupCoalitionsUser('user.id', 'userId', [
{
$match: {
coalitionId: { $in: targetCoalitionIds },
},
},
{ $match: { coalitionId: { $in: targetCoalitionIds } } },
]),
)
.addFields({ coalitions_users: { $first: '$coalitions_users' } })
Expand All @@ -56,19 +52,13 @@ export class ScoreService {
args?.filter ? [{ $match: args.filter }] : undefined,
),
)
.addFields({
value: {
$sum: '$scores.value',
},
})
.addFields({ value: { $sum: '$scores.value' } })
.append(addRank())
.append(addUserPreview('user'))
.project({
_id: 0,
userPreview: 1,
coalition: {
id: '$coalitions_users.coalitionId',
},
coalition: { id: '$coalitions_users.coalitionId' },
value: 1,
rank: 1,
});
Expand All @@ -87,15 +77,17 @@ export class ScoreService {
coalitionsUserId: { $ne: null },
coalitionId: { $in: targetCoalitionIds },
})
.group({ _id: '$coalitionId', value: { $sum: '$value' } })
.lookup({
from: 'coalitions',
localField: '_id',
foreignField: 'id',
as: 'coalition',
.group({
_id: '$coalitionId',
value: { $sum: '$value' },
})
.append(lookupCoalition('_id', 'id'))
.sort({ _id: 1 })
.project({ _id: 0, coalition: { $first: '$coalition' }, value: 1 });
.project({
_id: 0,
coalition: { $first: '$coalitions' },
value: 1,
});
}

async scoreRecordsPerCoalition(args?: {
Expand Down Expand Up @@ -144,7 +136,11 @@ export class ScoreService {
})
.sort({ _id: 1 })
.append(lookupCoalition('_id', 'id'))
.project({ _id: 0, coalition: { $first: '$coalitions' }, records: 1 });
.project({
_id: 0,
coalition: { $first: '$coalitions' },
records: 1,
});
}

async tigCountPerCoalition(args?: {
Expand All @@ -160,9 +156,7 @@ export class ScoreService {

return await aggregate
.match({ id: { $in: targetCoalitionIds } })
.addFields({
coalition: '$$ROOT',
})
.addFields({ coalition: '$$ROOT' })
.append(
lookupScores('id', 'coalitionId', [
{
Expand All @@ -188,4 +182,52 @@ export class ScoreService {
})),
);
}

async winCountPerCoalition(args?: {
targetCoalitionIds?: readonly number[];
filter?: FilterQuery<score>;
}): Promise<IntPerCoalition[]> {
const targetCoalitionIds =
args?.targetCoalitionIds ?? this.coalitionService.getSeoulCoalitionIds();

const aggregate = this.scoreModel.aggregate<IntPerCoalition>();

return await aggregate
.match({
...args?.filter,
coalitionsUserId: { $ne: null },
coalitionId: { $in: targetCoalitionIds },
})
.group({
_id: {
coalitionId: '$coalitionId',
at: {
$dateToString: {
format: '%Y-%m',
date: '$createdAt',
timezone: process.env.TZ,
},
},
},
value: { $sum: '$value' },
})
.sort({
at: 1,
value: -1,
})
.group({
_id: '$_id.at',
winCoalition: { $first: '$$ROOT' },
})
.group({
_id: '$winCoalition._id.coalitionId',
value: { $count: {} },
})
.append(lookupCoalition('_id', 'id'))
.project({
_id: 0,
coalition: { $first: '$coalitions' },
value: 1,
});
}
}
5 changes: 5 additions & 0 deletions app/src/page/home/coalition/home.coalition.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ export class HomeCoalitionResolver {
dateTemplate,
);
}

@ResolveField((_returns) => [IntPerCoalition])
async winCountPerCoalition(): Promise<IntPerCoalition[]> {
return await this.homeCoalitionService.winCountPerCoalition();
}
}
16 changes: 15 additions & 1 deletion app/src/page/home/coalition/home.coalition.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ScoreCacheService } from 'src/api/score/score.cache.service';
import { ScoreService } from 'src/api/score/score.service';
import { CacheOnReturn } from 'src/cache/decrators/onReturn/cache.decorator.onReturn.symbol';
import { DateRangeService } from 'src/dateRange/dateRange.service';
import type { DateRange, DateTemplate } from 'src/dateRange/dtos/dateRange.dto';
import { DateRange, DateTemplate } from 'src/dateRange/dtos/dateRange.dto';
import { DateWrapper } from 'src/dateWrapper/dateWrapper';
import type {
IntPerCoalition,
Expand Down Expand Up @@ -58,4 +58,18 @@ export class HomeCoalitionService {

return this.dateRangeService.toDateRanged(tigCountPerCoalition, dateRange);
}

@CacheOnReturn()
async winCountPerCoalition(): Promise<IntPerCoalition[]> {
const currmonth = new DateWrapper().startOfMonth();

const dateRange: DateRange = {
...this.dateRangeService.dateRangeFromTemplate(DateTemplate.TOTAL),
end: currmonth.toDate(),
};

return await this.scoreService.winCountPerCoalition({
filter: scoreDateRangeFilter(dateRange),
});
}
}
3 changes: 1 addition & 2 deletions app/src/page/leaderboard/score/leaderboard.score.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export class LeaderboardScoreResolver {
async byDateTemplate(
@MyUserId() myUserId: number,
@Args() paginationIndexArgs: PaginationIndexArgs,
@Args({ description: 'TOTAL, CURR_MONTH, CURR_WEEK 만 κ°€λŠ₯ν•©λ‹ˆλ‹€' })
{ dateTemplate }: DateTemplateArgs,
@Args() { dateTemplate }: DateTemplateArgs,
): Promise<LeaderboardElementDateRanged> {
if (
!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LeaderboardElementDateRanged } from '../../models/leaderboard.model';
@ObjectType()
export class LeaderboardScore {
@Field({
description: 'Available DateTemplate=[TOTAL, CURR_WEEK, CURR_MONTH]',
description: 'TOTAL, CURR_WEEK, CURR_MONTH 만 κ°€λŠ₯ν•©λ‹ˆλ‹€',
})
byDateTemplate: LeaderboardElementDateRanged;
}
3 changes: 2 additions & 1 deletion app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type HomeCoalition {
totalScoresPerCoalition: [IntPerCoalition!]!
scoreRecordsPerCoalition: [ScoreRecordPerCoalition!]!
tigCountPerCoalitionByDateTemplate(dateTemplate: DateTemplate!): IntPerCoalitionDateRanged!
winCountPerCoalition: [IntPerCoalition!]!
}

enum DateTemplate {
Expand Down Expand Up @@ -285,7 +286,7 @@ type LeaderboardLevel {
}

type LeaderboardScore {
"""Available DateTemplate=[TOTAL, CURR_WEEK, CURR_MONTH]"""
"""TOTAL, CURR_WEEK, CURR_MONTH 만 κ°€λŠ₯ν•©λ‹ˆλ‹€"""
byDateTemplate(pageSize: Int! = 10, pageNumber: Int! = 1, dateTemplate: DateTemplate!): LeaderboardElementDateRanged!
}

Expand Down

0 comments on commit afeb26d

Please sign in to comment.