Skip to content

Commit

Permalink
feat: 커뮤니티에 속한 사용자 추가 로직 추가 #95
Browse files Browse the repository at this point in the history
- 이미 커뮤니티 속해있는지 검증
- 사용자 검증 후 사용자 document update
- 사용자 document 내부 커뮤니티 schema 변경에 대한 수정
  • Loading branch information
NaayoungKwon committed Nov 24, 2022
1 parent 29f6de7 commit d3e32fa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
24 changes: 15 additions & 9 deletions server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,41 @@ export class CommunityService {
const communityId = appendUsersToCommunityDto.community_id;
const newCommunity = this.makeCommunityObj(communityId);
await Promise.all(
// 사용자 document 검증 (올바른 사용자인지, 해당 사용자가 이미 커뮤니티에 참여하고 있는건 아닌지)
appendUsersToCommunityDto.users.map(async (user_id) => {
const user = await this.userRepository.findById(user_id);
if (!user) {
throw new BadRequestException(
`커뮤니티에 추가를 요청한 사용자 _id(${user_id})가 올바르지 않습니다.`,
);
} else if (
(user.communities ?? false) &&
Array.from(user.communities.keys()).includes(communityId)
) {
throw new BadRequestException(`이미 커뮤니티에 추가된 사용자 입니다.`);
}
await this.userRepository.addArrAtArr(user_id, 'communities', [
appendUsersToCommunityDto.community_id,
]);
}),
);
await Promise.all(
// 사용자 document 검증이 끝난 후 update
appendUsersToCommunityDto.users.map(async (user_id) => {
await this.userRepository.updateObject({ _id: user_id }, newCommunity);
}),
);
const community = await this.communityRepository.addArrAtArr(
appendUsersToCommunityDto.community_id,
{ _id: appendUsersToCommunityDto.community_id },
'users',
appendUsersToCommunityDto.users,
);
if (!community) {
await Promise.all(
// 사용자 document에서 다시 삭제
appendUsersToCommunityDto.users.map((user_id) => {
this.userRepository.deleteElementAtArr(
{ _id: user_id },
{ communities: [appendUsersToCommunityDto.community_id] },
);
this.userRepository.deleteObject({ _id: user_id }, newCommunity);
}),
);
throw new BadRequestException('해당하는 커뮤니티의 _id가 올바르지 않습니다.');
}
return { message: '커뮤니티 사용자 추가 완료' };
}

async modifyCommunity(modifyCommunityDto: ModifyCommunityDto) {
Expand Down
5 changes: 3 additions & 2 deletions server/dao/repository/community.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ export class CommunityRepository {
return await this.communityModel.findById(_id);
}

async addArrAtArr(_id, attribute, appendArr) {
async addArrAtArr(filter, attribute, appendArr) {
const addArr = {};
addArr[attribute] = { $each: appendArr };
return await this.communityModel.findByIdAndUpdate(_id, { $addToSet: addArr }, { new: true });
return await this.communityModel.findOneAndUpdate(filter, { $addToSet: addArr }, { new: true });
// console.log('pass');
}

async findOne(condition: any) {
Expand Down
6 changes: 5 additions & 1 deletion server/dao/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export class UserRepository {
}

async updateObject(filter, appendElement) {
await this.userModel.updateOne(filter, { $set: appendElement });
return await this.userModel.updateOne(filter, { $set: appendElement });
}

async deleteObject(filter, appendElement) {
await this.userModel.updateOne(filter, { $unset: appendElement });
}

async deleteElementAtArr(filter, removeElement) {
Expand Down

0 comments on commit d3e32fa

Please sign in to comment.