diff --git a/src/main/java/com/project/growfit/domain/User/repository/ChildRepository.java b/src/main/java/com/project/growfit/domain/User/repository/ChildRepository.java index 98a5fb1..be8eee8 100644 --- a/src/main/java/com/project/growfit/domain/User/repository/ChildRepository.java +++ b/src/main/java/com/project/growfit/domain/User/repository/ChildRepository.java @@ -2,6 +2,7 @@ import com.project.growfit.domain.User.entity.Child; import com.project.growfit.domain.User.entity.Parent; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -18,4 +19,6 @@ public interface ChildRepository extends JpaRepository { boolean existsByCodeNumberAndLoginId(String code, String login_id); boolean existsByCodeNumber(String code); + + List findAllByParentOrderByCreatedAtDesc(Parent parent); } \ No newline at end of file diff --git a/src/main/java/com/project/growfit/domain/User/service/impl/AuthParentServiceImpl.java b/src/main/java/com/project/growfit/domain/User/service/impl/AuthParentServiceImpl.java index 7db318d..f5c9ffa 100644 --- a/src/main/java/com/project/growfit/domain/User/service/impl/AuthParentServiceImpl.java +++ b/src/main/java/com/project/growfit/domain/User/service/impl/AuthParentServiceImpl.java @@ -65,7 +65,7 @@ public ChildQrCodeResponseDto createQR() throws WriterException { int height = 200; String uniqueCode = UUID.randomUUID().toString(); - Child child = authenticatedProvider.getAuthenticatedChild(); + Child child = authenticatedProvider.getAuthenticatedChildForRegistration(); Long id = child.getId(); log.info("[createQR] QR 코드 생성 요청: user_id={}", parent.getId()); diff --git a/src/main/java/com/project/growfit/global/auth/service/AuthenticatedUserProvider.java b/src/main/java/com/project/growfit/global/auth/service/AuthenticatedUserProvider.java index cf5a356..73b5b87 100644 --- a/src/main/java/com/project/growfit/global/auth/service/AuthenticatedUserProvider.java +++ b/src/main/java/com/project/growfit/global/auth/service/AuthenticatedUserProvider.java @@ -7,6 +7,7 @@ import com.project.growfit.global.auth.dto.CustomUserDetails; import com.project.growfit.global.exception.BusinessException; import com.project.growfit.global.exception.ErrorCode; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -41,6 +42,26 @@ public Child getAuthenticatedChild() { }; } + public Child getAuthenticatedChildForRegistration() { + CustomUserDetails user = getCurrentUser(); + if (user == null) { + throw new BusinessException(ErrorCode.UNAUTHORIZED_CHILD); // 인증되지 않은 사용자 + } + return switch (user.getRole()) { + case "ROLE_CHILD" -> getChildByLoginId(user.getUserId()); + case "ROLE_PARENT" -> { + Parent parent = getParentByEmail(user.getEmail()); + List children = childRepository.findAllByParentOrderByCreatedAtDesc(parent); + + if (children.isEmpty()) { + throw new BusinessException(ErrorCode.CHILD_NOT_FOUND); + } + yield children.get(0); // 가장 최근에 생성된 자녀 + } + default -> throw new BusinessException(ErrorCode.FORBIDDEN_ACCESS); + }; + } + private CustomUserDetails getCurrentUser() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth == null || !auth.isAuthenticated()) {