Skip to content

Commit

Permalink
[BE#439] fix: 소셜 화면 타임존 이슈 해결 (#448)
Browse files Browse the repository at this point in the history
* fix: 소셜 화면 타임존 이슈 해결

- user 테이블에 timezone 생성
- getMates 메서드에서 요청 온 시각을 각 유저 타임존으로 바꿔 날짜 추출 후 학습 기록 조회하도록 수정

* fix: format 변경

* fix: 시간을 클라이언트에서 받도록 수정
  • Loading branch information
victolee0 authored Dec 13, 2023
1 parent e07cdba commit cb23e68
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 7 deletions.
21 changes: 21 additions & 0 deletions BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
const updatedUser = await this.usersService.updateTimezone(
user_id,
timezone,
);
return {
statusCode: 200,
message: '타임존 설정 성공',
};
}
}
16 changes: 11 additions & 5 deletions BE/src/mates/mates.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> {
return this.matesService.getMates(user_id, date);
}
Expand Down
10 changes: 8 additions & 2 deletions BE/src/mates/mates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,23 @@ export class MatesService {
}

async getMates(user_id: number, date: string): Promise<object[]> {
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) => {
Expand Down
7 changes: 7 additions & 0 deletions BE/src/users/entity/users.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down
9 changes: 9 additions & 0 deletions BE/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ export class UsersService {
}
}

async updateTimezone(user_id: number, timezone: string): Promise<UsersModel> {
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<object> {
const isDuplicated = await this.usersRepository.exist({
where: { nickname },
Expand Down

0 comments on commit cb23e68

Please sign in to comment.