Skip to content

Commit

Permalink
Merge pull request #642 from tukcomCD2024/fix/group_invite_reject-#639
Browse files Browse the repository at this point in the history
fix : 그룹 초대 거절에서 그룹장 아이디 삭제 #639
  • Loading branch information
seokho-1116 authored Aug 21, 2024
2 parents c588511 + 4023afc commit 7900037
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,8 @@ ResponseEntity<ApiSpec<String>> deleteGroupInvite(
ResponseEntity<ApiSpec<String>> rejectGroupInvitation(
Long memberId,

Long groupId,

@Parameter(in = ParameterIn.PATH, description = "그룹 초대 대상 아이디", required = true)
Long groupOwnerId
@Parameter(in = ParameterIn.PATH, description = "그룹 아이디", required = true)
Long groupId
);

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public ResponseEntity<ApiSpec<String>> deleteGroupInvite(
);
}

@DeleteMapping(value = "/{group_id}/member/{target_id}/reject")
@DeleteMapping(value = "/{group_id}/reject")
public ResponseEntity<ApiSpec<String>> rejectGroupInvitation(
@AuthenticationPrincipal final Long memberId,
@PathVariable("group_id") final Long groupId,
@PathVariable("target_id") final Long targetId) {
@PathVariable("group_id") final Long groupId
) {

memberGroupCommandService.rejectRequestGroup(memberId, groupId, targetId);
memberGroupCommandService.rejectRequestGroup(memberId, groupId);

return ResponseEntity.ok(
ApiSpec.empty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ public interface GroupInviteRepository extends Repository<GroupInvite, Long>,

void delete(GroupInvite groupInvite);

@Query("delete from GroupInvite gi "
+ "where gi.group.id =:groupId "
+ "and gi.groupOwner.id =:groupOwnerId "
+ "and gi.groupMember.id =:groupMemberId"
)
@Modifying
int deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
@Param("groupId") Long groupId,
@Param("groupOwnerId") Long groupOwnerId,
@Param("groupMemberId") Long groupMemberId
);

@Query("delete from GroupInvite gi where gi.id in :groupInviteIds")
@Modifying
void bulkDelete(@Param("groupInviteIds") List<Long> groupInviteIds);
Expand All @@ -47,5 +35,7 @@ void deleteByGroupIds(
@Param("groupIds") List<Long> groupIds,
@Param("deletedAt") ZonedDateTime deletedAt
);

Optional<GroupInvite> findGroupInviteByGroupIdAndGroupMemberId(Long groupId, Long groupMemberId);
}

Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ public void inviteGroup(final Long memberId, final SendGroupRequest sendGroupReq
}

@Transactional
public void rejectRequestGroup(final Long memberId, final Long groupId, final Long targetId) {
final int isDenyRequest = groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, targetId, memberId);
public void rejectRequestGroup(final Long memberId, final Long groupId) {
GroupInvite groupInvite = groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(
groupId, memberId).orElseThrow(GroupInviteNotFoundException::new);

if (isDenyRequest != 1) {
throw new GroupInviteNotFoundException();
}
groupInviteRepository.delete(groupInvite);
}

@RedissonLock(value = "#groupId")
Expand All @@ -80,7 +78,10 @@ public GroupAcceptNotificationDto acceptGroupInvite(final Long memberId, final L
.orElseThrow(GroupNotFoundException::new);

if (totalGroupMemberCount == 30) {
deleteGroupInvite(memberId, groupId, groupOwnerId);
transactionTemplate.executeWithoutResult(
status -> findAndDeleteGroupInvite(memberId, groupId)
);

throw new GroupMemberCountLimitException();
}

Expand All @@ -90,28 +91,26 @@ public GroupAcceptNotificationDto acceptGroupInvite(final Long memberId, final L
.orElseThrow(GroupNotFoundException::new);

transactionTemplate.executeWithoutResult(status -> {
deleteGroupInvite(memberId, groupId, groupOwnerId);
findAndDeleteGroupInvite(memberId, groupId);
memberGroupRepository.save(MemberGroup.createGroupMember(groupMember, group));
});

return new GroupAcceptNotificationDto(groupMember.getNickname(), groupOwnerId);
}

private void deleteGroupInvite(final Long memberId, final Long groupId,
final Long groupOwnerId) {
final int isDenyRequest = groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, groupOwnerId, memberId);
if (isDenyRequest != 1) {
throw new GroupInviteNotFoundException();
}
private void findAndDeleteGroupInvite(final Long memberId, final Long groupId) {
final GroupInvite groupInvite = groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(
groupId, memberId)
.orElseThrow(GroupInviteNotFoundException::new);

groupInviteRepository.delete(groupInvite);
}

@Transactional
public void deleteGroupInvite(final Long memberId, final Long groupInviteId) {
final GroupInvite groupInvite = groupInviteRepository.findGroupInviteByIdAndGroupOwnerId(
groupInviteId,
memberId)
.orElseThrow(GroupInviteNotFoundException::new);
groupInviteId, memberId
).orElseThrow(GroupInviteNotFoundException::new);

groupInviteRepository.delete(groupInvite);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ public static Optional<GroupInvite> groupInvite(Group group, Member groupOwner,
Member groupMember) {
return Optional.of(GroupInvite.createOf(group, groupOwner, groupMember));
}

public static Optional<GroupInvite> groupInvite(Long memberId) {
return Optional.of(
GroupInvite.createOf(
GroupFixture.group(),
MemberFixture.memberWithMemberId(memberId),
MemberFixture.memberWithMemberId(memberId + 1)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,13 @@ class MemberGroupCommandServiceTest {
//given
Long memberId = 1L;
Long groupId = 1L;
Long targetId = 2L;

given(groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, targetId, memberId)).willReturn(1);
given(groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(
groupId, memberId)).willReturn(GroupInviteFixture.groupInvite(memberId));

//when
// then
assertThatCode(
() -> groupMemberCommandService.rejectRequestGroup(memberId, groupId, targetId))
assertThatCode(() -> groupMemberCommandService.rejectRequestGroup(memberId, groupId))
.doesNotThrowAnyException();
}

Expand All @@ -165,15 +163,14 @@ class MemberGroupCommandServiceTest {
//given
Long memberId = 1L;
Long groupId = 1L;
Long targetId = 2L;

given(groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, targetId, memberId)).willReturn(0);
given(groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(
groupId, memberId)).willReturn(Optional.empty());

//when
// then
assertThatThrownBy(
() -> groupMemberCommandService.rejectRequestGroup(memberId, groupId, targetId))
() -> groupMemberCommandService.rejectRequestGroup(memberId, groupId))
.isInstanceOf(GroupInviteNotFoundException.class)
.hasMessageContaining(ErrorCode.GROUP_INVITATION_NOT_FOUND_ERROR.getMessage());
}
Expand All @@ -191,8 +188,8 @@ class MemberGroupCommandServiceTest {
Optional.of(GroupFixture.group()));
given(memberGroupRepository.findGroupOwnerId(groupId)).willReturn(Optional.of(2L));

given(groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, 2L, memberId)).willReturn(1);
given(groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(groupId, memberId))
.willReturn(GroupInviteFixture.groupInvite(memberId));

//when
groupMemberFacade.acceptGroupInvite(memberId, groupId);
Expand All @@ -216,8 +213,8 @@ class MemberGroupCommandServiceTest {
given(memberGroupRepository.findGroupOwnerId(groupId)).willReturn(
Optional.of(groupOwnerId));

given(groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, groupOwnerId, memberId)).willReturn(1);
given(groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(groupId, memberId))
.willReturn(GroupInviteFixture.groupInvite(memberId));

//when
GroupAcceptNotificationDto groupAcceptNotificationDto = groupMemberCommandService.acceptGroupInvite(
Expand Down Expand Up @@ -246,8 +243,8 @@ class MemberGroupCommandServiceTest {
Optional.of(GroupFixture.group()));
given(memberGroupRepository.findGroupOwnerId(groupId)).willReturn(Optional.of(2L));

given(groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, 2L, memberId)).willReturn(0);
given(groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(
groupId, memberId)).willReturn(Optional.empty());

//when
//then
Expand All @@ -265,8 +262,8 @@ class MemberGroupCommandServiceTest {

given(groupRepository.getTotalGroupMemberCount(groupId)).willReturn(Optional.of(30L));
given(memberGroupRepository.findGroupOwnerId(groupId)).willReturn(Optional.of(2L));
given(groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, 2L, memberId)).willReturn(1);
given(groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(groupId, memberId))
.willReturn(GroupInviteFixture.groupInvite(memberId));

assertThatThrownBy(
() -> groupMemberCommandService.acceptGroupInvite(memberId, groupId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock;
Expand All @@ -22,10 +21,12 @@
import site.timecapsulearchive.core.common.RedissonTest;
import site.timecapsulearchive.core.common.dependency.TestTransactionTemplate;
import site.timecapsulearchive.core.common.fixture.domain.GroupFixture;
import site.timecapsulearchive.core.common.fixture.domain.GroupInviteFixture;
import site.timecapsulearchive.core.common.fixture.domain.MemberFixture;
import site.timecapsulearchive.core.domain.group.repository.GroupRepository;
import site.timecapsulearchive.core.domain.member.entity.Member;
import site.timecapsulearchive.core.domain.member.repository.MemberRepository;
import site.timecapsulearchive.core.domain.member_group.entity.GroupInvite;
import site.timecapsulearchive.core.domain.member_group.facade.MemberGroupFacade;
import site.timecapsulearchive.core.domain.member_group.repository.group_invite_repository.GroupInviteRepository;
import site.timecapsulearchive.core.domain.member_group.repository.member_group_repository.MemberGroupRepository;
Expand Down Expand Up @@ -81,9 +82,8 @@ class RedisConcurrencyTest extends RedissonTest {
Optional.of(GroupFixture.group()));
given(memberGroupRepository.findGroupOwnerId(groupId))
.willReturn(Optional.of(2L));

given(groupInviteRepository.deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
groupId, 2L, memberId)).willReturn(1);
given(groupInviteRepository.findGroupInviteByGroupIdAndGroupMemberId(groupId, memberId))
.willReturn(GroupInviteFixture.groupInvite(memberId));

//when
for (int i = 0; i < MAX_THREADS_COUNT; i++) {
Expand All @@ -102,9 +102,6 @@ class RedisConcurrencyTest extends RedissonTest {
executorService.shutdown();

//then
verify(groupInviteRepository,
times(MAX_THREADS_COUNT)).deleteGroupInviteByGroupIdAndGroupOwnerIdAndGroupMemberId(
anyLong(), anyLong(), anyLong());
verify(memberGroupRepository, times(MAX_THREADS_COUNT)).save(any());
}

Expand Down

0 comments on commit 7900037

Please sign in to comment.