-
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 logtime module 추가를 통해 logtimeRecords 의 last 지원 범위 증가
- #392
- Loading branch information
Showing
10 changed files
with
234 additions
and
42 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,14 @@ | ||
import type { DateRange } from 'src/dateRange/dtos/dateRange.dto'; | ||
|
||
export type UserLogtimeRecordsByDateRangeInput = { | ||
userId: number; | ||
} & DateRange; | ||
|
||
export type UserLogtimeRecordByDateRangeOutput = { | ||
/** | ||
* | ||
* @description %Y-%m Date 형식 | ||
*/ | ||
yearMonth: string; | ||
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,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { DailyLogtimeService } from './dailyLogtime.service'; | ||
import { DailyLogtimeDaoImpl } from './db/dailyLogtime.database.dao'; | ||
import { | ||
dailyLogtimeSchema, | ||
daily_logtimes, | ||
} from './db/dailyLogtime.database.schema'; | ||
|
||
@Module({ | ||
imports: [ | ||
MongooseModule.forFeature([ | ||
{ name: daily_logtimes.name, schema: dailyLogtimeSchema }, | ||
]), | ||
], | ||
providers: [DailyLogtimeService, DailyLogtimeDaoImpl], | ||
exports: [DailyLogtimeService], | ||
}) | ||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
export class DailyLogtimeModule {} |
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,61 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { | ||
UserLogtimeRecordByDateRangeOutput, | ||
UserLogtimeRecordsByDateRangeInput, | ||
} from './dailyLogtime.dto'; | ||
import { DailyLogtimeService } from './dailyLogtime.service'; | ||
import { DailyLogtimeDaoImpl } from './db/dailyLogtime.database.dao'; | ||
|
||
describe(DailyLogtimeService.name, () => { | ||
let dailyLogtimeService: DailyLogtimeService; | ||
let testExistUserId = 1; | ||
let testDateRange = { | ||
start: new Date('2021-01-01T15:00:00.000Z'), | ||
end: new Date('2021-02-31T15:00:00.000Z'), | ||
}; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [DailyLogtimeService], | ||
}) | ||
.useMocker((token) => { | ||
if (token === DailyLogtimeDaoImpl) { | ||
return { | ||
userLogtimeRecordsByDateRange: ( | ||
args: UserLogtimeRecordsByDateRangeInput, | ||
): UserLogtimeRecordByDateRangeOutput[] => { | ||
if (args.userId !== testExistUserId) { | ||
return []; | ||
} | ||
|
||
return [ | ||
{ | ||
yearMonth: '2021-01', | ||
value: 1, | ||
}, | ||
{ | ||
yearMonth: '2021-02', | ||
value: 2, | ||
}, | ||
]; | ||
}, | ||
}; | ||
} | ||
}) | ||
.compile(); | ||
|
||
dailyLogtimeService = moduleRef.get(DailyLogtimeService); | ||
}); | ||
|
||
it('반환 타입이 IntRecord[] 인지 확인한다.', async () => { | ||
const result = await dailyLogtimeService.userLogtimeRecordsByDateRange( | ||
testExistUserId, | ||
testDateRange, | ||
); | ||
|
||
result.forEach((curr) => { | ||
expect(isNaN(curr.at.getTime())).toBe(false); | ||
expect(typeof curr.value === 'number').toBe(true); | ||
}); | ||
}); | ||
}); |
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,30 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import type { IntRecord } from 'src/common/models/common.valueRecord.model'; | ||
import type { DateRange } from 'src/dateRange/dtos/dateRange.dto'; | ||
import { DailyLogtimeDaoImpl } from './db/dailyLogtime.database.dao'; | ||
|
||
@Injectable() | ||
export class DailyLogtimeService { | ||
constructor(private readonly dailyLogtimeDao: DailyLogtimeDaoImpl) {} | ||
|
||
async userLogtimeRecordsByDateRange( | ||
userId: number, | ||
{ start, end }: DateRange, | ||
): Promise<IntRecord[]> { | ||
const userLogtimeRecords = | ||
await this.dailyLogtimeDao.userLogtimeRecordsByDateRange({ | ||
userId, | ||
start, | ||
end, | ||
}); | ||
|
||
return userLogtimeRecords.map(({ yearMonth, value }) => { | ||
const [year, month] = yearMonth.split('-').map(Number); | ||
|
||
return { | ||
at: new Date(year, month - 1), | ||
value, | ||
}; | ||
}); | ||
} | ||
} |
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,60 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import type { ConfigType } from '@nestjs/config'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import type { Model } from 'mongoose'; | ||
import { RUNTIME_CONFIG } from 'src/config/runtime'; | ||
import type { | ||
UserLogtimeRecordByDateRangeOutput, | ||
UserLogtimeRecordsByDateRangeInput, | ||
} from '../dailyLogtime.dto'; | ||
import { daily_logtimes } from './dailyLogtime.database.schema'; | ||
|
||
export type DailyLogtimeDao = { | ||
userLogtimeRecordsByDateRange: ( | ||
args: UserLogtimeRecordsByDateRangeInput, | ||
) => Promise<UserLogtimeRecordByDateRangeOutput[]>; | ||
}; | ||
|
||
@Injectable() | ||
export class DailyLogtimeDaoImpl implements DailyLogtimeDao { | ||
constructor( | ||
@InjectModel(daily_logtimes.name) | ||
private readonly dailyLogtime: Model<daily_logtimes>, | ||
@Inject(RUNTIME_CONFIG.KEY) | ||
private readonly runtimeConfig: ConfigType<typeof RUNTIME_CONFIG>, | ||
) {} | ||
|
||
async userLogtimeRecordsByDateRange({ | ||
userId, | ||
start, | ||
end, | ||
}: UserLogtimeRecordsByDateRangeInput): Promise< | ||
UserLogtimeRecordByDateRangeOutput[] | ||
> { | ||
return await this.dailyLogtime | ||
.aggregate<UserLogtimeRecordByDateRangeOutput>() | ||
.match({ | ||
userId, | ||
date: { | ||
$gte: start, | ||
$lte: end, | ||
}, | ||
}) | ||
.group({ | ||
_id: { | ||
$dateToString: { | ||
date: '$date', | ||
format: '%Y-%m', | ||
timezone: this.runtimeConfig.TIMEZONE, | ||
}, | ||
}, | ||
value: { $sum: '$value' }, | ||
}) | ||
.sort({ _id: 1 }) | ||
.project({ | ||
_id: 0, | ||
yearMonth: '$_id', | ||
value: 1, | ||
}); | ||
} | ||
} |
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
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