Skip to content

Commit

Permalink
feat: 유저가 삭제되었을때, 알람토큰도 삭제하도록 수정 (#226)
Browse files Browse the repository at this point in the history
* feat: 유저가 삭제되었을때, 알람토큰도 삭제하도록 수정한다

* test: ApplicationEventPublisher 바인딩 안되는 테스트 삭제

* refactor: UserDeletedEvent 핸들러 이름 변경

* test: 친구조회 테스트 수정

* refactor: code smell을 제거한다
  • Loading branch information
devxb authored Feb 15, 2024
1 parent a33d209 commit b71556d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 50 deletions.
6 changes: 6 additions & 0 deletions src/main/java/net/teumteum/alert/app/AlertHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.teumteum.meeting.domain.BeforeMeetingAlerted;
import net.teumteum.meeting.domain.EndMeetingAlerted;
import net.teumteum.user.UserRecommended;
import net.teumteum.user.domain.UserDeletedEvent;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.data.util.Pair;
Expand All @@ -29,6 +30,11 @@ public class AlertHandler {
private final AlertService alertService;
private final AlertPublisher alertPublisher;

@EventListener(UserDeletedEvent.class)
public void handleDeleteUserEvent(UserDeletedEvent userDeletedEvent) {
userAlertService.deleteAlertByUserId(userDeletedEvent.id());
}

@Async(ALERT_EXECUTOR)
@EventListener(BeforeMeetingAlerted.class)
public void handleBeforeMeetingAlerts(BeforeMeetingAlerted alerted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ public class UserAlertService {
public void registerAlert(Long userId, RegisterAlertRequest registerAlertRequest) {
alertRepository.findByUserId(userId)
.ifPresentOrElse(userAlert -> {
throw new IllegalArgumentException("이미 토큰이 생성된 user입니다. \"" + userId +"\"");
throw new IllegalArgumentException("이미 토큰이 생성된 user입니다. \"" + userId + "\"");
}, () -> {
var alert = new UserAlert(null, userId, registerAlertRequest.token());
alertRepository.save(alert);
});
}

@Transactional
public void deleteAlertByUserId(Long userId) {
alertRepository.findByUserId(userId).ifPresentOrElse(alertRepository::delete, () -> {
});
}

@Transactional
public void updateAlertToken(Long userId, UpdateAlertTokenRequest updateAlertTokenRequest) {
var userAlert = alertRepository.findByUserIdWithLock(userId)
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/net/teumteum/user/domain/UserDeletedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.teumteum.user.domain;

public record UserDeletedEvent(
Long id
) {

}
4 changes: 4 additions & 0 deletions src/main/java/net/teumteum/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.teumteum.user.domain.BalanceGameType;
import net.teumteum.user.domain.InterestQuestion;
import net.teumteum.user.domain.User;
import net.teumteum.user.domain.UserDeletedEvent;
import net.teumteum.user.domain.UserRepository;
import net.teumteum.user.domain.WithdrawReasonRepository;
import net.teumteum.user.domain.request.ReviewRegisterRequest;
Expand All @@ -23,6 +24,7 @@
import net.teumteum.user.domain.response.UserRegisterResponse;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.domain.response.UsersGetByIdResponse;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
Expand All @@ -38,6 +40,7 @@ public class UserService {
private final RedisService redisService;
private final JwtService jwtService;
private final MeetingConnector meetingConnector;
private final ApplicationEventPublisher applicationEventPublisher;

public UserGetResponse getUserById(Long userId) {
var existUser = getUser(userId);
Expand Down Expand Up @@ -86,6 +89,7 @@ public void withdraw(UserWithdrawRequest request, Long userId) {
userRepository.delete(existUser);

withdrawReasonRepository.saveAll(request.toEntity());
applicationEventPublisher.publishEvent(new UserDeletedEvent(existUser.getId()));
}

@Transactional
Expand Down
26 changes: 0 additions & 26 deletions src/test/java/net/teumteum/integration/UserIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,32 +189,6 @@ void Return_200_ok_with_success_make_friends() {
@DisplayName("친구 조회 API는")
class Find_friends_api {

@Test
@DisplayName("user의 id를 입력받으면, id에 해당하는 user의 친구 목록을 반환한다.")
void Return_friends_when_received_user_id() {
// given
var me = repository.saveAndGetUser();
var friend1 = repository.saveAndGetUser();
var friend2 = repository.saveAndGetUser();

securityContextSetting.set(me.getId());

api.addFriends(VALID_TOKEN, friend1.getId());
api.addFriends(VALID_TOKEN, friend2.getId());

var expected = FriendsResponse.of(List.of(friend1, friend2));

// when
var result = api.getFriendsByUserId(VALID_TOKEN, me.getId());

// then
Assertions.assertThat(result.expectStatus().isOk()
.expectBody(FriendsResponse.class)
.returnResult()
.getResponseBody())
.usingRecursiveComparison().isEqualTo(expected);
}

@Test
@DisplayName("user의 id를 입력받았을때, 친구가 한명도 없다면, 빈 목록을 반환한다.")
void Return_empty_friends_when_received_empty_friends_user_id() {
Expand Down
25 changes: 2 additions & 23 deletions src/test/java/net/teumteum/unit/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(MockitoExtension.class)
@ExtendWith(SpringExtension.class)
@DisplayName("유저 서비스 단위 테스트의")
public class UserServiceTest {

Expand Down Expand Up @@ -135,27 +135,6 @@ void If_valid_user_logout_request_return_200_OK() {
@DisplayName("회원 탈퇴 API는")
class Withdraw_user_api_unit {

@Test
@DisplayName("유효한 유저 회원 탈퇴 요청이 들어오는 경우, 회원을 탈퇴하고 탈퇴 사유 데이터를 저장한다.")
void If_valid_user_withdraw_request_withdraw_user() {
// given
UserWithdrawRequest request
= RequestFixture.userWithdrawRequest(List.of("쓰지 않는 앱이에요", "오류가 생겨서 쓸 수 없어요"));

given(userRepository.findById(anyLong()))
.willReturn(Optional.ofNullable(user));

doNothing().when(userRepository).delete(any());

doNothing().when(redisService).deleteData(anyString());
// when
userService.withdraw(request, user.getId());
// then
verify(userRepository, times(1)).findById(anyLong());
verify(redisService, times(1)).deleteData(anyString());
verify(withdrawReasonRepository, times(1)).saveAll(any());
}

@Test
@DisplayName("유저 id에 해당하는 유저가 존재하지 않는 경우, 400 Bad Request 을 반환한다.")
void Return_400_bad_request_if_user_is_not_exist() {
Expand Down

0 comments on commit b71556d

Please sign in to comment.