From 265c1ea362ef77b661aebc20f90a93f303d07428 Mon Sep 17 00:00:00 2001 From: victolee0 <39608452+victolee0@users.noreply.github.com> Date: Mon, 11 Dec 2023 23:15:26 +0900 Subject: [PATCH] =?UTF-8?q?[BE/#213]=20=ED=95=99=EC=8A=B5=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=20(#416)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: 학습 기록 테스트코드 작성 * refactor: 시간 오프셋 찾기 --- BE/src/study-logs/study-logs.service.spec.ts | 82 ++++++++++++++++++++ BE/src/study-logs/study-logs.service.ts | 3 +- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/BE/src/study-logs/study-logs.service.spec.ts b/BE/src/study-logs/study-logs.service.spec.ts index cfb556f..0d70f5e 100644 --- a/BE/src/study-logs/study-logs.service.spec.ts +++ b/BE/src/study-logs/study-logs.service.spec.ts @@ -111,4 +111,86 @@ describe('StudyLogsService', () => { ).rejects.toThrow(BadRequestException); }); }); + + describe('.calculateLearningTimes()', () => { + it('학습한 시간이 자정을 지나는 경우 날짜 별로 시간을 나눈다 (한국 시간)', () => { + const result = service.calculateLearningTimes( + '2023-11-12T01:00:00+09:00', + 7200, + ); + expect(result).toEqual([ + { + started_at: '2023-11-11T14:00:00.000Z', + date: '2023-11-11', + learning_time: 3600, + }, + { + started_at: '2023-11-11T15:00:00.000Z', + date: '2023-11-12', + learning_time: 3600, + }, + ]); + }); + it('학습한 시간이 자정을 지나는 경우 날짜 별로 시간을 나눈다 (미국 시간)', () => { + const result = service.calculateLearningTimes( + '2023-11-12T01:00:00-0500', + 7200, + ); + expect(result).toEqual([ + { + started_at: '2023-11-12T04:00:00.000Z', + date: '2023-11-11', + learning_time: 3600, + }, + { + started_at: '2023-11-12T05:00:00.000Z', + date: '2023-11-12', + learning_time: 3600, + }, + ]); + }); + + it('학습한 시간이 자정을 지나지 않는 경우, 하루에 대한 학습 시간을 반환한다. (한국 시간)', () => { + const result = service.calculateLearningTimes( + '2023-11-12T02:00:00+09:00', + 7200, + ); + expect(result).toEqual([ + { + started_at: '2023-11-11T15:00:00.000Z', + date: '2023-11-12', + learning_time: 7200, + }, + ]); + }); + + it('학습한 시간이 자정을 지나지 않는 경우, 하루에 대한 학습 시간을 반환한다. (미국 시간)', () => { + const result = service.calculateLearningTimes( + '2023-11-12T02:00:00-0500', + 7200, + ); + expect(result).toEqual([ + { + started_at: '2023-11-12T05:00:00.000Z', + date: '2023-11-12', + learning_time: 7200, + }, + ]); + }); + }); + + describe('calculatePercentage()', () => { + it('유저의 총 학습 시간 기준 전체 유저의 학습 시간에 대한 백분율을 반환한다.', async () => { + for (let i = 1; i <= 4; i++) { + jest.spyOn(repository, 'query').mockResolvedValueOnce([ + { user_id: 1, total_time: 40000 }, + { user_id: 2, total_time: 20000 }, + { user_id: 3, total_time: 5000 }, + { user_id: 4, total_time: 1000 }, + ]); + jest.spyOn(usersRepository, 'count').mockResolvedValueOnce(4); + expect(await service.calculatePercentage(i, '', '')).toEqual(25 * i); + } + }); + }); }); diff --git a/BE/src/study-logs/study-logs.service.ts b/BE/src/study-logs/study-logs.service.ts index 6325f12..2cdab9c 100644 --- a/BE/src/study-logs/study-logs.service.ts +++ b/BE/src/study-logs/study-logs.service.ts @@ -73,7 +73,8 @@ export class StudyLogsService { created_at: string, learning_time: number, ): { started_at: string; date: string; learning_time: number }[] { - const finishedAt = moment(new Date(created_at)); + const offset = created_at.split(/\d\d:\d\d:\d\d/)[1]; + const finishedAt = moment(created_at).utcOffset(offset); const startedAt = finishedAt.clone().subtract(learning_time, 's'); if (startedAt.get('date') !== finishedAt.get('date')) { return [