Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 유저가 삭제되었을때, 알람토큰도 삭제하도록 수정 #226

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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;

Expand All @@ -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)
Expand Down
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
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
Loading