Skip to content

Commit

Permalink
fix: 🐛 personal general 의 score info 에서 공동 순위에 대한 처리가 없던 문제
Browse files Browse the repository at this point in the history
- close #381
  • Loading branch information
jpham005 committed Oct 30, 2023
1 parent 86cb3c7 commit 60c0af2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 42 deletions.
19 changes: 19 additions & 0 deletions app/src/api/coalitionsUser/coalitionsUserModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import {
CoalitionsUserSchema,
coalitions_user,
} from './db/coalitionsUser.database.schema';
import { CoalitionsUserService } from './coalitionsUserService';

@Module({
imports: [
MongooseModule.forFeature([
{ name: coalitions_user.name, schema: CoalitionsUserSchema },
]),
],
providers: [CoalitionsUserService],
exports: [CoalitionsUserService, MongooseModule],
})
// eslint-disable-next-line
export class CoalitionsUserModule {}
22 changes: 22 additions & 0 deletions app/src/api/coalitionsUser/coalitionsUserService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { coalitions_user } from './db/coalitionsUser.database.schema';
import type { Model } from 'mongoose';

@Injectable()
export class CoalitionsUserService {
constructor(
@InjectModel(coalitions_user.name)
private readonly coalitionsUserModel: Model<coalitions_user>,
) {}

async findCoalitionsUserByUserIdAndLean(
userId: number,
): Promise<coalitions_user | null> {
return await this.coalitionsUserModel
.findOne({
userId,
})
.lean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class UserScoreInfo {
@Field({ nullable: true })
rankInCoalition?: number;

@Field()
@Field({ deprecationReason: 'deprecated at v0.9.0' })
rankInTotal: number;
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/page/personal/general/personal.general.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { CoalitionsUserModule } from 'src/api/coalitionsUser/coalitionsUserModule';
import { CursusUserModule } from 'src/api/cursusUser/cursusUser.module';
import { ExperienceUserModule } from 'src/api/experienceUser/experienceUser.module';
import { LocationModule } from 'src/api/location/location.module';
Expand All @@ -25,6 +26,7 @@ import { PersonalGeneralService } from './personal.general.service';
ProjectsUserModule,
ProjectModule,
ExperienceUserModule,
CoalitionsUserModule,
DateRangeModule,
],
providers: [PersonalGeneralResolver, PersonalGeneralService],
Expand Down
65 changes: 25 additions & 40 deletions app/src/page/personal/general/personal.general.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import type { FilterQuery } from 'mongoose';
import { CoalitionsUserService } from 'src/api/coalitionsUser/coalitionsUserService';
import { CursusUserCacheService } from 'src/api/cursusUser/cursusUser.cache.service';
import { CursusUserService } from 'src/api/cursusUser/cursusUser.service';
import { promoFilter } from 'src/api/cursusUser/db/cursusUser.database.query';
import { ExperienceUserService } from 'src/api/experienceUser/experienceUser.service';
import { locationDateRangeFilter } from 'src/api/location/db/location.database.aggregate';
import type { location } from 'src/api/location/db/location.database.schema';
import { LocationService } from 'src/api/location/location.service';
import { scoreDateRangeFilter } from 'src/api/score/db/score.database.aggregate';
import { ScoreCacheService } from 'src/api/score/score.cache.service';
import { ScoreService } from 'src/api/score/score.service';
import { TeamService } from 'src/api/team/team.service';
import { CacheOnReturn } from 'src/cache/decrators/onReturn/cache.decorator.onReturn.symbol';
import type { IntDateRanged } from 'src/common/models/common.dateRanaged.model';
Expand All @@ -34,10 +33,10 @@ export class PersonalGeneralService {
private readonly cursusUserService: CursusUserService,
private readonly cursusUserCacheService: CursusUserCacheService,
private readonly locationService: LocationService,
private readonly scoreService: ScoreService,
private readonly scoreCacheService: ScoreCacheService,
private readonly teamService: TeamService,
private readonly experineceUserService: ExperienceUserService,
private readonly coalitionsUserService: CoalitionsUserService,
private readonly dateRangeService: DateRangeService,
) {}

Expand Down Expand Up @@ -82,51 +81,37 @@ export class PersonalGeneralService {
async scoreInfo(userId: number): Promise<UserScoreInfo> {
const dateTemplate = DateTemplate.CURR_MONTH;

const scoreRankingCache = await this.scoreCacheService.getScoreRanking({
dateTemplate,
});

const dateRange = this.dateRangeService.dateRangeFromTemplate(dateTemplate);

const scoreRanking =
scoreRankingCache ??
(await this.scoreService.scoreRanking({
filter: scoreDateRangeFilter(dateRange),
}));

const me = scoreRanking.find(
({ userPreview }) => userPreview.id === userId,
);

if (!me) {
const filtered = await this.scoreCacheService.getScoreRank({
dateTemplate,
const coalitionsUser =
await this.coalitionsUserService.findCoalitionsUserByUserIdAndLean(
userId,
});

if (!filtered) {
throw new NotFoundException();
}
);

if (!coalitionsUser) {
return {
value: filtered.value,
rankInTotal: filtered.rank,
rankInCoalition: filtered.rank,
value: 0,
rankInTotal: 0,
rankInCoalition: 0,
};
}

const rankInCoalition = me.coalition
? scoreRanking
.filter(
({ coalition }) => coalition && coalition.id === me.coalition?.id,
)
.findIndex(({ userPreview }) => userPreview.id === userId) + 1
: undefined;
const scoreRank = await this.scoreCacheService.getScoreRank({
dateTemplate,
userId,
coalitionId: coalitionsUser.coalitionId,
});

if (!scoreRank) {
return {
value: 0,
rankInTotal: 0,
rankInCoalition: 0,
};
}

return {
value: me.value,
rankInTotal: me.rank,
rankInCoalition,
value: scoreRank.value,
rankInTotal: scoreRank.rank,
rankInCoalition: scoreRank.rank,
};
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ type LevelRecord {
type UserScoreInfo {
value: Int!
rankInCoalition: Int
rankInTotal: Int!
rankInTotal: Int! @deprecated(reason: "deprecated at v0.9.0")
}

type PersonalGeneral {
Expand Down

0 comments on commit 60c0af2

Please sign in to comment.