Skip to content

Commit

Permalink
[merge] 유저 탈퇴 시, 관련 데이터 삭제 - #176
Browse files Browse the repository at this point in the history
  • Loading branch information
sjk4618 authored Jul 18, 2024
2 parents a07e740 + 2280496 commit 8d96158
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package org.dateroad.date.service;

import org.dateroad.date.domain.Date;
import org.dateroad.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

import java.time.LocalTime;
import java.util.Optional;

@Repository
public interface DateRepository extends JpaRepository<Date, Long> {
Optional<Date> findFirstByUserIdAndDateAfterOrDateAndStartAtAfterOrderByDateAscStartAtAsc(
Long userId, LocalDate currentDate, LocalDate sameDay, LocalTime currentTime);
Expand All @@ -21,6 +25,10 @@ Optional<Date> findFirstByUserIdAndDateAfterOrDateAndStartAtAfterOrderByDateAscS
@Query("select d from Date d where d.user.id = :userId and d.date >= :currentDate order by d.date asc")
List<Date> findFutureDatesByUserId(@Param("userId") Long userId, @Param("currentDate") LocalDate currentDate);

@Modifying
@Query("DELETE FROM UserTag ut WHERE ut.user.id = :userId")
void deleteAllByUserId(@Param("userId") Long userId);

}


Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import org.dateroad.auth.jwt.JwtProvider;
import org.dateroad.auth.jwt.Token;
import org.dateroad.code.FailureCode;
import org.dateroad.date.repository.CourseRepository;
import org.dateroad.date.service.DateRepository;
import org.dateroad.dateAccess.repository.DateAccessRepository;
import org.dateroad.exception.*;
import org.dateroad.feign.apple.AppleFeignProvider;
import org.dateroad.feign.kakao.KakaoFeignProvider;
import org.dateroad.exception.ConflictException;
import org.dateroad.exception.EntityNotFoundException;
import org.dateroad.exception.UnauthorizedException;
import org.dateroad.like.repository.LikeRepository;
import org.dateroad.point.repository.PointRepository;
import org.dateroad.refreshtoken.domain.RefreshToken;
import org.dateroad.refreshtoken.repository.RefreshTokenRepository;
import org.dateroad.s3.S3Service;
import org.dateroad.tag.domain.DateTagType;
import org.dateroad.tag.domain.UserTag;
import org.dateroad.tag.repository.UserTagRepository;
Expand All @@ -25,7 +29,6 @@
import org.dateroad.user.repository.UserRepository;
import org.dateroad.user.dto.request.UserSignUpReq;
import org.dateroad.user.dto.response.UserJwtInfoRes;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -47,6 +50,11 @@ public class AuthService {
private final JwtProvider jwtProvider;
private final RefreshTokenRepository refreshTokenRepository;
private final UserService userService;
private final PointRepository pointRepository;
private final DateRepository dateRepository;
private final DateAccessRepository dateAccessRepository;
private final CourseRepository courseRepository;
private final LikeRepository likeRepository;

@Transactional
public UserJwtInfoRes signUp(final String token, final UserSignUpReq userSignUpReq, @Nullable final MultipartFile image, final List<DateTagType> tag) {
Expand Down Expand Up @@ -91,8 +99,7 @@ public void withdraw(final Long userId, final AppleWithdrawAuthCodeReq AppleWith
throw new InvalidValueException(FailureCode.INVALID_PLATFORM_TYPE);
}

deleteRefreshToken(foundUser.getId());
userRepository.deleteById(foundUser.getId());
deleteAllDataByUser(foundUser.getId());
}

//닉네임 중복체크
Expand Down Expand Up @@ -167,13 +174,41 @@ private RefreshToken getRefreshTokenByToken(final String refreshToken) {
}
}

//refreshToken 삭제
private void deleteRefreshToken(final long userId) {
refreshTokenRepository.deleteRefreshTokenByUserId(userId);
}

//토큰 발급
private Token issueToken(final Long userId) {
return jwtProvider.issueToken(userId);
}

//리프레시 토큰 삭제
private void deleteRefreshToken(final Long userId) {
refreshTokenRepository.deleteRefreshTokenByUserId(userId);
}

//유저 탈퇴 시, 모든 유저 정보 삭제
private void deleteAllDataByUser(final Long userId) {

//유저태그 삭제
userTagRepository.deleteAllByUserId(userId);

//유저포인트 삭제
pointRepository.deleteAllByUserId(userId);

//유저데이트 삭제
dateRepository.deleteAllByUserId(userId);

//유저 date_access 삭제
dateAccessRepository.deleteAllByUserId(userId);

//유저 코스 삭제
courseRepository.deleteAllByUserId(userId);

//유저 좋아요 삭제
likeRepository.deleteAllByUserId(userId);

//리프레시토큰 삭제
deleteRefreshToken(userId);

//유저 삭제
userRepository.deleteById(userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.dateroad.user.service;

import io.micrometer.common.lang.Nullable;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dateroad.code.FailureCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public interface CourseRepository extends JpaRepository<Course, Long> , JpaSpeci
List<Course> findTopCoursesByLikes(Pageable pageable);
@Query("SELECT c FROM Course c ORDER BY c.createdAt DESC")
List<Course> findTopCoursesByCreatedAt(Pageable pageable);

@Modifying
@Query("DELETE FROM UserTag ut WHERE ut.user.id = :userId")
void deleteAllByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import org.dateroad.date.domain.Course;
import org.dateroad.dateAccess.domain.DateAccess;
import org.dateroad.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -21,4 +22,8 @@ public interface DateAccessRepository extends JpaRepository<DateAccess,Long> {
@Transactional
@Query(value = "DELETE FROM date_access WHERE course_id = :courseId", nativeQuery = true)
void deleteByCourse(@Param("courseId") Long courseId);

@Modifying
@Query("DELETE FROM UserTag ut WHERE ut.user.id = :userId")
void deleteAllByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.dateroad.like.repository;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.dateroad.date.domain.Course;
import org.dateroad.like.domain.Like;
Expand All @@ -26,4 +25,8 @@ public interface LikeRepository extends JpaRepository<Like, Long> {

@Query("SELECT l.course, COUNT(l) FROM Like l WHERE l.course IN :courses GROUP BY l.course")
List<Object[]> countByCourses(@Param("courses") List<Course> courses);

@Modifying
@Query("DELETE FROM UserTag ut WHERE ut.user.id = :userId")
void deleteAllByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import java.util.List;
import org.dateroad.point.domain.Point;
import org.dateroad.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface PointRepository extends JpaRepository<Point, Long> {
List<Point> findAllByUserIdOrderByCreatedAtDesc(Long userId);
List<Point> findAllByUserIdOrderByCreatedAtDesc(final Long userId);

@Modifying
@Query("DELETE FROM UserTag ut WHERE ut.user.id = :userId")
void deleteAllByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package org.dateroad.tag.repository;

import org.dateroad.tag.domain.UserTag;
import org.dateroad.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserTagRepository extends JpaRepository<UserTag, Long> {
List<UserTag> findAllByUserId(Long userId);
void deleteAllByUserId(Long userId);
List<UserTag> findAllByUserId(final Long userId);

@Modifying
@Query("DELETE FROM UserTag ut WHERE ut.user.id = :userId")
void deleteAllByUserId(@Param("userId") final Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public class User extends BaseTimeEntity {
@Setter
private int totalPoint = 0;

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<UserTag> userTags;

public static User create(final String name, final String platformUserId, final Platform platForm, final String imageUrl) {
return User.builder()
.name(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import org.dateroad.user.domain.Platform;
import org.dateroad.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
boolean existsUserByPlatFormAndPlatformUserId(final Platform platform, final String platformUserId);

Expand Down

0 comments on commit 8d96158

Please sign in to comment.