Skip to content

Commit

Permalink
feat: add API to invite user to channel (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiwon-Woo committed Dec 12, 2022
1 parent 98137a3 commit 44c9f0e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
14 changes: 14 additions & 0 deletions backend/src/api/channel/channel.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,18 @@ export class ChannelController {
changeRole,
);
}

@Post(':channelId/invite/:targetId')
@ApiOperation({ summary: '특정 채널에 사용자 초대하기' })
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiBody({ type: ChangeRoleInChannelDto })
inviteUser(
@Req() req,
@Param('channelId') channelId: string,
@Param('targetId') targetId: string,
) {
const userId = req.user.id;
return this.channelService.inviteUser(userId, channelId, targetId);
}
}
54 changes: 48 additions & 6 deletions backend/src/api/channel/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,28 @@ export class ChannelService {
if (!channel) {
throw new BadRequestException('존재하지 않는 채널');
}
const joinChannels = await this.channelMemberRepository.findChannelHaveJoin(
userId,
channelId,
);
const joinChannels =
await this.channelMemberRepository.findChannelHaveJoinOrInvite(
userId,
channelId,
);
if (
joinChannels &&
(joinChannels.roleInChannel === RoleInChannel.BLOCK ||
joinChannels.banEndAt >= new Date())
) {
throw new BadRequestException('채널로부터 차단 당했습니다');
}
if (joinChannels && !joinChannels.leftAt) {
if (joinChannels && joinChannels.joinAt && !joinChannels.leftAt) {
throw new BadRequestException('이미 참여한 채널');
}
// 비밀번호 암호화 검증 추가
if (channel.password && channel.password !== channelPassword.password) {
throw new BadRequestException('비밀번호가 틀렸습니다');
}
if (joinChannels && joinChannels.leftAt) {
if (joinChannels) {
await this.channelMemberRepository.update(joinChannels.id, {
joinAt: () => 'CURRENT_TIMESTAMP',
leftAt: null,
});
return joinChannels;
Expand Down Expand Up @@ -355,4 +357,44 @@ export class ChannelService {
});
return { success: true };
}

async inviteUser(userId: string, channelId: string, targetId: string) {
const target = await this.userRepository.findOneBy({ id: targetId });
const channel = await this.channelRepository.findOneBy({ id: channelId });
if (!target && !channel) {
throw new BadRequestException(
'존재하지 않는 채널이거나 존재하지 않는 사용자',
);
}
const joinChannels =
await this.channelMemberRepository.findChannelHaveJoinOrInvite(
targetId,
channelId,
);
if (
joinChannels &&
(joinChannels.roleInChannel === RoleInChannel.BLOCK ||
joinChannels.banEndAt >= new Date())
) {
throw new BadRequestException('채널로부터 차단 당했습니다');
}
if (joinChannels && (!joinChannels.leftAt || !joinChannels.joinAt)) {
throw new BadRequestException(
'이미 사용자가 채널에 있거나 초대된 상태입니다.',
);
}
if (joinChannels) {
await this.channelMemberRepository.update(joinChannels.id, {
joinAt: null,
});
return joinChannels;
}
const channelMember = this.channelMemberRepository.create({
userId: target,
channelId: channel,
joinAt: null,
});
await this.channelMemberRepository.save(channelMember);
return channelMember;
}
}
8 changes: 7 additions & 1 deletion backend/src/core/channel/channel-member.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class ChannelMemberRepository extends Repository<ChannelMember> {
relations: ['userId', 'channelId'],
where: {
userId: { id: userId },
joinAt: Not(IsNull()),
leftAt: IsNull(),
channelId: { deletedAt: IsNull() },
},
Expand All @@ -23,6 +24,7 @@ export class ChannelMemberRepository extends Repository<ChannelMember> {
relations: ['userId', 'channelId'],
where: {
userId: { id: userId },
joinAt: Not(IsNull()),
leftAt: IsNull(),
channelId: { id: channelId, deletedAt: IsNull() },
},
Expand All @@ -34,6 +36,7 @@ export class ChannelMemberRepository extends Repository<ChannelMember> {
relations: ['userId', 'channelId'],
where: {
userId: { id: userId },
joinAt: Not(IsNull()),
leftAt: IsNull(),
banEndAt: IsNull() || LessThan(new Date()),
roleInChannel: RoleInChannel.OWNER || RoleInChannel.ADMINISTRATOR,
Expand All @@ -42,7 +45,7 @@ export class ChannelMemberRepository extends Repository<ChannelMember> {
});
}

async findChannelHaveJoin(userId: string, channelId: string) {
async findChannelHaveJoinOrInvite(userId: string, channelId: string) {
return await this.findOne({
relations: ['userId', 'channelId'],
where: {
Expand All @@ -60,6 +63,7 @@ export class ChannelMemberRepository extends Repository<ChannelMember> {
},
where: {
leftAt: IsNull(),
joinAt: Not(IsNull()),
banEndAt: IsNull() || LessThan(new Date()),
roleInChannel: RoleInChannel.ADMINISTRATOR,
channelId: { id: channelId, deletedAt: IsNull() },
Expand All @@ -75,6 +79,7 @@ export class ChannelMemberRepository extends Repository<ChannelMember> {
},
where: {
leftAt: IsNull(),
joinAt: Not(IsNull()),
banEndAt: IsNull() || LessThan(new Date()),
roleInChannel: Not(RoleInChannel.OWNER),
channelId: { id: channelId, deletedAt: IsNull() },
Expand All @@ -87,6 +92,7 @@ export class ChannelMemberRepository extends Repository<ChannelMember> {
relations: ['userId', 'channelId'],
where: {
leftAt: IsNull(),
joinAt: Not(IsNull()),
channelId: { id: channelId },
},
});
Expand Down

0 comments on commit 44c9f0e

Please sign in to comment.