| | |
| | | |
| | | 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; |
| | |
| | | @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 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); |
| | |
| | | + " => 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(); |
| | | } |