Skip to content

Commit

Permalink
imp: 그룹 유저 삭제 처리 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 18, 2024
1 parent 3816058 commit 754e8a3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.hero.alignlab.common.model.HeroPageRequest
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.auth.model.AuthUser
import com.hero.alignlab.domain.group.domain.Group
import com.hero.alignlab.domain.group.domain.GroupUser
import com.hero.alignlab.domain.group.model.CreateGroupContext
import com.hero.alignlab.domain.group.model.request.CheckGroupRegisterRequest
import com.hero.alignlab.domain.group.model.request.CreateGroupRequest
Expand Down Expand Up @@ -66,24 +67,29 @@ class GroupFacade(
}

suspend fun withdraw(uid: Long, groupId: Long) {
val group = groupService.findByIdOrThrow(groupId)

/** 그룹 승계 또는 제거 */
if (group.ownerUid == uid) {
withdrawGroupOwner(uid, group)
parZip(
{ groupService.findByIdOrThrow(groupId) },
{ groupUserService.findByGroupIdAndUidOrThrow(groupId, uid) }
) { group, groupUser ->
when (group.ownerUid == uid) {
/** 그룹 승계 또는 제거 */
true -> withdrawGroupOwner(uid, group, groupUser)

/** 그룹원 제거 */
false -> withdrawGroupUser(group, uid, groupUser)
}
}

/** 그룹원 제거 */
withdrawGroupUser(group, uid)
}

private suspend fun withdrawGroupOwner(uid: Long, group: Group) {
val groupUser = groupUserService.findTop1ByGroupIdAndUidNotOrderByCreatedAtAsc(group.id, uid)
private suspend fun withdrawGroupOwner(uid: Long, group: Group, groupUser: GroupUser) {
val otherGroupUser = groupUserService.findTop1ByGroupIdAndUidNotOrderByCreatedAtAsc(group.id, uid)

txTemplates.writer.executesOrNull {
when (groupUser == null) {
when (otherGroupUser == null) {
/** 그룹 제거 */
true -> groupService.deleteByIdSync(group.id)
true -> {
groupService.deleteByIdSync(group.id)
}

/** 그룹 승계 */
false -> {
Expand All @@ -94,13 +100,14 @@ class GroupFacade(
groupService.saveSync(succeedGroup)
}
}
groupUserService.deleteByGroupIdAndUidSync(group.id, uid)

groupUserService.delete(groupUser)
}
}

private suspend fun withdrawGroupUser(group: Group, uid: Long) {
private suspend fun withdrawGroupUser(group: Group, uid: Long, groupUser: GroupUser) {
txTemplates.writer.executesOrNull {
groupUserService.deleteByGroupIdAndUidSync(group.id, uid)
groupUserService.delete(groupUser)
groupService.saveSync(group.apply { this.userCount -= 1 })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class GroupUserService(
groupUserRepository.deleteByGroupIdAndUid(groupId, uid)
}

@Transactional
fun delete(groupUser: GroupUser) {
groupUserRepository.delete(groupUser)
}

@Transactional
fun saveSync(groupId: Long, uid: Long): GroupUser {
return groupUserRepository.save(GroupUser(groupId = groupId, uid = uid))
Expand All @@ -66,6 +71,11 @@ class GroupUserService(
}
}

suspend fun findByGroupIdAndUidOrThrow(groupId: Long, uid: Long): GroupUser {
return findByGroupIdAndUid(groupId, uid)
?: throw NotFoundException(ErrorCode.NOT_FOUND_GROUP_USER_ERROR)
}

suspend fun findByGroupIdAndUid(groupId: Long, uid: Long): GroupUser? {
return withContext(Dispatchers.IO) {
groupUserRepository.findByGroupIdAndUid(groupId, uid)
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/hero/alignlab/exception/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum class ErrorCode(val status: HttpStatus, val description: String) {
/** Group User Error Code */
DUPLICATE_GROUP_JOIN_ERROR(HttpStatus.BAD_REQUEST, "한개의 그룹만 참여 가능합니다."),
EXCEED_GROUP_USER_COUNT_ERROR(HttpStatus.BAD_REQUEST, "그룹 정원을 초과하였습니다."),
NOT_FOUND_GROUP_USER_ERROR(HttpStatus.NOT_FOUND, "그룹 유저 정보를 찾을 수 없습니다."),

/** Image Client Error Code */
IMAGE_CLIENT_UPLOAD_ERROR(HttpStatus.BAD_REQUEST, "이미지 업로드 중 오류가 발생했습니다."),
Expand Down

0 comments on commit 754e8a3

Please sign in to comment.