Skip to content

Commit

Permalink
refactor: 중복 로직 함수화 #95
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon authored and soomanbaek committed Nov 25, 2022
1 parent 431b5f6 commit f3d5a2e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
15 changes: 3 additions & 12 deletions server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ModifyCommunityDto,
DeleteCommunityDto,
} from './dto';
import { IsUserInCommunity } from '@community/helper/checkUserIsInCommunity';
import { IsUserInCommunity, makeCommunityObj } from '@community/helper';

@Injectable()
export class CommunityService {
Expand All @@ -21,7 +21,7 @@ export class CommunityService {
...createCommunityDto,
users: [createCommunityDto.managerId],
});
const newCommunity = this.makeCommunityObj(community._id.toString());
const newCommunity = makeCommunityObj(community._id.toString());
await this.userRepository.updateObject({ _id: createCommunityDto.managerId }, newCommunity);
return community;
}
Expand All @@ -34,7 +34,7 @@ export class CommunityService {
if (!IsUserInCommunity(reqUser, communityId)) {
throw new BadRequestException(`커뮤니티에 속하지 않는 사용자는 요청할 수 없습니다.`);
}
const newCommunity = this.makeCommunityObj(communityId);
const newCommunity = makeCommunityObj(communityId);
await Promise.all(
// 사용자 document 검증 (올바른 사용자인지, 해당 사용자가 이미 커뮤니티에 참여하고 있는건 아닌지)
appendUsersToCommunityDto.users.map(async (user_id) => {
Expand Down Expand Up @@ -121,13 +121,4 @@ export class CommunityService {
},
);
}

makeCommunityObj(community_id: string) {
const newCommunity = {};
newCommunity[`communities.${community_id}`] = {
_id: community_id,
channels: {},
};
return newCommunity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const IsUserInCommunity = (user, communityId) =>
(user.communities ?? false) && Array.from(user.communities.keys()).includes(communityId);
2 changes: 2 additions & 0 deletions server/apps/api/src/community/helper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './checkUserIsInCommunity';
export * from './makeCommunityObj';
8 changes: 8 additions & 0 deletions server/apps/api/src/community/helper/makeCommunityObj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const makeCommunityObj = (community_id: string) => {
const newCommunity = {};
newCommunity[`communities.${community_id}`] = {
_id: community_id,
channels: {},
};
return newCommunity;
};

0 comments on commit f3d5a2e

Please sign in to comment.