Skip to content

Commit

Permalink
feat: 보틀 더받기 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
injoon2019 committed Oct 27, 2024
1 parent c965de8 commit cf7c4c1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ class BottleControllerV2(
fun getPingPongList(@AuthUserId userId: Long): PingPongListResponseV2 {
return bottleFacadeV2.getPingPongBottles(userId)
}

// TODO: 따닥 방지
@ApiOperation("마이페이지 - 추가로 보틀 받기")
@GetMapping("/additional-random")
@AuthRequired
fun getAdditionalRandomBottle(@AuthUserId userId: Long) {
return bottleFacadeV2.getAdditionalRandomBottle(userId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ class BottleFacadeV2(
}
}

fun getAdditionalRandomBottle(userId: Long) {
val user = userService.findByIdAndNotDeleted(userId)
val blockUserIds = blockContactListService.findAllByUserId(userId).map { it.userId }.toSet() // 내가 차단한 유저
val blockedMeUserIds = blockContactListService.findAllByPhoneNumber(
user.phoneNumber ?: throw IllegalStateException("핸드폰 번호를 등록해주세요")
).map { it.userId }.toSet() // 나를 차단한 유저

bottleService.matchAdditionalRandomBottle(user, BOTTLE_PUSH_TIME.hour, blockUserIds, blockedMeUserIds)
?.also {
applicationEventPublisher.publishEvent(
BottleMatchEventDto(
sourceUserId = it.sourceUser.id,
targetUserId = it.targetUser.id,
)
)
}
}

private fun getNextBottleLeftHours(now: LocalDateTime): Int {
return if (now.toLocalTime() > BOTTLE_PUSH_TIME) {
BOTTLE_PUSH_TIME.hour + (LocalTime.MAX.hour - now.hour)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,36 @@ class BottleMatchingRepository(
}
)
}

fun findAdditionalAllUserCanBeMatched(userId: Long, gender: Gender): List<UsersCanBeMatchedDto> {
val sql = """
SELECT u.id AS willMatchUserId, u.gender AS willMatchUserGender, u.city AS city
FROM user u
JOIN user_profile up ON up.user_id = u.id
AND up.image_url IS NOT NULL
AND up.introduction IS NOT NULL
AND JSON_LENGTH(up.introduction) > 0
WHERE 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")
)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,41 @@ class BottleService(
return savedBottle
}

@Transactional
fun matchAdditionalRandomBottle(
user: User,
matchingHour: Int,
blockUserIds: Set<Long>,
blockedMeUserIds: Set<Long>
): Bottle? {
if (user.isNotRegisterProfile()) return null
if (user.isMatchInactive()) return null

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

// 보틀 더받기 API는 매칭 상대가 없으면 이전에 매칭된 상대라도 보여준다
if (usersCanBeMatched.isEmpty()) {
usersCanBeMatched = bottleMatchingRepository.findAdditionalAllUserCanBeMatched(user.id, user.gender!!)
.filter { it.willMatchUserId !in blockUserIds }
.filter { it.willMatchUserId !in blockedMeUserIds }
}

if (usersCanBeMatched.isEmpty()) return null

val matchingUserDto = findUserSameRegionOrRandom(usersCanBeMatched, user)
val matchingUser = userRepository.findByIdAndDeletedFalse(matchingUserDto.willMatchUserId)
?: throw IllegalArgumentException("탈퇴한 회원입니다")

val bottle = Bottle(targetUser = user, sourceUser = matchingUser, expiredAt = LocalDateTime.now().plusDays(1))
val savedBottle = bottleRepository.save(bottle)

user.updateLastRandomMatchedAt(LocalDateTime.now())

return savedBottle
}

private fun getMatchingTime(matchingHour: Int): LocalDateTime {
val now = LocalDateTime.now()
var matchingTime = now.with(LocalTime.of(matchingHour, 0))
Expand Down

0 comments on commit cf7c4c1

Please sign in to comment.