From e93c2fa0d3f054f6dd5adef5ecce0c4e58a5ffa9 Mon Sep 17 00:00:00 2001 From: wayne <wayne8692wayne8692@gmail.com> Date: 星期五, 26 十一月 2021 19:30:55 +0800 Subject: [PATCH] [update] 調整OtpVerify需寫入至Otp_tmp table中 --- pamapi/src/main/java/com/pollex/pam/service/CustomerService.java | 44 +++++++++++++++++++++++++++++++++----------- 1 files changed, 33 insertions(+), 11 deletions(-) diff --git a/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java b/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java index 30561b9..fe9a396 100644 --- a/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java +++ b/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java @@ -1,8 +1,11 @@ package com.pollex.pam.service; +import java.util.Optional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.pollex.pam.domain.Customer; import com.pollex.pam.domain.OtpTmp; @@ -13,6 +16,7 @@ import com.pollex.pam.service.mapper.CustomerDTOMapper; @Service +@Transactional public class CustomerService { @Autowired @@ -31,17 +35,35 @@ return customerRepository.save(customer); } - public String registerCustomer(CustomerRegisterDTO registDTO) { - String account = registDTO.getContactType() == OtpLoginTypeEnum.EMAIL?registDTO.getEmail():registDTO.getPhone(); - OtpTmp otpTmp = otpTmpService.findByAccountAndIndexKey(account, registDTO.getIndexKey()); - if(otpTmp.getStatus() == OtpTmpStatusEnum.VERRIFIED) { - Customer customer = customerDTOMapper.toCustomer(registDTO); - save(customer); - String jwt = customerAuthService.authorize(account, registDTO.getIndexKey(), registDTO.getOtpCode()); - return jwt; - }else { - throw new UsernameNotFoundException("Otp record not found"); - } + public Customer registerCustomer(CustomerRegisterDTO registDTO) { + boolean isCustomerExist = checkCustomerExist(registDTO); + if(isCustomerExist) { + throw new UsernameAlreadyUsedException(); + + }else { + String account = getCustomerAccount(registDTO); + + OtpTmp otpTmp = otpTmpService.findByAccountAndIndexKey(account, registDTO.getIndexKey()); + if(otpTmp.getStatus() == OtpTmpStatusEnum.VERRIFIED) { + Customer customer = customerDTOMapper.toCustomer(registDTO); + save(customer); + return customer; + }else { + throw new IllegalArgumentException("Invalid indexKey state error. IndexKey: " + + registDTO.getIndexKey() + + " => status: " + otpTmp.getStatus()); + } + } } + + private String getCustomerAccount(CustomerRegisterDTO registDTO) { + return registDTO.getContactType() == OtpLoginTypeEnum.EMAIL?registDTO.getEmail():registDTO.getPhone(); + } + + private boolean checkCustomerExist(CustomerRegisterDTO registDTO) { + String account = getCustomerAccount(registDTO); + Optional<Customer> customer = customerRepository.findOneByEmailEqualsOrPhoneEquals(account, account); + return customer.isPresent(); + } } -- Gitblit v1.8.0