Skip to content

Commit

Permalink
가입 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Jan 13, 2025
1 parent d6411c7 commit 1f8011d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 51 deletions.
3 changes: 0 additions & 3 deletions src/main/kotlin/com/ohayo/moyamoya/api/user/UserApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import org.springframework.web.bind.annotation.RestController
class UserApi(
private val userService: UserService
) {
@GetMapping("exists")
fun exists(@RequestParam phone: String) = userService.exists(phone)

@PostMapping("send-code")
fun sendCode(@RequestParam phone: String) = userService.sendCode(phone)

Expand Down
30 changes: 18 additions & 12 deletions src/main/kotlin/com/ohayo/moyamoya/api/user/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.ohayo.moyamoya.api.user

import com.ohayo.moyamoya.api.common.VoidRes
import com.ohayo.moyamoya.api.user.value.ExistsUserRes
import com.ohayo.moyamoya.api.user.value.RefreshReq
import com.ohayo.moyamoya.api.user.value.SignUpReq
import com.ohayo.moyamoya.api.user.value.UserRes
import com.ohayo.moyamoya.api.user.value.VerifyCodeRes
import com.ohayo.moyamoya.core.*
import com.ohayo.moyamoya.core.extension.findByIdSafety
import com.ohayo.moyamoya.global.CustomException
Expand All @@ -26,10 +26,6 @@ class UserService(
private val phoneCodeRepository: PhoneCodeRepository,
private val sessionHolder: UserSessionHolder
) {
fun exists(phone: String) = ExistsUserRes(
isExists = userRepository.existsByPhone(phone)
)

fun sendCode(phone: String): VoidRes {
val code = smsClient.sendAuthorizationCode(phone)
phoneCodeRepository.save(
Expand All @@ -41,7 +37,8 @@ class UserService(
return VoidRes()
}

fun verifyCode(phone: String, code: String): VoidRes {
@Transactional(rollbackFor = [Exception::class])
fun verifyCode(phone: String, code: String): VerifyCodeRes {
val codes = phoneCodeRepository.findByStatusAndPhoneAndCode(
status = PhoneCodeEntity.Status.UNUSED,
phone = phone,
Expand All @@ -50,12 +47,21 @@ class UserService(

if (codes.isEmpty()) throw CustomException(HttpStatus.BAD_REQUEST, "인증 실패")

codes.forEach {
it.updateStatus()
}

codes.forEach { it.updateStatus() }
phoneCodeRepository.saveAll(codes)
return VoidRes()

val user = userRepository.findByPhone(phone)
return if (user == null) {
VerifyCodeRes(
isNewUser = true,
token = null
)
} else {
VerifyCodeRes(
isNewUser = false,
token = jwtClient.generate(user)
)
}
}

fun signUp(req: SignUpReq): Token {
Expand All @@ -72,7 +78,7 @@ class UserService(
codes.forEach {
it.updateStatus()
}

phoneCodeRepository.saveAll(codes)

val school = schoolRepository.findByIdSafety(req.schoolId)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ohayo.moyamoya.api.user.value

import com.ohayo.moyamoya.infra.token.Token

data class VerifyCodeRes(
val isNewUser: Boolean,
val token: Token?
)
1 change: 0 additions & 1 deletion src/main/kotlin/com/ohayo/moyamoya/core/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.springframework.stereotype.Repository
@Repository
interface UserRepository : JpaRepository<UserEntity, Int> {
fun existsByPhone(phone: String): Boolean

fun findByPhone(phone: String): UserEntity?
}

Expand Down
45 changes: 17 additions & 28 deletions src/test/kotlin/com/ohayo/moyamoya/api/UserApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,37 @@ class UserApiTest {
val testPhone2 = "testPhone2"

@Test
fun `계정 존재여부 확인 - 존재하지 않음`() {
mvc.get("/users/exists") {
fun `인증 코드 발송`() {
mvc.post("/users/send-code") {
param("phone", testPhone2)
}.andExpect {
status { isOk() }
content {
jsonPath("$.isExists", equalTo(false))
contentType(MediaType.APPLICATION_JSON)
}
}
}.andExpect { status { isOk() } }
}

@Test
fun `계정 존재여부 확인 - 존재함`() {
`회원 가입`()
mvc.get("/users/exists") {
fun `인증 코드 발송 후 검증`() {
this.`인증 코드 발송`()
mvc.post("/users/verify-code") {
param("phone", testPhone2)
param("code", TestSmsClient.FAKE_AUTHORIZATION_CODE)
}.andExpect {
status { isOk() }
content {
jsonPath("$.isExists", equalTo(true))
contentType(MediaType.APPLICATION_JSON)
}
jsonPath("$.isNewUser", equalTo(true))
}
}

@Test
fun `인증 코드 발송`() {
mvc.post("/users/send-code") {
param("phone", testPhone2)
}.andExpect { status { isOk() } }
}

@Test
fun `인증 코드 발송 후 검증`() {
`인증 코드 발송`()
fun `회원 가입 후 재 인증`() {
this.`회원 가입`()
this.`인증 코드 발송`()
mvc.post("/users/verify-code") {
param("phone", testPhone2)
param("code", TestSmsClient.FAKE_AUTHORIZATION_CODE)
}.andExpect { status { isOk() } }
}.andExpect {
status { isOk() }
jsonPath("$.isNewUser", equalTo(false))
}.andDo { print() }
}

@Test
fun `인증 코드 발송 후 검증 - 이상한 코드`() {
this.`인증 코드 발송`()
Expand Down Expand Up @@ -93,7 +82,7 @@ class UserApiTest {
contentType = MediaType.APPLICATION_JSON
}.andExpect { status { isOk() } }
}

@Test
fun `회원 가입 - 이상한 인증 코드`() {
this.`인증 코드 발송 후 검증`()
Expand Down
2 changes: 0 additions & 2 deletions src/test/kotlin/com/ohayo/moyamoya/api/dummyValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.ohayo.moyamoya.core.UserEntity
import java.time.LocalDate

val dummySchool = SchoolEntity(
id = 0,
name = "testName",
type = SchoolType.HIGH,
cityName = "testCityName",
Expand All @@ -23,7 +22,6 @@ val dummySchool = SchoolEntity(
)

val dummyUser = UserEntity(
id = 0,
phone = "testPhone",
school = dummySchool,
schoolGrade = 1,
Expand Down

0 comments on commit 1f8011d

Please sign in to comment.