From 242fad1691917c4fd82c7f04b6190a7113628e93 Mon Sep 17 00:00:00 2001
From: wayne <wayne8692wayne8692@gmail.com>
Date: 星期五, 11 三月 2022 15:31:04 +0800
Subject: [PATCH] Merge branch 'pollex-dev' into sit

---
 pamapi/src/main/java/com/pollex/pam/service/CustomerService.java |   59 +++++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 43 insertions(+), 16 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 fe9a396..3f1b6c4 100644
--- a/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java
+++ b/pamapi/src/main/java/com/pollex/pam/service/CustomerService.java
@@ -2,47 +2,55 @@
 
 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.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.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 {
-	
+
 	@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);
@@ -54,9 +62,28 @@
 	    				+ " => 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();
 	}

--
Gitblit v1.8.0