From 3b0671286dd280c0172352e6f3d116ecd7051efe Mon Sep 17 00:00:00 2001
From: jack <jack.su@pollex.com.tw>
Date: 星期二, 08 八月 2023 15:27:31 +0800
Subject: [PATCH] [UPDATE] 顧問登入如果密碼解密失敗直接拋錯給前端

---
 pamapi/src/main/java/com/pollex/pam/service/CustomerService.java |   79 +++++++++++++++++++++++++++++----------
 1 files changed, 59 insertions(+), 20 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 2fd1590..98af88a 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java
@@ -2,57 +2,96 @@
 
 import java.util.Optional;
 
+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;
-	
+
 	@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) {
+
+	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);
+	    		customer.setDataFrom(DataFromEnum.PAM);
 	        	save(customer);
-	        	String jwt = customerAuthService.authorize(account, registDTO.getIndexKey(), registDTO.getOtpCode());
-	        	return jwt;
+	        	return customer;
 	    	}else {
-	    		throw new UsernameNotFoundException("Otp record not found");
+	    		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();
@@ -60,7 +99,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();
 	}
 }

--
Gitblit v1.8.0