Skip to content

Commit

Permalink
feat: 커뮤니티 수정 요청 사용자의 권한 확인 #95
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon committed Nov 25, 2022
1 parent 7c2ec5f commit 9a3c629
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
9 changes: 4 additions & 5 deletions server/apps/api/src/community/community.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ export class CommunityController {
@Req() req: any,
) {
try {
const _id = req.user._id;
await this.communityService.appendParticipantsToCommunity({
...appendUsersToCommunityDto,
requestUser_id: _id,
});
await this.communityService.appendParticipantsToCommunity(
req.user,
appendUsersToCommunityDto,
);
return responseForm(200, { message: '커뮤니티 사용자 추가 완료' });
} catch (error) {
this.logger.error(JSON.stringify(error.response));
Expand Down
14 changes: 9 additions & 5 deletions server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ModifyCommunityDto,
DeleteCommunityDto,
} from './dto';
import { IsUserInCommunity } from '@community/helper/checkUserIsInCommunity';

@Injectable()
export class CommunityService {
Expand All @@ -25,8 +26,14 @@ export class CommunityService {
return community;
}

async appendParticipantsToCommunity(appendUsersToCommunityDto: AppendUsersToCommunityDto) {
async appendParticipantsToCommunity(
reqUser,
appendUsersToCommunityDto: AppendUsersToCommunityDto,
) {
const communityId = appendUsersToCommunityDto.community_id;
if (!IsUserInCommunity(reqUser, communityId)) {
throw new BadRequestException(`커뮤니티에 속하지 않는 사용자는 요청할 수 없습니다.`);
}
const newCommunity = this.makeCommunityObj(communityId);
await Promise.all(
// 사용자 document 검증 (올바른 사용자인지, 해당 사용자가 이미 커뮤니티에 참여하고 있는건 아닌지)
Expand All @@ -36,10 +43,7 @@ export class CommunityService {
throw new BadRequestException(
`커뮤니티에 추가를 요청한 사용자 _id(${user_id})가 올바르지 않습니다.`,
);
} else if (
(user.communities ?? false) &&
Array.from(user.communities.keys()).includes(communityId)
) {
} else if (IsUserInCommunity(user, communityId)) {
throw new BadRequestException(`이미 커뮤니티에 추가된 사용자 입니다.`);
}
}),
Expand Down

0 comments on commit 9a3c629

Please sign in to comment.