From 06d4d776daeb93559fb7bdb15512d35ed94c3199 Mon Sep 17 00:00:00 2001 From: NaayoungKwon Date: Tue, 22 Nov 2022 23:55:51 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=BB=A4=EB=AE=A4=EB=8B=88=ED=8B=B0?= =?UTF-8?q?=EC=97=90=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=B6=94=EA=B0=80=20?= =?UTF-8?q?service=20layer=20=EA=B5=AC=ED=98=84=20#95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/src/community/community.controller.ts | 22 +++++++++++---- .../api/src/community/community.service.ts | 28 +++++++++++++++++++ .../append-particitants-to-community.dto.ts | 23 +++++++++++++++ server/dao/repository/community.repository.ts | 10 +++++++ server/tsconfig.json | 1 + 5 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 server/apps/api/src/community/dto/append-particitants-to-community.dto.ts diff --git a/server/apps/api/src/community/community.controller.ts b/server/apps/api/src/community/community.controller.ts index a499bcb9..70d9e8c6 100644 --- a/server/apps/api/src/community/community.controller.ts +++ b/server/apps/api/src/community/community.controller.ts @@ -13,6 +13,7 @@ import { CommunityService } from '@api/src/community/community.service'; import { CreateCommunityDto } from '@api/src/community/dto/create-community.dto'; import { responseForm } from '@utils/responseForm'; import { JwtAccessGuard } from '@api/src/auth/guard'; +import { AppendUsersToCommunityDto } from '@community/dto/append-particitants-to-community.dto'; @Controller('api/community') export class CommunityController { @@ -41,19 +42,28 @@ export class CommunityController { } } - @Post(':community_id/participants') + @Post('participants') @UseGuards(JwtAccessGuard) async appendParticipantsToCommunity( - @Param('community_id') community_id: string, - @Body('users') users: string[], + // @Param('community_id') community_id: string, + // @Body('users') users: string[], + @Body() appendUsersToCommunityDto: AppendUsersToCommunityDto, @Req() req: any, ) { try { const _id = req.user._id; - const appendUsersToCommunityDto = { requestUser_id: _id, community_id }; - console.log(users); - const result = await this.communityService.appendParticipantsToCommunity(); + // const appendUsersToCommunityDto: AppendUsersToCommunityDto = { + // requestUser_id: _id, + // community_id, + // }; + // console.log(users); + const result = await this.communityService.appendParticipantsToCommunity({ + ...appendUsersToCommunityDto, + requestUser_id: _id, + }); + return responseForm(200, result); } catch (error) { + this.logger.error(JSON.stringify(error.response)); if (process.env.NODE_ENV == 'prod') { throw error; } else { diff --git a/server/apps/api/src/community/community.service.ts b/server/apps/api/src/community/community.service.ts index 5b033bca..a3651cd5 100644 --- a/server/apps/api/src/community/community.service.ts +++ b/server/apps/api/src/community/community.service.ts @@ -2,6 +2,8 @@ import { BadRequestException, Injectable } from '@nestjs/common'; import { CreateCommunityDto } from '@api/src/community/dto/create-community.dto'; import { CommunityRepository } from '@repository/community.repository'; import { UserRepository } from '@repository/user.repository'; +import { AppendUsersToCommunityDto } from '@api/src/community/dto/append-particitants-to-community.dto'; +import { getUserBasicInfo } from '@user/dto/user-basic-info.dto'; @Injectable() export class CommunityService { @@ -20,4 +22,30 @@ export class CommunityService { users: [createCommunityDto.managerId], }); } + + async appendParticipantsToCommunity(appendUsersToCommunityDto: AppendUsersToCommunityDto) { + const community = await this.communityRepository.findById( + appendUsersToCommunityDto.community_id, + ); + if (!community) { + throw new BadRequestException('해당하는 커뮤니티의 _id가 올바르지 않습니다.'); + } + await Promise.all( + appendUsersToCommunityDto.users.map((user) => + this.userRepository.findById(user).then((result) => { + if (!result) { + throw new BadRequestException( + `커뮤니티에 추가를 요청한 사용자 _id(${user})가 올바르지 않습니다.`, + ); + } + }), + ), + ); + await this.communityRepository.addArrAtArr( + { _id: appendUsersToCommunityDto.community_id }, + 'users', + appendUsersToCommunityDto.users, + ); + return { message: '커뮤니티 사용자 추가 완료' }; + } } diff --git a/server/apps/api/src/community/dto/append-particitants-to-community.dto.ts b/server/apps/api/src/community/dto/append-particitants-to-community.dto.ts new file mode 100644 index 00000000..fb6b8ab7 --- /dev/null +++ b/server/apps/api/src/community/dto/append-particitants-to-community.dto.ts @@ -0,0 +1,23 @@ +import { + ArrayNotEmpty, + IsArray, + IsNotEmpty, + IsNumber, + IsOptional, + IsString, +} from 'class-validator'; + +export class AppendUsersToCommunityDto { + @IsNotEmpty() + @IsString() + community_id: string; + + @IsOptional() + @IsString() + requestUser_id: string; + + @IsNotEmpty() + @IsArray() + @ArrayNotEmpty() + users: string[]; +} diff --git a/server/dao/repository/community.repository.ts b/server/dao/repository/community.repository.ts index f73f1f2f..d7becfb0 100644 --- a/server/dao/repository/community.repository.ts +++ b/server/dao/repository/community.repository.ts @@ -12,4 +12,14 @@ export class CommunityRepository { const result = await this.communityModel.create(createCommunityDto); return (result as any)._doc; } + + async findById(_id: string) { + return await this.communityModel.findById(_id); + } + + async addArrAtArr(filter, attribute, appendArr) { + const addArr = {}; + addArr[attribute] = { $each: appendArr }; + await this.communityModel.updateOne(filter, { $addToSet: addArr }); + } } diff --git a/server/tsconfig.json b/server/tsconfig.json index b43f558a..28fdce23 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -23,6 +23,7 @@ "@utils/*": ["utils/*"], "@custom_pipe/*": ["custom_pipe/*"], "@user/*": ["apps/api/src/user/*"], + "@community/*": ["apps/api/src/community/*"], "@api/*": ["apps/api/*"], "@mock/*": ["__mock__/*"], }