Skip to content

Commit

Permalink
feat: 플래닝 포커에서 user join 시, 프로필 이미지 응답 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchef1 committed Nov 26, 2024
1 parent 1075f9c commit 9468ba0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
4 changes: 3 additions & 1 deletion apps/server/src/account/account.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import { AccessTokenGuard } from '@/account/guard/accessToken.guard';
import { RefreshTokenGuard } from '@/account/guard/refreshToken.guard';
import { AuthService } from '@/account/auth.service';
import { Account } from '@/account/entity/account.entity';
import { UserService } from './user.service';

@Module({
imports: [JwtModule.register({ global: true }), TypeOrmModule.forFeature([Account])],
controllers: [AuthController],
providers: [
AuthService,
UserService,
AccessTokenStrategy,
RefreshTokenStrategy,
AccessTokenGuard,
RefreshTokenGuard,
],
exports: [AccessTokenGuard],
exports: [AccessTokenGuard, UserService],
})
export class AccountModule {}
3 changes: 3 additions & 0 deletions apps/server/src/account/dto/user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export class UserDto {

username: string;

profieImage: string;

constructor(account: Account) {
this.id = account.id;
this.username = account.username;
this.profieImage = account.profileImage;
}
}
13 changes: 13 additions & 0 deletions apps/server/src/account/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Account } from './entity/account.entity';

@Injectable()
export class UserService {
constructor(@InjectRepository(Account) private userRepository: Repository<Account>) {}

async getOne(id: number) {
return this.userRepository.findOneBy({ id });
}
}
43 changes: 33 additions & 10 deletions apps/server/src/planning-poker/gateway/planning-poker.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import {
WebSocketServer,
} from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';
import { UserService } from '@/account/user.service';

@WebSocketGateway()
export class PlanningPokerGateway implements OnGatewayConnection, OnGatewayDisconnect {
constructor(private readonly userService: UserService) {}

@WebSocketServer()
server: Server;

Expand All @@ -18,9 +21,10 @@ export class PlanningPokerGateway implements OnGatewayConnection, OnGatewayDisco
{ isRevealed: boolean; users: Map<number, { username: string; card: string }> }
> = new Map();

handleConnection(client: Socket) {
async handleConnection(client: Socket) {
const { projectId } = client.handshake.auth;
const { id, username } = client.data.user;
const { profileImage } = await this.userService.getOne(id);

if (!this.selectedCards.has(projectId)) {
this.selectedCards.set(projectId, { isRevealed: false, users: new Map() });
Expand All @@ -29,19 +33,38 @@ export class PlanningPokerGateway implements OnGatewayConnection, OnGatewayDisco

client.join(projectId);

const userDetails = await this.getUserDetails(projectId);

client.emit('users_fetched', userDetails);
this.broadcastToOthers(client, 'user_joined', { userId: id, username, profileImage });
}

private async getUserDetails(projectId: string) {
const getCardStatus = (card: string, isRevealed: boolean) => {
if (isRevealed) {
return card;
}
return card === '' ? '' : 'SELECTED';
};

const projectCards = this.getProjectCardsOrThrow(projectId);

const userDetails = {
const users = await Promise.all(
Array.from(projectCards.users.entries()).map(async ([userId, { username, card }]) => {
const user = await this.userService.getOne(userId);
return {
userId,
username,
card: getCardStatus(card, projectCards.isRevealed),
profileImage: user.profileImage,
};
})
);

return {
isRevealed: projectCards.isRevealed,
users: Array.from(projectCards.users.entries()).map(([userId, { username, card }]) => ({
userId,
username,
card: projectCards.isRevealed ? card : card === '' ? '' : 'SELECTED',
})),
users,
};

client.emit('users_fetched', userDetails);
this.broadcastToOthers(client, 'user_joined', { userId: id, username });
}

handleDisconnect(client: Socket) {
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/planning-poker/planning-poker.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Module } from '@nestjs/common';
import { PlanningPokerGateway } from './gateway/planning-poker.gateway';
import { AccountModule } from '@/account/account.module';

@Module({
providers: [PlanningPokerGateway],
imports: [AccountModule],
})
export class PlanningPokerModule {}

0 comments on commit 9468ba0

Please sign in to comment.