Skip to content

Commit

Permalink
[FEATURE] 탈퇴 유저 정보 삭제 스케쥴링 구현 (#152)
Browse files Browse the repository at this point in the history
* feat : 탈퇴 유저 정보 삭제 스케쥴링 구현 #147

* feat : 탈퇴 유저 정보 삭제 스케쥴링 구현 #147

* spotless apply #147
  • Loading branch information
bongsh0112 authored Jan 20, 2024
1 parent 9503786 commit 6d79596
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Api/src/main/java/tify/server/TifyApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EntityScan("tify.server.domain")
@RequiredArgsConstructor
@SpringBootApplication
@EnableScheduling
@EnableAsync
@Slf4j
public class TifyApiApplication implements ApplicationListener<ApplicationReadyEvent> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface AnswerReportRepository extends JpaRepository<AnswerReport, Long> {

Optional<AnswerReport> findByAnswerIdAndReportUserId(Long answerId, Long reportUserId);

void deleteAllByReportUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface AnswerRepository extends JpaRepository<Answer, Long>, AnswerCus
Optional<Answer> findByQuestionIdAndUserId(Long questionId, Long userId);

Long countAllByUserId(Long userId);

void deleteAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ boolean existsByFavorQuestion_FavorQuestionCategoryAndUserId(

boolean existsByFavorQuestion_FavorQuestionCategory_DetailCategoryAndUserId(
DetailCategory detailCategory, Long userId);

void deleteAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ List<Knock> findAllByDailyQuestionIdAndUserIdAndKnockedUserId(
List<Knock> findDistinctByDailyQuestionIdAndKnockedUserId(Long questionId, Long knockedUserId);

List<Knock> findAllByKnockedUserIdAndDailyQuestionId(Long knockedUserId, Long dailyQuestionId);

void deleteAllByUserIdOrKnockedUserId(Long userId, Long knockedUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface NeighborRepository
List<Neighbor> findAllByFromUserIdAndIsView(Long fromUserId, boolean isView);

List<Neighbor> findAllByFromUserIdOrderByOrder(Long fromUserId);

void deleteAllByFromUserIdOrToUserId(Long fromUserId, Long toUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

public interface UserBlockRepository extends JpaRepository<UserBlock, Long> {

public Optional<UserBlock> findByFromUserIdAndToUserId(Long fromUserId, Long toUserId);
Optional<UserBlock> findByFromUserIdAndToUserId(Long fromUserId, Long toUserId);

public boolean existsByFromUserIdAndToUserId(Long fromUserId, Long toUserId);
boolean existsByFromUserIdAndToUserId(Long fromUserId, Long toUserId);

public List<UserBlock> findAllByFromUserId(Long fromUserId);
List<UserBlock> findAllByFromUserId(Long fromUserId);

void deleteAllByFromUserIdOrToUserId(Long fromUserId, Long toUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface UserReportRepository extends JpaRepository<UserReport, Long> {
List<UserReport> findAllByToUserId(Long userId);

Optional<UserReport> findByFromUserIdAndToUserId(Long fromUserId, Long toUserId);

void deleteAllByFromUserIdOrToUserId(Long fromUserId, Long toUserId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tify.server.domain.domains.user.service;


import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import tify.server.core.annotation.DomainService;
Expand Down Expand Up @@ -63,4 +66,24 @@ public boolean userCanRegister(OauthInfo oauthInfo) {
return !userAdaptor.existByOauthInfo(oauthInfo)
|| userResignAdaptor.existByOauthInfo(oauthInfo);
}

public List<User> getResignedUsers() {
return userResignAdaptor.queryAll().stream()
.map(
resign -> {
LocalDateTime resignedTime =
resign.getCreatedAt()
.toLocalDateTime()
.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
if (resignedTime.isEqual(LocalDateTime.now().minusMonths(6))) {
return userAdaptor.query(resign.getUserId());
}
return null;
})
.filter(Objects::nonNull)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tify.server.domain.scheduler.user;


import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tify.server.domain.domains.question.repository.AnswerReportRepository;
import tify.server.domain.domains.question.repository.AnswerRepository;
import tify.server.domain.domains.question.repository.FavorAnswerRepository;
import tify.server.domain.domains.question.repository.KnockRepository;
import tify.server.domain.domains.user.domain.User;
import tify.server.domain.domains.user.repository.NeighborRepository;
import tify.server.domain.domains.user.repository.UserBlockRepository;
import tify.server.domain.domains.user.repository.UserReportRepository;
import tify.server.domain.domains.user.repository.UserRepository;
import tify.server.domain.domains.user.service.UserDomainService;

@Component
@RequiredArgsConstructor
public class UserResignScheduler {

private final UserDomainService userDomainService;

private final UserRepository userRepository;
private final AnswerRepository answerRepository;
private final AnswerReportRepository answerReportRepository;
private final FavorAnswerRepository favorAnswerRepository;
private final KnockRepository knockRepository;
private final NeighborRepository neighborRepository;
private final UserBlockRepository userBlockRepository;
private final UserReportRepository userReportRepository;

@Transactional
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
public void deleteResignedUsers() {
List<User> resignedUserList = userDomainService.getResignedUsers();
resignedUserList.forEach(
user -> {
answerRepository.deleteAllByUserId(user.getId());
answerReportRepository.deleteAllByReportUserId(user.getId());
favorAnswerRepository.deleteAllByUserId(user.getId());
knockRepository.deleteAllByUserIdOrKnockedUserId(user.getId(), user.getId());
neighborRepository.deleteAllByFromUserIdOrToUserId(user.getId(), user.getId());
userBlockRepository.deleteAllByFromUserIdOrToUserId(user.getId(), user.getId());
userReportRepository.deleteAllByFromUserIdOrToUserId(
user.getId(), user.getId());
userRepository.delete(user);
});
}
}

0 comments on commit 6d79596

Please sign in to comment.