-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ daily coalition score 추가를 통한 scoreRecordsPerCoalition last 추가
- #392
- Loading branch information
Showing
11 changed files
with
233 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { coalition } from 'src/api/coalition/db/coalition.database.schema'; | ||
import type { DateRange } from 'src/dateRange/dtos/dateRange.dto'; | ||
|
||
export type FindScorePerCoalitionByDateInput = DateRange; | ||
|
||
export type FindScorePerCoalitionByDateOutput = { | ||
coalition: coalition; | ||
scores: { | ||
date: Date; | ||
value: number; | ||
}[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { CoalitionModule } from 'src/api/coalition/coalition.module'; | ||
import { DailyCoalitionScoreService } from './dailyCoalitionScore.service'; | ||
import { DailyCoalitionScoreDaoImpl } from './db/dailyCoalitionScore.database.dao'; | ||
import { | ||
dailyCoalitionScoreSchema, | ||
mv_daily_score_values, | ||
} from './db/dailyCoalitionScore.database.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ name: mv_daily_score_values.name, schema: dailyCoalitionScoreSchema }, | ||
]), | ||
CoalitionModule, | ||
], | ||
providers: [DailyCoalitionScoreService, DailyCoalitionScoreDaoImpl], | ||
exports: [DailyCoalitionScoreService], | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
export class DailyCoalitionScoreModule {} |
29 changes: 29 additions & 0 deletions
29
app/src/dailyCoalitionScore/dailyCoalitionScore.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { DailyCoalitionScoreDaoImpl } from './db/dailyCoalitionScore.database.dao'; | ||
import { ScoreRecordPerCoalition } from 'src/page/home/coalition/models/home.coalition.model'; | ||
import type { DateRange } from 'src/dateRange/dtos/dateRange.dto'; | ||
import { CoalitionService } from 'src/api/coalition/coalition.service'; | ||
|
||
@Injectable() | ||
export class DailyCoalitionScoreService { | ||
constructor( | ||
private readonly dailyCoalitionScoreDao: DailyCoalitionScoreDaoImpl, | ||
private readonly coalitionService: CoalitionService, | ||
) {} | ||
|
||
async scoreRecordsPerCoalitions({ | ||
start, | ||
end, | ||
}: DateRange): Promise<ScoreRecordPerCoalition[]> { | ||
const scoresPerCoalition = | ||
await this.dailyCoalitionScoreDao.findScoresPerCoalitionByDate({ | ||
start, | ||
end, | ||
}); | ||
|
||
return scoresPerCoalition.map(({ coalition, scores }) => ({ | ||
coalition: this.coalitionService.daoToDto(coalition), | ||
records: scores.map(({ date, value }) => ({ at: date, value })), | ||
})); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
app/src/dailyCoalitionScore/db/dailyCoalitionScore.database.dao.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import type { Model } from 'mongoose'; | ||
import { mv_daily_score_values } from './dailyCoalitionScore.database.schema'; | ||
import type { | ||
FindScorePerCoalitionByDateInput, | ||
FindScorePerCoalitionByDateOutput, | ||
} from '../dailyCoalitionScore.dto'; | ||
import { RUNTIME_CONFIG } from 'src/config/runtime'; | ||
import type { ConfigType } from '@nestjs/config'; | ||
import { lookupCoalition } from 'src/api/coalition/db/coalition.database.aggregate'; | ||
import { coalition } from 'src/api/coalition/db/coalition.database.schema'; | ||
|
||
export type DailyCoalitionScoreDao = { | ||
findScoresPerCoalitionByDate: ( | ||
args: FindScorePerCoalitionByDateInput, | ||
) => Promise<FindScorePerCoalitionByDateOutput[]>; | ||
}; | ||
|
||
@Injectable() | ||
export class DailyCoalitionScoreDaoImpl implements DailyCoalitionScoreDao { | ||
constructor( | ||
@InjectModel(mv_daily_score_values.name) | ||
private readonly dailyCoalitionScore: Model<mv_daily_score_values>, | ||
@Inject(RUNTIME_CONFIG.KEY) | ||
private readonly runtimeConfig: ConfigType<typeof RUNTIME_CONFIG>, | ||
) {} | ||
|
||
async findScoresPerCoalitionByDate({ | ||
start, | ||
end, | ||
}: FindScorePerCoalitionByDateInput): Promise< | ||
FindScorePerCoalitionByDateOutput[] | ||
> { | ||
return await this.dailyCoalitionScore | ||
.aggregate<FindScorePerCoalitionByDateOutput>() | ||
.match({ | ||
date: { | ||
$gte: start, | ||
$lt: end, | ||
}, | ||
}) | ||
.group({ | ||
_id: { | ||
date: { | ||
$dateFromParts: { | ||
year: { | ||
$year: { | ||
date: '$date', | ||
timezone: this.runtimeConfig.TIMEZONE, | ||
}, | ||
}, | ||
month: { | ||
$month: { | ||
date: '$date', | ||
timezone: this.runtimeConfig.TIMEZONE, | ||
}, | ||
}, | ||
timezone: this.runtimeConfig.TIMEZONE, | ||
}, | ||
}, | ||
coalitionId: '$coalitionId', | ||
}, | ||
value: { | ||
$sum: '$value', | ||
}, | ||
}) | ||
.sort({ | ||
'_id.date': 1, | ||
'_id.coalitionId': 1, | ||
}) | ||
.group({ | ||
_id: '$_id.coalitionId', | ||
scores: { | ||
$push: { | ||
date: '$_id.date', | ||
value: '$value', | ||
}, | ||
}, | ||
}) | ||
.append(lookupCoalition('_id', 'id')) | ||
.sort({ _id: 1 }) | ||
.project({ | ||
_id: 0, | ||
coalition: { | ||
$first: `$${coalition.name}s`, | ||
}, | ||
scores: 1, | ||
}); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/dailyCoalitionScore/db/dailyCoalitionScore.database.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; | ||
import type { HydratedDocument } from 'mongoose'; | ||
|
||
export type DailyCoalitionScoreDocument = | ||
HydratedDocument<mv_daily_score_values>; | ||
|
||
@Schema({ collection: 'mv_daily_score_values' }) | ||
export class mv_daily_score_values { | ||
@Prop({ required: true }) | ||
date: Date; | ||
|
||
@Prop({ required: true }) | ||
coalitionId: number; | ||
|
||
@Prop({ required: true }) | ||
value: number; | ||
} | ||
|
||
export const dailyCoalitionScoreSchema = SchemaFactory.createForClass( | ||
mv_daily_score_values, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.