Skip to content

Commit

Permalink
feat: 커뮤니티 사용자 추가 시 사용자 document update #95
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon committed Nov 25, 2022
1 parent 121fed2 commit 2883069
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ export class CommunityService {
async appendParticipantsToCommunity(appendUsersToCommunityDto: AppendUsersToCommunityDto) {
await Promise.all(
appendUsersToCommunityDto.users.map(async (user_id) => {
const user = this.userRepository.findById(user_id);
const user = await this.userRepository.findById(user_id);
if (!user) {
throw new BadRequestException(
`커뮤니티에 추가를 요청한 사용자 _id(${user_id})가 올바르지 않습니다.`,
);
}
await this.userRepository.addArrAtArr({ _id: user_id }, 'communities', [
appendUsersToCommunityDto.community_id,
]);
}),
);
const community = await this.communityRepository.addArrAtArr(
Expand All @@ -35,6 +38,14 @@ export class CommunityService {
appendUsersToCommunityDto.users,
);
if (!community) {
await Promise.all(
appendUsersToCommunityDto.users.map((user_id) => {
this.userRepository.deleteElementAtArr(
{ _id: user_id },
{ communities: [appendUsersToCommunityDto.community_id] },
);
}),
);
throw new BadRequestException('해당하는 커뮤니티의 _id가 올바르지 않습니다.');
}
return { message: '커뮤니티 사용자 추가 완료' };
Expand Down
6 changes: 6 additions & 0 deletions server/dao/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ export class UserRepository {
async deleteElementAtArr(filter, removeElement) {
await this.userModel.updateOne(filter, { $pullAll: removeElement });
}

async addArrAtArr(filter, attribute, appendArr) {
const addArr = {};
addArr[attribute] = { $each: appendArr };
return await this.userModel.findOneAndUpdate(filter, { $addToSet: addArr }, { new: true });
}
}

0 comments on commit 2883069

Please sign in to comment.