Skip to content

Commit

Permalink
Fix Broken UserQuit (Disable SoftDelete, Use To HardDelete)
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Diger committed Dec 31, 2023
1 parent f1a1a30 commit 1295225
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class BettingApplicationService(
val betting = findByIdOrThrowException(bettingId)
betting.validateOwnerId(userId)
betting.validateStartDate()
bettingService.softDelete(bettingId)
bettingService.hardDelete(bettingId)
}

private fun findByIdOrThrowException(bettingId: Long): Betting {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,18 @@ class UserApplicationService(
executionMessage = "아직 진행중인 다짐이 있어 회원탈퇴에 실패했습니다."
)
}
userService.hardDelete(id)
refreshTokenService.hardDelete(user)
goalService.hardDelete(user)
goalProofService.hardDelete(user)
gifticonService.hardDelete(user)
bettingService.hardDelete(user)
else if (bettingService.existsBettingParticipantUser(id)) {
throw BaseException.of(
exceptionCode = ExceptionCode.E400_BAD_REQUEST,
executionMessage = "아직 진행중인 다짐에 대한 내기가 있어 회원탈퇴에 실패했습니다."
)
}
bettingService.hardDeleteByUserId(id)
gifticonService.hardDeleteByUserId(id)
goalProofService.hardDeleteByUserId(id)
goalService.hardDeleteByUserId(id)
refreshTokenService.hardDeleteByUserId(id)
userService.hardDeleteById(id)
}

fun isNicknameDuplicated(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ class BettingService(
return bettingRepository.findAllByUserEntity(userEntity).map { it.toDto() }
}

fun existsBettingParticipantUser(userId: Long): Boolean {
val userEntity =
userRepository.findByIdOrNull(userId) ?: throw IllegalStateException("cannot find user $userId")
val bettings = bettingRepository.findAllByUserEntity(userEntity)

for (betting in bettings) {
if (betting.goalEntity.endDate > LocalDateTime.now()) return true
}
return false
}

@Transactional
fun update(bettingId: Long, predictionType: PredictionType): Betting {
val betting = bettingRepository.findByIdOrNull(bettingId)
Expand Down Expand Up @@ -101,8 +112,17 @@ class BettingService(
}

@Transactional
fun hardDelete(user: User) {
val bettings = bettingRepository.findAllByUserEntity(user.fromDto())
fun hardDelete(bettingId: Long) {
val betting = bettingRepository.findByIdOrNull(bettingId)
?: throw IllegalStateException("Cannot find betting $bettingId")
bettingRepository.delete(betting)
}

@Transactional
fun hardDeleteByUserId(userId: Long) {
val bettings = bettingRepository.findAllByUserEntity(
userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId")
)
bettingRepository.deleteAll(bettings)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.whatever.raisedragon.domain.gifticon

import com.whatever.raisedragon.domain.user.User
import com.whatever.raisedragon.domain.user.UserRepository
import com.whatever.raisedragon.domain.user.fromDto
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand Down Expand Up @@ -32,8 +31,10 @@ class GifticonService(
}

@Transactional
fun hardDelete(user: User) {
val gifticons = gifticonRepository.findAllByUserEntity(user.fromDto())
fun hardDeleteByUserId(userId: Long) {
val gifticons = gifticonRepository.findAllByUserEntity(
userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId")
)
gifticonRepository.deleteAll(gifticons)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,19 @@ class GoalService(
}

@Transactional
fun hardDelete(user: User) {
val goals = goalRepository.findAllByUserEntity(user.fromDto())
fun hardDelete(
goal: Goal,
userEntity: UserEntity,
) {
val goalEntity = goal.fromDto(userEntity)
goalRepository.delete(goalEntity)
}

@Transactional
fun hardDeleteByUserId(userId: Long) {
val goals = goalRepository.findAllByUserEntity(
userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId")
)
goalRepository.deleteAll(goals)
}

Expand All @@ -108,6 +119,10 @@ class GoalService(
}

fun findProceedingGoalIsExistsByUser(user: User): Boolean {
return goalRepository.existsByUserEntityAndEndDateIsAfter(user.fromDto(), LocalDateTime.now())
val existsByUserEntityAndEndDateIsAfter = goalRepository.existsByUserEntityAndEndDateIsAfter(
userEntity = user.fromDto(),
now = LocalDateTime.now()
)
return existsByUserEntityAndEndDateIsAfter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class GoalProofService(
}

@Transactional
fun hardDelete(user: User) {
val goalProofs = goalProofRepository.findAllByUserEntity(user.fromDto())
fun hardDeleteByUserId(userId: Long) {
val goalProofs = goalProofRepository.findAllByUserEntity(
userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId")
)
goalProofRepository.deleteAll(goalProofs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package com.whatever.raisedragon.domain.refreshtoken

import com.whatever.raisedragon.domain.user.User
import com.whatever.raisedragon.domain.user.UserEntity
import com.whatever.raisedragon.domain.user.UserRepository
import com.whatever.raisedragon.domain.user.fromDto
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class RefreshTokenService(
private val refreshTokenRepository: RefreshTokenRepository
private val refreshTokenRepository: RefreshTokenRepository,
private val userRepository: UserRepository
) {

@Transactional
Expand All @@ -31,8 +34,10 @@ class RefreshTokenService(
}

@Transactional
fun hardDelete(user: User) {
val refreshTokenEntity = refreshTokenRepository.findByUserEntity(user.fromDto())!!
fun hardDeleteByUserId(userId: Long) {
val refreshTokenEntity = refreshTokenRepository.findByUserEntity(
userEntity = userRepository.findByIdOrNull(userId) ?: throw IllegalArgumentException("Cannot find user $userId")
)!!
refreshTokenRepository.delete(refreshTokenEntity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class UserService(
}

@Transactional
fun hardDelete(id: Long) {
fun hardDeleteById(id: Long) {
val userEntity = loadById(id).fromDto()
userRepository.delete(userEntity)
}
Expand Down

0 comments on commit 1295225

Please sign in to comment.