Skip to content

Commit

Permalink
feat: 자신의 커뮤니티 받아오기 service layer 1차 #115
Browse files Browse the repository at this point in the history
- 사용자 document에서 커뮤니티 정보를 받아옴
- 커뮤니티 id 별로 실제로 존재하는지 검증
- 사용자 document의 채널들이 각 커뮤니티에 속한 채널인지 확인
- 채널 별 유효한 채널인지 검증
- 최근 접근 시간과 마지막 message 전송 시간 비교 logic 구현
  • Loading branch information
NaayoungKwon committed Nov 24, 2022
1 parent 150589a commit b71bc3e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
40 changes: 40 additions & 0 deletions server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DeleteCommunityDto,
} from './dto';
import { IsUserInCommunity, makeCommunityObj } from '@community/helper';
import { communityInUser } from '@user/dto/community-in-user.dto';

@Injectable()
export class CommunityService {
Expand All @@ -16,6 +17,45 @@ export class CommunityService {
private readonly userRepository: UserRepository,
) {}

async getCommunities(user2) {
const user = await this.userRepository.findById('637f2abb146636e4082885b1');
const infos = [];
await Promise.all(
Array.from(user.communities.values()).map(async (userCommunity) => {
const { _id, channels } = userCommunity as communityInUser;
const community = await this.communityRepository.findById(_id);
if (!community) {
throw new BadRequestException('해당하는 커뮤니티의 _id가 올바르지 않습니다.');
}
const result = Array.from(channels.keys()).filter(
(channel_id: string) => !community.channels.includes(channel_id),
);
if (result.length > 0) {
console.log(result);
throw new BadRequestException('커뮤니티에 없는 비정상적인 채널이 존재합니다.');
}
const info = {};
info[_id] = [];
await Promise.all(
Array.from(channels.keys()).map(async (channelId) => {
const lastRead = channels.get(channelId);
console.log(lastRead);
// TODO: soft delete이면 조건 다시 설정
// const channel = await this.channelRepository.findById(channelId);
// if (!channel) {
// throw new BadRequestException('존재하지 않는 채널입니다.');
// }
// info[_id][channelId] = lastRead < channel.updatedAt;
// console.log(info[_id]);
}),
);
console.log(info);

infos.push(info);
}),
);
return infos;
}
async createCommunity(createCommunityDto: CreateCommunityDto) {
const community = await this.communityRepository.create({
...createCommunityDto,
Expand Down
4 changes: 4 additions & 0 deletions server/apps/api/src/user/dto/community-in-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface communityInUser {
_id: string;
channels?: Map<string, Date>;
}

0 comments on commit b71bc3e

Please sign in to comment.