|
14 | 14 | import com.leets7th.job_is_be.domain.user.repository.UserProfileRepository; |
15 | 15 | import com.leets7th.job_is_be.domain.user.repository.UserRegionRepository; |
16 | 16 | import com.leets7th.job_is_be.domain.user.repository.UserRepository; |
| 17 | +import com.leets7th.job_is_be.domain.user.enums.OnboardingStep; |
17 | 18 | import com.leets7th.job_is_be.global.exception.GeneralException; |
18 | 19 | import com.leets7th.job_is_be.global.status.ErrorStatus; |
19 | 20 | import org.springframework.stereotype.Service; |
20 | 21 | import org.springframework.transaction.annotation.Transactional; |
21 | 22 |
|
22 | 23 | import java.util.ArrayList; |
| 24 | +import java.time.LocalDateTime; |
23 | 25 | import java.util.LinkedHashSet; |
24 | 26 | import java.util.List; |
25 | 27 | import java.util.Map; |
@@ -105,6 +107,31 @@ public ProfileDraftResponse getDraft(Long userId) { |
105 | 107 | return toDraftResponse(profile, findJobCategories(userId), findRegion(userId)); |
106 | 108 | } |
107 | 109 |
|
| 110 | + @Transactional |
| 111 | + public void completeOnboarding(Long userId) { |
| 112 | + UserProfile profile = userProfileRepository.findByUserId(userId) |
| 113 | + .orElseThrow(() -> new GeneralException(ErrorStatus.PROFILE_NOT_FOUND)); |
| 114 | + if (profile.isOnboardingCompleted()) { |
| 115 | + throw new GeneralException(ErrorStatus.PROFILE_ALREADY_COMPLETED); |
| 116 | + } |
| 117 | + if (profile.getOnboardingStep() != OnboardingStep.REVIEW) { |
| 118 | + throw new GeneralException(ErrorStatus.PROFILE_ONBOARDING_NOT_READY); |
| 119 | + } |
| 120 | + |
| 121 | + List<UserJobCategory> jobCategories = findJobCategories(userId); |
| 122 | + long primaryCount = jobCategories.stream().filter(UserJobCategory::isPrimary).count(); |
| 123 | + boolean missingRequiredValue = jobCategories.isEmpty() |
| 124 | + || jobCategories.size() > MAX_JOB_CATEGORIES |
| 125 | + || primaryCount != 1 |
| 126 | + || findRegion(userId) == null |
| 127 | + || profile.getCareerLevel() == null; |
| 128 | + if (missingRequiredValue) { |
| 129 | + throw new GeneralException(ErrorStatus.PROFILE_REQUIRED_FIELDS_MISSING); |
| 130 | + } |
| 131 | + |
| 132 | + profile.completeOnboarding(LocalDateTime.now()); |
| 133 | + } |
| 134 | + |
108 | 135 | private List<JobCategory> resolveJobCategories(List<Long> requestedIds, Long primaryId) { |
109 | 136 | List<Long> ids = requestedIds == null ? List.of() : requestedIds; |
110 | 137 | LinkedHashSet<Long> uniqueIds = new LinkedHashSet<>(ids); |
|
0 commit comments