Skip to content

Commit

Permalink
Revert "feat: 회원가입시 3개 받도록 수정 (#558)" (#562)
Browse files Browse the repository at this point in the history
This reverts commit ad71348.
  • Loading branch information
injoon2019 authored Nov 11, 2024
1 parent 79ff949 commit 284ae79
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.nexters.bottles.api.auth.component

import com.nexters.bottles.app.auth.event.DeleteUserEventDto
import com.nexters.bottles.api.auth.component.event.DeleteUserEventDto
import com.nexters.bottles.app.auth.service.BlackListService
import com.nexters.bottles.app.auth.service.RefreshTokenService
import com.nexters.bottles.app.bottle.service.BottleCachingService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nexters.bottles.app.auth.event
package com.nexters.bottles.api.auth.component.event

data class DeleteUserEventDto(
val userId: Long,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.bottles.app.auth.event
package com.nexters.bottles.api.auth.event

import com.nexters.bottles.api.auth.event.dto.SignUpEventDto
import com.nexters.bottles.app.notification.component.FcmClient
import com.nexters.bottles.app.notification.component.dto.FcmNotification
import com.nexters.bottles.app.notification.service.FcmTokenService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nexters.bottles.app.auth.event
package com.nexters.bottles.api.auth.event.dto

class SignUpEventDto(
val userName: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import com.nexters.bottles.api.auth.component.ApplePublicKeyGenerator
import com.nexters.bottles.api.auth.component.AuthCodeGenerator
import com.nexters.bottles.api.auth.component.JwtTokenProvider
import com.nexters.bottles.api.auth.component.NaverSmsEncoder
import com.nexters.bottles.app.auth.event.DeleteUserEventDto
import com.nexters.bottles.app.auth.event.SignUpEventDto
import com.nexters.bottles.api.auth.component.event.DeleteUserEventDto
import com.nexters.bottles.api.auth.event.dto.SignUpEventDto
import com.nexters.bottles.api.auth.facade.dto.AppleRevokeResponse
import com.nexters.bottles.api.auth.facade.dto.AppleSignInUpRequest
import com.nexters.bottles.api.auth.facade.dto.AppleSignInUpResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BottleFacade(
if (isActiveMatching) {
val matchingHour = BOTTLE_PUSH_TIME.hour
bottleService.matchRandomBottle(user.id, matchingHour, blockUserIds, blockedMeUserIds)
.forEach {
?.also {
applicationEventPublisher.publishEvent(
BottleMatchEventDto(
bottleId = it.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BottleFacadeV2(
).map { it.userId }.toSet() // 나를 차단한 유저

bottleService.matchRandomBottle(user.id, BOTTLE_PUSH_TIME.hour, blockUserIds, blockedMeUserIds)
.forEach {
?.also {
applicationEventPublisher.publishEvent(
BottleMatchEventDto(
bottleId = it.id,
Expand Down Expand Up @@ -78,7 +78,7 @@ class BottleFacadeV2(
).map { it.userId }.toSet() // 나를 차단한 유저

bottleService.matchAdditionalRandomBottle(user.id, BOTTLE_PUSH_TIME.hour, blockUserIds, blockedMeUserIds)
.forEach {
?.also {
applicationEventPublisher.publishEvent(
BottleMatchEventDto(
bottleId = it.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,6 @@ class BottleMatchingRepository(
)
}

fun findAllUserCanBeMatchedWithoutIntroduction(userId: Long, gender: Gender): List<UsersCanBeMatchedDto> {
val sql = """
SELECT u.id AS willMatchUserId, u.gender AS willMatchUserGender, u.city AS city
FROM user u
LEFT JOIN bottle_history bh ON bh.user_id = :userId AND bh.matched_user_id = u.id
WHERE bh.id IS NULL
AND u.id != :userId
AND u.gender != :gender
AND u.deleted = false
AND u.is_match_activated = true;
""".trimIndent()

val namedParameters = mapOf(
"userId" to userId,
"gender" to gender.name
)

return namedParameterJdbcTemplate.query(
sql,
namedParameters,
{ rs, _ ->
UsersCanBeMatchedDto(
willMatchUserId = rs.getLong("willMatchUserId"),
willMatchUserGender = rs.getString("willMatchUserGender"),
willMatchCity = rs.getString("city")
)
}
)
}

fun findAdditionalAllUserCanBeMatched(userId: Long, gender: Gender): List<UsersCanBeMatchedDto> {
val sql = """
SELECT u.id AS willMatchUserId, u.gender AS willMatchUserGender, u.city AS city
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,47 +167,45 @@ class BottleService(
userId: Long,
matchingHour: Int,
blockUserIds: Set<Long>,
blockedMeUserIds: Set<Long>,
count: Int = 1,
): List<Bottle> {
blockedMeUserIds: Set<Long>
): Bottle? {
val user = userRepository.findByIdOrNull(userId) ?: throw IllegalStateException("회원가입 상태를 문의해주세요")

if (user.isNotRegisterProfile()) return emptyList()
if (user.isMatchInactive()) return emptyList()
if (user.isNotRegisterProfile()) return null
if (user.isMatchInactive()) return null

val matchingTime = getMatchingTime(matchingHour)
if (user.lastRandomMatchedAt > matchingTime) return emptyList()
if (user.lastRandomMatchedAt > matchingTime) return null

val usersCanBeMatched = bottleMatchingRepository.findAllUserCanBeMatched(user.id, user.gender!!)
.filter { it.willMatchUserId !in blockUserIds }
.filter { it.willMatchUserId !in blockedMeUserIds }

if (usersCanBeMatched.isEmpty()) return emptyList()
if (usersCanBeMatched.isEmpty()) return null

val matchingUserDtos = findUserSameRegionOrRandom(usersCanBeMatched, user, count)
val matchingUsers = userRepository.findByIdInAndDeletedFalse(matchingUserDtos.map { it.willMatchUserId })
val matchingUserDto = findUserSameRegionOrRandom(usersCanBeMatched, user)
val matchingUser = userRepository.findByIdAndDeletedFalse(matchingUserDto.willMatchUserId)
?: throw IllegalArgumentException("탈퇴한 회원입니다")

val now = LocalDateTime.now()
var bottles = matchingUsers.map { matchingUser -> Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = now.plusDays(1)) }
val savedBottles = bottleRepository.saveAll(bottles)
val bottle = Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = matchingTime.plusDays(1))
val savedBottle = bottleRepository.save(bottle)

user.updateLastRandomMatchedAt(LocalDateTime.now())

return savedBottles
return savedBottle
}

@Transactional
fun matchAdditionalRandomBottle(
userId: Long,
matchingHour: Int,
blockUserIds: Set<Long>,
blockedMeUserIds: Set<Long>,
count: Int = 1,
): List<Bottle> {
blockedMeUserIds: Set<Long>
): Bottle? {
val user = userRepository.findByIdOrNull(userId) ?: throw IllegalStateException("회원가입 상태를 문의해주세요")

if (user.isNotRegisterProfile()) return emptyList()
if (user.isMatchInactive()) return emptyList()
if (user.isNotRegisterProfile()) return null
if (user.isMatchInactive()) return null

var usersCanBeMatched = bottleMatchingRepository.findAllUserCanBeMatched(user.id, user.gender!!)
.filter { it.willMatchUserId !in blockUserIds }
Expand All @@ -220,18 +218,18 @@ class BottleService(
.filter { it.willMatchUserId !in blockedMeUserIds }
}

if (usersCanBeMatched.isEmpty()) return emptyList()
if (usersCanBeMatched.isEmpty()) return null

val matchingUserDtos = findUserSameRegionOrRandom(usersCanBeMatched, user, count)
val matchingUsers = userRepository.findByIdInAndDeletedFalse(matchingUserDtos.map { it.willMatchUserId })
val matchingUserDto = findUserSameRegionOrRandom(usersCanBeMatched, user)
val matchingUser = userRepository.findByIdAndDeletedFalse(matchingUserDto.willMatchUserId)
?: throw IllegalArgumentException("탈퇴한 회원입니다")

val now = LocalDateTime.now()
var bottles = matchingUsers.map { matchingUser -> Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = now.plusDays(1)) }
val savedBottles = bottleRepository.saveAll(bottles)
val bottle = Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = LocalDateTime.now().plusDays(1))
val savedBottle = bottleRepository.save(bottle)

user.updateLastRandomMatchedAt(LocalDateTime.now())

return savedBottles
return savedBottle
}

private fun getMatchingTime(matchingHour: Int): LocalDateTime {
Expand All @@ -245,25 +243,13 @@ class BottleService(

private fun findUserSameRegionOrRandom(
usersCanBeMatchedDtos: List<UsersCanBeMatchedDto>,
targetUser: User,
count: Int,
): List<UsersCanBeMatchedDto> {
val canBeMatchedDtos = usersCanBeMatchedDtos.shuffled()
.filter {
targetUser: User
): UsersCanBeMatchedDto {
return usersCanBeMatchedDtos.shuffled()
.firstOrNull {
targetUser.gender?.name != it.willMatchUserGender
targetUser.city == it.willMatchCity
}


// 필터링된 사용자가 count에 도달하지 못하면 추가로 사용자 채우기
return if (canBeMatchedDtos.size < count) {
val additionalDtos = usersCanBeMatchedDtos.shuffled()
.filter { it !in canBeMatchedDtos } // 이미 선택된 항목을 제외
.take(count - canBeMatchedDtos.size) // 모자란 개수만큼 추가
canBeMatchedDtos + additionalDtos // 기존 결과에 추가 결과를 합쳐서 반환
} else {
canBeMatchedDtos
}
} ?: usersCanBeMatchedDtos[0]
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -299,22 +285,23 @@ class BottleService(
}

@Transactional
fun matchFirstRandomBottle(userId: Long, count: Int): List<Bottle> {
fun matchFirstRandomBottle(userId: Long): Bottle? {
val user = userRepository.findByIdOrNull(userId) ?: throw IllegalStateException("회원가입 상태를 문의해주세요")

val usersCanBeMatched = bottleMatchingRepository.findAllUserCanBeMatchedWithoutIntroduction(user.id, user.gender!!).take(count)
if (usersCanBeMatched.isEmpty()) return emptyList()
val usersCanBeMatched = bottleMatchingRepository.findAllUserCanBeMatched(user.id, user.gender!!)
if (usersCanBeMatched.isEmpty()) return null

val matchingUserDtos = findUserSameRegionOrRandom(usersCanBeMatched, user, count)
val matchingUsers = userRepository.findByIdInAndDeletedFalse(matchingUserDtos.map { it.willMatchUserId })
val matchingUserDto = findUserSameRegionOrRandom(usersCanBeMatched, user)
val matchingUser = userRepository.findByIdAndDeletedFalse(matchingUserDto.willMatchUserId)
?: throw IllegalArgumentException("탈퇴한 회원입니다")

val now = LocalDateTime.now()
var bottles = matchingUsers.map { matchingUser -> Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = now.plusDays(1)) }
val savedBottles = bottleRepository.saveAll(bottles)
val bottle = Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = now.plusDays(1))
val savedBottle = bottleRepository.save(bottle)

user.updateLastRandomMatchedAt(now)

return savedBottles
return savedBottle
}

// TODO 클라이언트에서 문답 읽음 표시를 v2로 옮긴 후 변경 -> Letter의 isReadByOtherUser 제거 (이후 읽음 표시는 BottleReadHistory 한곳에서만 관리하도록 함)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.nexters.bottles.app.user.component.event

import com.nexters.bottles.app.auth.event.SignUpEventDto
import com.nexters.bottles.app.bottle.service.BottleHistoryService
import com.nexters.bottles.app.bottle.service.BottleService
import com.nexters.bottles.app.common.component.FileService
import com.nexters.bottles.app.user.component.event.dto.IntroductionSaveEventDto
import com.nexters.bottles.app.user.component.event.dto.UploadImageEventDto
import com.nexters.bottles.app.user.service.UserService
import org.springframework.scheduling.annotation.Async
Expand All @@ -20,9 +20,8 @@ class UserProfileApplicationEventListener(

@Async
@TransactionalEventListener
fun handleCustomEvent(event: SignUpEventDto) {
var savedBottles = bottleService.matchFirstRandomBottle(event.userId, 3)
savedBottles.forEach {
fun handleCustomEvent(event: IntroductionSaveEventDto) {
bottleService.matchFirstRandomBottle(event.userId)?.let {
bottleHistoryService.saveMatchingHistory(sourceUserId = it.sourceUser.id, targetUserId = it.targetUser.id)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nexters.bottles.app.user.component.event.dto

data class IntroductionSaveEventDto(
val userId: Long,
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ interface UserRepository : JpaRepository<User, Long> {

fun findByIdAndDeletedFalse(id: Long): User?

fun findByIdInAndDeletedFalse(id: List<Long>): List<User>

fun findAllByDeletedFalseAndIsMatchActivatedTrue(): List<User>

fun findByPhoneNumber(phoneNumber: String?): User?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nexters.bottles.app.user.service

import com.nexters.bottles.app.user.component.event.dto.IntroductionSaveEventDto
import com.nexters.bottles.app.user.component.event.dto.UploadImageEventDto
import com.nexters.bottles.app.user.domain.QuestionAndAnswer
import com.nexters.bottles.app.user.domain.User
Expand Down Expand Up @@ -42,7 +43,15 @@ class UserProfileService(
fun saveIntroduction(userId: Long, introduction: List<QuestionAndAnswer>, firstMatchingCount: Int) {
val user = userRepository.findByIdOrNull(userId) ?: throw IllegalStateException("회원가입 상태를 문의해주세요")
profileRepository.findByUserId(user.id)?.let {
val isFirstRegisterIntroduction = it.introduction.isEmpty()
it.introduction = introduction
if (isFirstRegisterIntroduction) {
repeat(firstMatchingCount) {
applicationEventPublisher.publishEvent(
IntroductionSaveEventDto(userId = userId)
)
}
}
} ?: run {
profileRepository.save(
UserProfile(
Expand Down

0 comments on commit 284ae79

Please sign in to comment.