package com.pollex.pam.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; 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; @Service public class CustomerService { @Autowired CustomerRepository customerRepository; @Autowired CustomerDTOMapper customerDTOMapper; @Autowired CustomerAuthService customerAuthService; @Autowired OtpTmpService otpTmpService; 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"); } } }