Skip to content

Commit

Permalink
feat: 커뮤니티에 사용자 추가 service layer 구현 #95
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon committed Nov 25, 2022
1 parent 7afa449 commit d624488
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
22 changes: 16 additions & 6 deletions server/apps/api/src/community/community.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: '커뮤니티 사용자 추가 완료' };
}
}
Original file line number Diff line number Diff line change
@@ -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[];
}
10 changes: 10 additions & 0 deletions server/dao/repository/community.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
1 change: 1 addition & 0 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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__/*"],
}
Expand Down

0 comments on commit d624488

Please sign in to comment.