保誠-保戶業務員媒合平台
Tomas
2023-09-14 e828225de7636d0195db2c3c9b1701a5b2f12049
pamapi/src/main/java/com/pollex/pam/service/CustomerService.java
@@ -2,25 +2,34 @@
import java.util.Optional;
import com.pollex.pam.security.SecurityUtils;
import com.pollex.pam.service.dto.CustomerDTO;
import com.pollex.pam.service.mapper.CustomerMapper;
import com.pollex.pam.business.security.SecurityUtils;
import com.pollex.pam.business.service.OtpTmpService;
import com.pollex.pam.business.service.UsernameAlreadyUsedException;
import com.pollex.pam.business.service.dto.CustomerDTO;
import com.pollex.pam.business.service.mapper.CustomerMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import com.pollex.pam.enums.OtpLoginTypeEnum;
import com.pollex.pam.enums.OtpTmpStatusEnum;
import com.pollex.pam.repository.CustomerRepository;
import com.pollex.pam.service.dto.CustomerRegisterDTO;
import com.pollex.pam.service.mapper.CustomerDTOMapper;
import com.pollex.pam.business.domain.Customer;
import com.pollex.pam.business.domain.OtpTmp;
import com.pollex.pam.business.enums.DataFromEnum;
import com.pollex.pam.business.enums.OtpLoginTypeEnum;
import com.pollex.pam.business.enums.OtpTmpStatusEnum;
import com.pollex.pam.business.repository.CustomerRepository;
import com.pollex.pam.business.service.dto.CustomerRegisterDTO;
import com.pollex.pam.business.service.mapper.CustomerDTOMapper;
@Service
@Transactional
public class CustomerService {
   private static final Logger log = LoggerFactory.getLogger(CustomerService.class);
   @Autowired
   CustomerRepository customerRepository;
@@ -42,6 +51,7 @@
   }
   public Customer registerCustomer(CustomerRegisterDTO registDTO) {
      boolean isCustomerExist = checkCustomerExist(registDTO);
      if(isCustomerExist) {
         throw new UsernameAlreadyUsedException();
@@ -50,15 +60,23 @@
         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());
          }
         if(otpTmp!=null) {
            if(otpTmp.getStatus() == OtpTmpStatusEnum.VERRIFIED) {
                Customer customer = customerDTOMapper.toCustomer(registDTO);
                customer.setDataFrom(DataFromEnum.PAM);
                 save(customer);
                 return customer;
             }else {
                throw new IllegalArgumentException("Invalid indexKey state error. IndexKey: "
                      + registDTO.getIndexKey()
                      + " => status: " + otpTmp.getStatus());
             }
         }else {
            throw new IllegalArgumentException("otp tmp not exist");
         }
      }
   }
@@ -88,7 +106,7 @@
   private boolean checkCustomerExist(CustomerRegisterDTO registDTO) {
      String account = getCustomerAccount(registDTO);
      Optional<Customer> customer = customerRepository.findOneByEmailEqualsOrPhoneEquals(account, account);
      Optional<Customer> customer = customerRepository.findOneByEmailEqualsOrPhoneEquals(account);
      return customer.isPresent();
   }
}