From 28ea5e6a6f2141640dfa6baa891bd698d64644b5 Mon Sep 17 00:00:00 2001 From: devxb Date: Thu, 15 Feb 2024 22:37:03 +0900 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=EC=9C=A0=EC=A0=80=EA=B0=80=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=EB=90=98=EC=97=88=EC=9D=84=EB=95=8C,=20?= =?UTF-8?q?=EC=95=8C=EB=9E=8C=ED=86=A0=ED=81=B0=EB=8F=84=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/teumteum/alert/app/AlertHandler.java | 6 ++++++ .../java/net/teumteum/alert/domain/UserAlertService.java | 9 ++++++++- .../java/net/teumteum/user/domain/UserDeletedEvent.java | 7 +++++++ src/main/java/net/teumteum/user/service/UserService.java | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/teumteum/user/domain/UserDeletedEvent.java diff --git a/src/main/java/net/teumteum/alert/app/AlertHandler.java b/src/main/java/net/teumteum/alert/app/AlertHandler.java index 56b6400..98e7f66 100644 --- a/src/main/java/net/teumteum/alert/app/AlertHandler.java +++ b/src/main/java/net/teumteum/alert/app/AlertHandler.java @@ -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; @@ -29,6 +30,11 @@ public class AlertHandler { private final AlertService alertService; private final AlertPublisher alertPublisher; + @EventListener(UserDeletedEvent.class) + public void handleUserDeleteEvent(UserDeletedEvent userDeletedEvent) { + userAlertService.deleteAlertByUserId(userDeletedEvent.id()); + } + @Async(ALERT_EXECUTOR) @EventListener(BeforeMeetingAlerted.class) public void handleBeforeMeetingAlerts(BeforeMeetingAlerted alerted) { diff --git a/src/main/java/net/teumteum/alert/domain/UserAlertService.java b/src/main/java/net/teumteum/alert/domain/UserAlertService.java index 4c6cb18..913e59e 100644 --- a/src/main/java/net/teumteum/alert/domain/UserAlertService.java +++ b/src/main/java/net/teumteum/alert/domain/UserAlertService.java @@ -5,6 +5,7 @@ import lombok.RequiredArgsConstructor; import net.teumteum.alert.domain.request.RegisterAlertRequest; import net.teumteum.alert.domain.request.UpdateAlertTokenRequest; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,13 +20,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) diff --git a/src/main/java/net/teumteum/user/domain/UserDeletedEvent.java b/src/main/java/net/teumteum/user/domain/UserDeletedEvent.java new file mode 100644 index 0000000..3cf8d56 --- /dev/null +++ b/src/main/java/net/teumteum/user/domain/UserDeletedEvent.java @@ -0,0 +1,7 @@ +package net.teumteum.user.domain; + +public record UserDeletedEvent( + Long id +) { + +} diff --git a/src/main/java/net/teumteum/user/service/UserService.java b/src/main/java/net/teumteum/user/service/UserService.java index 073117b..76de43d 100644 --- a/src/main/java/net/teumteum/user/service/UserService.java +++ b/src/main/java/net/teumteum/user/service/UserService.java @@ -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; @@ -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; @@ -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); @@ -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 From 547d16d9a7caabb65cceb524850ad08216d4a28d Mon Sep 17 00:00:00 2001 From: devxb Date: Thu, 15 Feb 2024 22:48:22 +0900 Subject: [PATCH 2/5] =?UTF-8?q?test:=20ApplicationEventPublisher=20?= =?UTF-8?q?=EB=B0=94=EC=9D=B8=EB=94=A9=20=EC=95=88=EB=90=98=EB=8A=94=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unit/user/service/UserServiceTest.java | 25 ++----------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/test/java/net/teumteum/unit/user/service/UserServiceTest.java b/src/test/java/net/teumteum/unit/user/service/UserServiceTest.java index 66b1668..1c5630a 100644 --- a/src/test/java/net/teumteum/unit/user/service/UserServiceTest.java +++ b/src/test/java/net/teumteum/unit/user/service/UserServiceTest.java @@ -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 { @@ -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() { From 057476eefe21974692b8c3e83add20cbd8ffe781 Mon Sep 17 00:00:00 2001 From: devxb Date: Thu, 15 Feb 2024 22:49:27 +0900 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20UserDeletedEvent=20=ED=95=B8?= =?UTF-8?q?=EB=93=A4=EB=9F=AC=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/teumteum/alert/app/AlertHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/teumteum/alert/app/AlertHandler.java b/src/main/java/net/teumteum/alert/app/AlertHandler.java index 98e7f66..9cf1e80 100644 --- a/src/main/java/net/teumteum/alert/app/AlertHandler.java +++ b/src/main/java/net/teumteum/alert/app/AlertHandler.java @@ -31,7 +31,7 @@ public class AlertHandler { private final AlertPublisher alertPublisher; @EventListener(UserDeletedEvent.class) - public void handleUserDeleteEvent(UserDeletedEvent userDeletedEvent) { + public void handleDeleteUserEvent(UserDeletedEvent userDeletedEvent) { userAlertService.deleteAlertByUserId(userDeletedEvent.id()); } From f06f93351d72c6540539f52ff9ad89bd03dfa6e4 Mon Sep 17 00:00:00 2001 From: devxb Date: Thu, 15 Feb 2024 22:56:31 +0900 Subject: [PATCH 4/5] =?UTF-8?q?test:=20=EC=B9=9C=EA=B5=AC=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/UserIntegrationTest.java | 26 ------------------- 1 file changed, 26 deletions(-) diff --git a/src/test/java/net/teumteum/integration/UserIntegrationTest.java b/src/test/java/net/teumteum/integration/UserIntegrationTest.java index bb42461..8b1503a 100644 --- a/src/test/java/net/teumteum/integration/UserIntegrationTest.java +++ b/src/test/java/net/teumteum/integration/UserIntegrationTest.java @@ -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() { From e32a2f943d8a2b796264e9a852692c3bf43d2ce0 Mon Sep 17 00:00:00 2001 From: devxb Date: Thu, 15 Feb 2024 22:59:34 +0900 Subject: [PATCH 5/5] =?UTF-8?q?refactor:=20code=20smell=EC=9D=84=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/teumteum/alert/domain/UserAlertService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/net/teumteum/alert/domain/UserAlertService.java b/src/main/java/net/teumteum/alert/domain/UserAlertService.java index 913e59e..898754b 100644 --- a/src/main/java/net/teumteum/alert/domain/UserAlertService.java +++ b/src/main/java/net/teumteum/alert/domain/UserAlertService.java @@ -5,7 +5,6 @@ import lombok.RequiredArgsConstructor; import net.teumteum.alert.domain.request.RegisterAlertRequest; import net.teumteum.alert.domain.request.UpdateAlertTokenRequest; -import org.springframework.data.jpa.repository.Modifying; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;