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

[Fix]: 유저 탈퇴시 관련 데이터 삭제 (#109) #110

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@
import io.sobok.SobokSobok.auth.ui.dto.JwtTokenResponse;
import io.sobok.SobokSobok.exception.ErrorCode;
import io.sobok.SobokSobok.exception.model.NotFoundException;
import io.sobok.SobokSobok.friend.infrastructure.FriendQueryRepository;
import io.sobok.SobokSobok.friend.infrastructure.FriendRepository;
import io.sobok.SobokSobok.pill.domain.Pill;
import io.sobok.SobokSobok.pill.infrastructure.PillQueryRepository;
import io.sobok.SobokSobok.pill.infrastructure.PillRepository;
import io.sobok.SobokSobok.pill.infrastructure.PillScheduleQueryRepository;
import io.sobok.SobokSobok.pill.infrastructure.PillScheduleRepository;
import io.sobok.SobokSobok.security.jwt.Jwt;
import io.sobok.SobokSobok.security.jwt.JwtProvider;
import io.sobok.SobokSobok.sticker.infrastructure.LikeScheduleQueryRepository;
import io.sobok.SobokSobok.sticker.infrastructure.LikeScheduleRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AuthService {

private final UserRepository userRepository;
private final FriendRepository friendRepository;
private final PillRepository pillRepository;
private final PillScheduleRepository pillScheduleRepository;
private final LikeScheduleRepository likeScheduleRepository;

private final RedisTemplate<String, String> redisTemplate;
private final JwtProvider jwtProvider;
Expand Down Expand Up @@ -62,8 +77,7 @@ public JwtTokenResponse refresh(String token) {
@Transactional
public void leave(Long userId, String leaveReason) {

User user = userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(ErrorCode.UNREGISTERED_USER));
User user = UserServiceUtil.findUserById(userRepository, userId);

ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
if (valueOperations.get(user.getSocialInfo().getSocialId()) == null) {
Expand All @@ -72,5 +86,14 @@ public void leave(Long userId, String leaveReason) {

redisTemplate.delete(user.getSocialInfo().getSocialId());
user.deleteUser(leaveReason);

List<Pill> pills = pillRepository.findAllByUserId(userId);
for (Pill pill : pills) {
pillScheduleRepository.deleteAllByPillId(pill.getId());
}

pillRepository.deleteAllByUserId(userId);
friendRepository.deleteAllBySenderIdOrReceiverId(userId, userId);
likeScheduleRepository.deleteAllBySenderId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

public interface FriendRepository extends JpaRepository<Friend, Long> {

// READ
List<Friend> findAllBySenderId(Long senderId);

Integer countBySenderId(Long senderId);

// DELETE
void deleteAllBySenderIdOrReceiverId(Long senderId, Long receiverId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ public interface PillRepository extends JpaRepository<Pill, Long> {

// READ
List<Pill> findAllByUserId(Long userId);

// DELETE
void deleteAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@

public interface LikeScheduleRepository extends JpaRepository<LikeSchedule, Long> {

// READ
Boolean existsBySenderIdAndScheduleId(Long senderId, Long scheduleId);

// DELETE
void deleteAllBySenderId(Long userId);
}
Loading