diff --git a/src/main/java/org/sopt/app/application/poke/PokeHistoryService.java b/src/main/java/org/sopt/app/application/poke/PokeHistoryService.java index 24bebe8c..872c1431 100755 --- a/src/main/java/org/sopt/app/application/poke/PokeHistoryService.java +++ b/src/main/java/org/sopt/app/application/poke/PokeHistoryService.java @@ -1,16 +1,14 @@ package org.sopt.app.application.poke; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import lombok.RequiredArgsConstructor; -import lombok.val; +import java.util.*; +import lombok.*; import org.sopt.app.application.poke.PokeInfo.PokeHistoryInfo; +import org.sopt.app.application.user.UserWithdrawEvent; import org.sopt.app.common.exception.BadRequestException; import org.sopt.app.common.response.ErrorCode; import org.sopt.app.domain.entity.poke.PokeHistory; import org.sopt.app.interfaces.postgres.PokeHistoryRepository; +import org.springframework.context.event.EventListener; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -80,4 +78,10 @@ public List getAllPokeHistoryByUsers(Long userId, Long friendUs val pokeHistories = pokeHistoryRepository.findAllPokeHistoryByUsers(userId, friendUserId); return pokeHistories.stream().map(PokeHistoryInfo::from).toList(); } + + @EventListener(UserWithdrawEvent.class) + public void handleUserWithdrawEvent(final UserWithdrawEvent event) { + pokeHistoryRepository.deleteAllByPokerIdInQuery(event.getUserId()); + pokeHistoryRepository.deleteAllByPokedIdInQuery(event.getUserId()); + } } diff --git a/src/main/java/org/sopt/app/interfaces/postgres/PokeHistoryRepository.java b/src/main/java/org/sopt/app/interfaces/postgres/PokeHistoryRepository.java index fd7dd96f..b9ecd6b8 100755 --- a/src/main/java/org/sopt/app/interfaces/postgres/PokeHistoryRepository.java +++ b/src/main/java/org/sopt/app/interfaces/postgres/PokeHistoryRepository.java @@ -22,6 +22,12 @@ public interface PokeHistoryRepository extends JpaRepository Page findAllByIdIsInOrderByCreatedAtDesc(List historyIds, Pageable pageable); + @Query("DELETE From PokeHistory ph WHERE ph.pokedId = :userId") + void deleteAllByPokedIdInQuery(@Param("userId") Long userId); + + @Query("DELETE From PokeHistory ph WHERE ph.pokerId = :userId") + void deleteAllByPokerIdInQuery(@Param("userId") Long userId); + @Query("SELECT ph FROM PokeHistory ph WHERE ((ph.pokerId = :userId AND ph.pokedId = :friendId) OR (ph.pokerId = :friendId AND ph.pokedId = :userId)) AND ph.isReply = false ORDER BY ph.createdAt DESC ") List findAllWithFriendOrderByCreatedAtDescIsReplyFalse(@Param("userId") Long userId, @Param("friendId") Long friendId);