From 41fe37bdb2eb29426dc7429e3003e71c4e4a6ca3 Mon Sep 17 00:00:00 2001
From: Klaus <klaus@vividnext.co.kr>
Date: Wed, 30 Aug 2023 21:53:45 +0900
Subject: [PATCH] =?UTF-8?q?=EA=B4=80=EB=A6=AC=EC=9E=90=20-=20=EC=9C=A0?=
 =?UTF-8?q?=EC=A0=80=20=EB=B9=84=EB=B0=80=EB=B2=88=ED=98=B8=20=EC=B4=88?=
 =?UTF-8?q?=EA=B8=B0=ED=99=94=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../sodalive/admin/member/AdminMemberController.kt   |  7 +++++++
 .../sodalive/admin/member/AdminMemberRepository.kt   | 12 ++++++++++++
 .../sodalive/admin/member/AdminMemberService.kt      | 10 ++++++++++
 .../sodalive/admin/member/ResetPasswordRequest.kt    |  3 +++
 4 files changed, 32 insertions(+)
 create mode 100644 src/main/kotlin/kr/co/vividnext/sodalive/admin/member/ResetPasswordRequest.kt

diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberController.kt b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberController.kt
index 436e23f..7070169 100644
--- a/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberController.kt
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberController.kt
@@ -4,6 +4,7 @@ import kr.co.vividnext.sodalive.common.ApiResponse
 import org.springframework.data.domain.Pageable
 import org.springframework.security.access.prepost.PreAuthorize
 import org.springframework.web.bind.annotation.GetMapping
+import org.springframework.web.bind.annotation.PostMapping
 import org.springframework.web.bind.annotation.PutMapping
 import org.springframework.web.bind.annotation.RequestBody
 import org.springframework.web.bind.annotation.RequestMapping
@@ -14,6 +15,12 @@ import org.springframework.web.bind.annotation.RestController
 @RequestMapping("/admin/member")
 @PreAuthorize("hasRole('ADMIN')")
 class AdminMemberController(private val service: AdminMemberService) {
+    @PostMapping("/password/reset")
+    fun resetPassword(@RequestBody request: ResetPasswordRequest) = ApiResponse.ok(
+        service.resetPassword(request = request),
+        "비밀번호가 초기화 되었습니다."
+    )
+
     @PutMapping
     fun updateMember(@RequestBody request: UpdateMemberRequest) = ApiResponse.ok(
         service.updateMember(request = request),
diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberRepository.kt b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberRepository.kt
index 4b1a2ed..538259b 100644
--- a/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberRepository.kt
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberRepository.kt
@@ -15,6 +15,7 @@ interface AdminMemberQueryRepository {
 
     fun searchMemberTotalCount(searchWord: String, role: MemberRole? = null): Int
     fun getCreatorAllList(): List<GetAdminCreatorAllListResponse>
+    fun findByIdAndActive(memberId: Long): Member?
 }
 
 class AdminMemberQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : AdminMemberQueryRepository {
@@ -109,4 +110,15 @@ class AdminMemberQueryRepositoryImpl(private val queryFactory: JPAQueryFactory)
             )
             .fetch()
     }
+
+    override fun findByIdAndActive(memberId: Long): Member? {
+        return queryFactory
+            .selectFrom(member)
+            .where(
+                member.id.eq(memberId)
+                    .and(member.isActive.isTrue)
+            )
+            .orderBy(member.id.desc())
+            .fetchFirst()
+    }
 }
diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberService.kt
index 8b81fbc..7cccc74 100644
--- a/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberService.kt
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/AdminMemberService.kt
@@ -6,6 +6,7 @@ import kr.co.vividnext.sodalive.member.MemberRole
 import org.springframework.beans.factory.annotation.Value
 import org.springframework.data.domain.Pageable
 import org.springframework.data.repository.findByIdOrNull
+import org.springframework.security.crypto.password.PasswordEncoder
 import org.springframework.stereotype.Service
 import org.springframework.transaction.annotation.Transactional
 import java.time.ZoneId
@@ -14,6 +15,7 @@ import java.time.format.DateTimeFormatter
 @Service
 class AdminMemberService(
     private val repository: AdminMemberRepository,
+    private val passwordEncoder: PasswordEncoder,
 
     @Value("\${cloud.aws.cloud-front.host}")
     private val cloudFrontHost: String
@@ -133,4 +135,12 @@ class AdminMemberService(
     fun getCreatorAllList(): List<GetAdminCreatorAllListResponse> {
         return repository.getCreatorAllList()
     }
+
+    @Transactional
+    fun resetPassword(request: ResetPasswordRequest) {
+        val member = repository.findByIdAndActive(memberId = request.memberId)
+            ?: throw SodaException("잘못된 회원정보입니다.\n다시 시도해 주세요.")
+
+        member.password = passwordEncoder.encode(member.email.split("@")[0])
+    }
 }
diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/ResetPasswordRequest.kt b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/ResetPasswordRequest.kt
new file mode 100644
index 0000000..009df01
--- /dev/null
+++ b/src/main/kotlin/kr/co/vividnext/sodalive/admin/member/ResetPasswordRequest.kt
@@ -0,0 +1,3 @@
+package kr.co.vividnext.sodalive.admin.member
+
+data class ResetPasswordRequest(val memberId: Long)