|
| 1 | +package com.devkor.ifive.nadab.domain.test.application; |
| 2 | + |
| 3 | +import com.devkor.ifive.nadab.domain.terms.application.TermsCommandService; |
| 4 | +import com.devkor.ifive.nadab.domain.test.api.dto.request.CreateTestUserRequest; |
| 5 | +import com.devkor.ifive.nadab.domain.test.api.dto.response.CreateTestUserResponse; |
| 6 | +import com.devkor.ifive.nadab.domain.user.core.entity.SignupStatusType; |
| 7 | +import com.devkor.ifive.nadab.domain.user.core.entity.User; |
| 8 | +import com.devkor.ifive.nadab.domain.user.core.repository.UserRepository; |
| 9 | +import com.devkor.ifive.nadab.domain.user.core.service.UserProfileUpdateService; |
| 10 | +import com.devkor.ifive.nadab.domain.wallet.core.entity.UserWallet; |
| 11 | +import com.devkor.ifive.nadab.domain.wallet.core.repository.UserWalletRepository; |
| 12 | +import com.devkor.ifive.nadab.global.core.response.ErrorCode; |
| 13 | +import com.devkor.ifive.nadab.global.exception.BadRequestException; |
| 14 | +import com.devkor.ifive.nadab.global.exception.ConflictException; |
| 15 | +import lombok.RequiredArgsConstructor; |
| 16 | +import org.springframework.beans.factory.annotation.Value; |
| 17 | +import org.springframework.context.annotation.Profile; |
| 18 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 19 | +import org.springframework.stereotype.Service; |
| 20 | +import org.springframework.transaction.annotation.Transactional; |
| 21 | + |
| 22 | +@Profile({"local", "dev"}) |
| 23 | +@Service |
| 24 | +@Transactional |
| 25 | +@RequiredArgsConstructor |
| 26 | +public class TestUserService { |
| 27 | + |
| 28 | + private final UserRepository userRepository; |
| 29 | + private final UserWalletRepository userWalletRepository; |
| 30 | + private final UserProfileUpdateService userProfileUpdateService; |
| 31 | + private final TermsCommandService termsCommandService; |
| 32 | + private final PasswordEncoder passwordEncoder; |
| 33 | + |
| 34 | + @Value("${test.account.password:}") |
| 35 | + private String testUserPassword; |
| 36 | + |
| 37 | + public CreateTestUserResponse createTestUser(CreateTestUserRequest request) { |
| 38 | + if (testUserPassword == null || testUserPassword.isBlank()) { |
| 39 | + throw new BadRequestException(ErrorCode.TEST_USER_PASSWORD_NOT_CONFIGURED); |
| 40 | + } |
| 41 | + if (userRepository.existsByEmail(request.email())) { |
| 42 | + throw new ConflictException(ErrorCode.EMAIL_ALREADY_EXISTS); |
| 43 | + } |
| 44 | + |
| 45 | + String passwordHash = passwordEncoder.encode(testUserPassword); |
| 46 | + User user = User.createUser(request.email(), passwordHash); |
| 47 | + userRepository.save(user); |
| 48 | + |
| 49 | + userProfileUpdateService.updateNickname(user, request.nickname()); |
| 50 | + user.updateSignupStatus(SignupStatusType.COMPLETED); |
| 51 | + |
| 52 | + userWalletRepository.save(UserWallet.create(user)); |
| 53 | + termsCommandService.saveConsents(user.getId(), true, true, true, false); |
| 54 | + |
| 55 | + return new CreateTestUserResponse( |
| 56 | + user.getId(), |
| 57 | + user.getEmail(), |
| 58 | + user.getNickname(), |
| 59 | + user.getSignupStatus().name() |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
0 commit comments