From 06d3a2b2801ecaaa60a3427a70a8179164ffedb9 Mon Sep 17 00:00:00 2001 From: Tomas <tomasysh@gmail.com> Date: 星期三, 16 二月 2022 16:44:38 +0800 Subject: [PATCH] Merge branch 'Phase3' of https://dev.pollex.com.tw:8443/r/pcalife/PAM into Phase3 --- pamapi/src/main/java/com/pollex/pam/service/CustomerService.java | 83 ++++++++++++++++++++++++++++++++--------- 1 files changed, 65 insertions(+), 18 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..79a086d 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,14 @@ package com.pollex.pam.service; +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 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,35 +19,76 @@ import com.pollex.pam.service.mapper.CustomerDTOMapper; @Service +@Transactional public class CustomerService { - + @Autowired CustomerRepository customerRepository; - + @Autowired CustomerDTOMapper customerDTOMapper; - + @Autowired CustomerAuthService customerAuthService; - + @Autowired OtpTmpService otpTmpService; - + + @Autowired + CustomerMapper customerMapper; + public Customer save(Customer customer) { 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()); + } + } + + } + + public void updateLoggedCustomer(CustomerDTO customerDTO) { + Long customerId = SecurityUtils.getCustomerDBId(); + Customer customer = customerRepository.findById(customerId) + .orElseThrow(() -> new UsernameNotFoundException("customerId which is from token is not found in customer db table, customer id = " + customerId)); + + customer.setEmail(customerDTO.getEmail()); + customer.setPhone(customerDTO.getPhone()); + customer.setName(customerDTO.getName()); + customerRepository.save(customer); + } + + public CustomerDTO getLoggedCustomerDTO() { + Long customerId = SecurityUtils.getCustomerDBId(); + Customer customer = customerRepository.findById(customerId) + .orElseThrow(() -> new UsernameNotFoundException("customerId which is from token is not found in customer db table, customer id = " + customerId)); + + return customerMapper.toDto(customer); + } + + 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