Skip to content

Commit

Permalink
Merge pull request #362 from modern-agile-team/feat/#360/modify_chat_…
Browse files Browse the repository at this point in the history
…logic
  • Loading branch information
hobiJeong authored Mar 15, 2024
2 parents 0664701 + 1f428a1 commit 4b8f9ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TokenService } from '@src/auth/services/token.service';
import { BannedUserModule } from '@src/admins/banned-user/banned-users.module';
import { JwtModuleOptionsFactory } from '@src/auth/jwt/factories/jwt-module-options.factory';
import { S3Module } from '@src/common/s3/s3.module';
import { ChatModule } from '@src/chat/chat.module';

@Module({
imports: [
Expand All @@ -27,6 +28,7 @@ import { S3Module } from '@src/common/s3/s3.module';
}),
BannedUserModule,
S3Module,
ChatModule,
],
exports: [TokenService, TokenRepository],
controllers: [AuthController],
Expand Down
5 changes: 5 additions & 0 deletions src/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { BannedUserErrorResponseDto } from '@src/admins/banned-user/dtos/banned-
import { UserIntroService } from '@src/users/services/user-intro-service';
import { AppConfigService } from '@src/core/app-config/services/app-config.service';
import { ENV_KEY } from '@src/core/app-config/constants/app-config.constant';
import { ChatService } from '@src/chat/services/chat.service';

@Injectable()
export class AuthService implements AuthServiceInterface {
Expand All @@ -31,6 +32,7 @@ export class AuthService implements AuthServiceInterface {
private readonly dataSource: DataSource,
private readonly userIntroService: UserIntroService,
private readonly appConfigService: AppConfigService,
private readonly chatService: ChatService,
) {}

async login(authorizeCode: string, provider: UserProvider) {
Expand Down Expand Up @@ -446,6 +448,9 @@ export class AuthService implements AuthServiceInterface {
HttpStatus.NOT_FOUND,
);
}

await this.chatService.leaveChatRooms(userId);

return { message: '사용자 계정 삭제가 완료되었습니다.' };
}
}
7 changes: 3 additions & 4 deletions src/chat/chat.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { S3Module } from '@src/common/s3/s3.module';
import { AuthModule } from '@src/auth/auth.module';
import { UserModule } from '@src/users/user.module';
import { ChatController } from '@src/chat/controllers/chat.controller';
import { EventsGateway } from '@src/chat/events/events.gateway';
Expand All @@ -24,10 +23,10 @@ import { NotificationService } from '@src/chat/services/notification.service';
{ name: ChatImages.name, schema: ChatImagesSchema },
]),
S3Module,
AuthModule,
UserModule,
forwardRef(() => UserModule),
],
controllers: [ChatController],
providers: [ChatService, ChatRepository, NotificationService, EventsGateway],
exports: [ChatService],
})
export class ChatModule {}
2 changes: 1 addition & 1 deletion src/chat/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { ApiLeaveChatRoom } from '@src/chat/swagger-decorators/leave-chat-room.d
@UseInterceptors(SuccessResponseInterceptor, ClassSerializerInterceptor)
@Controller('chat-room')
export class ChatController {
constructor(private chatService: ChatService) {}
constructor(private readonly chatService: ChatService) {}

/**
*
Expand Down
24 changes: 19 additions & 5 deletions src/chat/services/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,23 @@ export class ChatService {
throw new ForbiddenException('본인과 채팅방을 생성할 수 없습니다.');
}

const existReceiver = await this.userService.findOneByOrNotFound({
select: ['id'],
where: { id: receiverId },
});

const existChatRoom = await this.chatRepository.findOneChatRoom({
$and: [
{ originalMembers: { $all: [myId, receiverId] } },
{ originalMembers: { $all: [myId, existReceiver.id] } },
{ deletedAt: null },
{ chatRoomType: chatRoomType },
],
});

if (!existChatRoom) {
const returnedChatRoom = await this.chatRepository.createChatRoom({
originalMembers: [myId, receiverId],
chatMembers: [myId, receiverId],
originalMembers: [myId, existReceiver.id],
chatMembers: [myId, existReceiver.id],
chatRoomType: chatRoomType,
});

Expand All @@ -157,14 +162,14 @@ export class ChatService {

const { chatMembers, _id } = existChatRoom;

const pushUserId = chatMembers.includes(myId) ? receiverId : myId;
const pushUserId = chatMembers.includes(myId) ? existReceiver.id : myId;

if (chatMembers.length === 2) {
return new ChatRoomDto(existChatRoom);
}

const updateWriteOpResult = await this.chatRepository.updateOneChatRoom(
{ _id: _id },
{ _id },
{
$push: { chatMembers: pushUserId },
},
Expand Down Expand Up @@ -205,6 +210,15 @@ export class ChatService {
);
}

leaveChatRooms(userId: number) {
return this.chatRepository.updateManyChatRoom(
{
chatMembers: { $in: userId },
},
{ $pull: { chatMembers: userId } },
);
}

/**
*
* @param myId
Expand Down

0 comments on commit 4b8f9ca

Please sign in to comment.