Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/online teacher #81

Merged
merged 11 commits into from
Oct 8, 2023
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,
Comment on lines +359 to +360
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 내리면 user/profile/{userId}랑 형식이 달라지는데
univ, major로 내리는거 프런트 쪽에서도 협의된 내용이야?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swagger에 나오니까 괜찮을듯

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