diff --git a/BE/src/auth/auth.controller.ts b/BE/src/auth/auth.controller.ts index b7f3df0..0e4ceb3 100644 --- a/BE/src/auth/auth.controller.ts +++ b/BE/src/auth/auth.controller.ts @@ -134,4 +134,25 @@ export class AuthController { ), }; } + + @Patch('timezone') + @UseGuards(AccessTokenGuard) + @ApiOperation({ summary: '유저 타임존 설정 (완)' }) + @ApiResponse({ status: 200, description: '타임존 설정 성공' }) + @ApiResponse({ status: 400, description: '잘못된 요청' }) + @ApiResponse({ status: 401, description: '인증 실패' }) + @ApiBearerAuth() + async patchTimezone( + @User('id') user_id: number, + @Body('timezone') timezone: string, + ): Promise { + const updatedUser = await this.usersService.updateTimezone( + user_id, + timezone, + ); + return { + statusCode: 200, + message: '타임존 설정 성공', + }; + } } diff --git a/BE/src/mates/mates.controller.ts b/BE/src/mates/mates.controller.ts index 9eedbd4..fccc8e3 100644 --- a/BE/src/mates/mates.controller.ts +++ b/BE/src/mates/mates.controller.ts @@ -33,15 +33,21 @@ export class MatesController { @ApiCreatedResponse({ description: 'OK', }) - @ApiQuery({ - name: 'date', - example: '2023-11-22', - description: '날짜', + @ApiBody({ + schema: { + properties: { + date: { + type: 'datetime', + example: '2023-11-22T14:00:00+09:00', + description: '날짜', + }, + }, + }, }) @ApiOperation({ summary: '모든 친구들 조회하기 (완)' }) getMates( @User('id') user_id: number, - @Query('date') date: string, + @Body('date') date: string, ): Promise { return this.matesService.getMates(user_id, date); } diff --git a/BE/src/mates/mates.service.ts b/BE/src/mates/mates.service.ts index 600e36d..7a44b0c 100644 --- a/BE/src/mates/mates.service.ts +++ b/BE/src/mates/mates.service.ts @@ -54,17 +54,23 @@ export class MatesService { } async getMates(user_id: number, date: string): Promise { + const offset = date.split(/\d\d:\d\d:\d\d/)[1]; + + const nowUserTime = moment(date) + .utcOffset(offset) + .format('YYYY-MM-DD HH:mm:ss'); + const studyTimeByFollowing = await this.userRepository.query( ` SELECT u.id, u.nickname, u.image_url, COALESCE(SUM(s.learning_time), 0) AS total_time FROM users_model u LEFT JOIN mates m ON m.following_id = u.id - LEFT JOIN study_logs s ON s.user_id = u.id AND s.date = ? + LEFT JOIN study_logs s ON s.user_id = u.id AND s.date = DATE(CONVERT_TZ(?, ?, u.timezone)) WHERE m.follower_id = ? GROUP BY u.id ORDER BY total_time DESC `, - [date, user_id], + [nowUserTime, offset, user_id], ); return Promise.all( studyTimeByFollowing.map(async (record) => { diff --git a/BE/src/users/entity/users.entity.ts b/BE/src/users/entity/users.entity.ts index 86bd31b..3753e0a 100644 --- a/BE/src/users/entity/users.entity.ts +++ b/BE/src/users/entity/users.entity.ts @@ -52,6 +52,13 @@ export class UsersModel { }) auth_type: AuthTypeEnum; + @Column({ + type: 'char', + default: '+09:00', + length: 6, + }) + timezone: string; + @OneToMany(() => StudyLogs, (studyLog) => studyLog.user_id) study_logs: StudyLogs[]; diff --git a/BE/src/users/users.service.ts b/BE/src/users/users.service.ts index f4092e6..abc3d4a 100644 --- a/BE/src/users/users.service.ts +++ b/BE/src/users/users.service.ts @@ -79,6 +79,15 @@ export class UsersService { } } + async updateTimezone(user_id: number, timezone: string): Promise { + const selectedUser = await this.usersRepository.findOne({ + where: { id: user_id }, + }); + selectedUser.timezone = timezone; + const updatedUser = await this.usersRepository.save(selectedUser); + return updatedUser; + } + async isUniqueNickname(nickname: string): Promise { const isDuplicated = await this.usersRepository.exist({ where: { nickname },