Skip to content

Commit

Permalink
feat: 커뮤니티 정보 전달 시 channel last Read 확인 #115
Browse files Browse the repository at this point in the history
  • Loading branch information
NaayoungKwon committed Nov 25, 2022
1 parent d13a577 commit 3bf7150
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
5 changes: 3 additions & 2 deletions server/apps/api/src/channel/channel.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { ChannelController } from './channel.controller';
import { ChannelService } from './channel.service';
import { MongooseModule } from '@nestjs/mongoose';
Expand All @@ -13,10 +13,11 @@ import { UserModule } from '@user/user.module';
@Module({
imports: [
MongooseModule.forFeature([{ name: Channel.name, schema: ChannelSchema }]),
CommunityModule,
forwardRef(() => CommunityModule),
UserModule,
],
controllers: [ChannelController],
providers: [ChannelService, ChannelRepository, CommunityRepository, UserRepository],
exports: [MongooseModule],
})
export class ChannelModule {}
7 changes: 5 additions & 2 deletions server/apps/api/src/community/community.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Module } from '@nestjs/common';
import { forwardRef, Module } from '@nestjs/common';
import { CommunityController } from './community.controller';
import { CommunityService } from './community.service';
import { MongooseModule } from '@nestjs/mongoose';
import { Community, CommunitySchema } from '@schemas/community.schema';
import { CommunityRepository } from '@repository/community.repository';
import { UserRepository } from '@repository/user.repository';
import { UserModule } from '@user/user.module';
import { ChannelModule } from '@api/src/channel/channel.module';
import { ChannelRepository } from '@repository/channel.repository';

@Module({
imports: [
MongooseModule.forFeature([{ name: Community.name, schema: CommunitySchema }]),
UserModule,
forwardRef(() => ChannelModule),
],
controllers: [CommunityController],
providers: [CommunityService, CommunityRepository, UserRepository],
providers: [CommunityService, CommunityRepository, UserRepository, ChannelRepository],
exports: [MongooseModule],
})
export class CommunityModule {}
27 changes: 12 additions & 15 deletions server/apps/api/src/community/community.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import {
} from './dto';
import { IsUserInCommunity, makeCommunityObj } from '@community/helper';
import { communityInUser } from '@user/dto/community-in-user.dto';
import { ChannelRepository } from '@repository/channel.repository';

@Injectable()
export class CommunityService {
constructor(
private readonly communityRepository: CommunityRepository,
private readonly userRepository: UserRepository,
private readonly channelRepository: ChannelRepository,
) {}

async getCommunities(user2) {
const user = await this.userRepository.findById('637f2abb146636e4082885b1');
async getCommunities(user) {
// const user = await this.userRepository.findById('637f2abb146636e4082885b1'); // 검증용
const infos = [];
await Promise.all(
Array.from(user.communities.values()).map(async (userCommunity) => {
Expand All @@ -34,27 +36,22 @@ export class CommunityService {
console.log(result);
throw new BadRequestException('커뮤니티에 없는 비정상적인 채널이 존재합니다.');
}
const info = {};
info[_id] = [];
const 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]);
const channel = (await this.channelRepository.findById(channelId)) as any;
if (!channel || channel.deletedAt) {
throw new BadRequestException('존재하지 않는 채널입니다.');
}
// TODO : channel document의 updatedAt 아니고 다르값 비교
info[_id].push({ [channelId]: lastRead.getTime() >= channel.updatedAt.getTime() });
}),
);
console.log(info);

infos.push(info);
}),
);
return infos;
return { communities: infos };
}
async createCommunity(createCommunityDto: CreateCommunityDto) {
const community = await this.communityRepository.create({
Expand Down
8 changes: 8 additions & 0 deletions server/dao/repository/channel.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ export class ChannelRepository {
async findOne(condition: any) {
return await this.channelModel.findOne(condition);
}

async findById(_id: string) {
return await this.channelModel.findById(_id);
}

async findOr(conditions: any) {
return await this.channelModel.find({ $or: conditions });
}
}

0 comments on commit 3bf7150

Please sign in to comment.