Skip to content

Commit

Permalink
Fix/online teacher (#81)
Browse files Browse the repository at this point in the history
* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정

* 온라인 선생님 수정
  • Loading branch information
seongyunlee authored Oct 8, 2023
1 parent 6a466ba commit 3e00f27
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ export class UserController {

@ApiTags('User')
@ApiOperation(UserOperation.onlineTeacher)
@ApiBearerAuth('Authorization')
@ApiResponse(UserResponse.onlineTeacher)
@Get('user/list/teacher/online')
getOnlineTeachers(@Headers() headers: Headers) {
return this.userService.getOnlineTeachers();
return this.userService.getOnlineTeachers(AccessToken.userId(headers));
}

@ApiTags('User')
Expand Down
55 changes: 40 additions & 15 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,46 @@ export class UserService {
}
}

async getOnlineTeachers() {
const users = await this.redisRepository.getAllKeys();
console.log(users);
if (users.length == 0)
return new Success('현재 온라인 선생님이 없습니다.', []);
const userInfos = await this.userRepository.usersInfo(users);
const onlineTeachers = userInfos.filter((user) => user.role == 'teacher');
const result = onlineTeachers.map((teacher) => {
const { id, name, profileImage, followers } = teacher;
return { id, name, profileImage, followers: followers.length };
});
return new Success(
'현재 온라인 선생님들을 성공적으로 가져왔습니다.',
result,
);
async getOnlineTeachers(userId: string) {
try {
const users = await this.redisRepository.getAllKeys();
const userState = await Promise.all(
users.map(async (user) => {
return {
id: user,
online: (await this.redisRepository.getSocketId(user)) != null,
};
}),
);
const onlineUsers = userState.filter((user) => user.online);
if (onlineUsers.length == 0)
return new Success('현재 온라인 선생님이 없습니다.', []);
const userIds = onlineUsers.map((teacher) => teacher.id);
const userInfos = await this.userRepository.usersInfo(userIds);
const teacherInfos = userInfos.filter((user) => user.role == 'teacher');
const result: TeacherListing[] = await Promise.all(
teacherInfos.map(async (teacher) => {
return {
id: teacher.id,
name: teacher.name,
profileImage: teacher.profileImage,
role: teacher.role,
univ: teacher.school.name,
major: teacher.school.department,
followerIds: teacher.followers,
reserveCnt: (
await this.tutoringRepository.getTutoringCntOfTeacher(teacher.id)
).length,
};
}),
);
return new Success(
'현재 온라인 선생님들을 성공적으로 가져왔습니다.',
result,
);
} catch (error) {
return new Fail(error.message);
}
}

async otherFollowing(userId: string) {
Expand Down

0 comments on commit 3e00f27

Please sign in to comment.