保誠-保戶業務員媒合平台
[ADD]【todo 131166】新增客戶個人帳號資訊API,又因應會有更新機制,故不把手機及email資訊放在detail中
修改4個檔案
新增5個檔案
168 ■■■■ 已變更過的檔案
pamapi/src/doc/客戶API/取得個人帳號資訊.txt 8 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/doc/客戶API/更新個人帳號資訊.txt 10 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/enums/CustomerDetailEnum.java 4 ●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/security/SecurityUtils.java 1 ●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/CustomerAuthService.java 19 ●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/CustomerService.java 43 ●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/dto/CustomerDTO.java 31 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/service/mapper/CustomerMapper.java 19 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/main/java/com/pollex/pam/web/rest/CustomerInfoResource.java 33 ●●●●● 修補檔 | 檢視 | 原始 | 究查 | 歷程
pamapi/src/doc/客戶API/取得個人帳號資訊.txt
比對新檔案
@@ -0,0 +1,8 @@
http get: http://localhost:8080/api/customer/info
response body:
{
    "email": null,
    "phone": "0955555555",
    "name": "test999"
}
pamapi/src/doc/客戶API/更新個人帳號資訊.txt
比對新檔案
@@ -0,0 +1,10 @@
http put: http://localhost:8080/api/customer/info
request body:
{
    "email": null,
    "phone": "0955555555",
    "name": "test998"
}
response http code: 204 (NO_CONTENT)
pamapi/src/main/java/com/pollex/pam/enums/CustomerDetailEnum.java
@@ -2,9 +2,7 @@
public enum CustomerDetailEnum {
    ID("CustomerId"),
    NAME("CustomerName"),
    ACCOUNT("CustomerAccount"),
    CONTACT_TYPE("ContactType");
    NAME("CustomerName");
    private final String value;
pamapi/src/main/java/com/pollex/pam/security/SecurityUtils.java
@@ -107,7 +107,6 @@
        return userDetails.get(ConsultantDetailEnum.AGENT_NO.getValue());
    }
    // todo , should get id from user details
    public static Long getCustomerId() {
        Map<String, String> userDetails = getCurrentUserDetails();
        return Long.parseLong(userDetails.get(CustomerDetailEnum.ID.getValue()));
pamapi/src/main/java/com/pollex/pam/service/CustomerAuthService.java
@@ -25,16 +25,16 @@
@Service
@Transactional
public class CustomerAuthService {
    @Autowired
    AuthenticationManagerBuilder authenticationManagerBuilder;
    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    TokenProvider tokenProvider;
    public String authorize(Customer account, String indexKey, String otpCode) {
        OtpAccount otpAccount = OtpAccount.createOtpAccount(account, indexKey);
        OtpAuthenticationToken authenticationToken = new OtpAuthenticationToken(
@@ -43,26 +43,25 @@
        );
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        Authentication authentication = buildCustomerAuthToken(account, otpCode, indexKey);
        String jwt = tokenProvider.createToken(authentication, false);
        return jwt;
    }
    public UsernamePasswordAuthenticationToken buildCustomerAuthToken(Customer customer
            , String otpCode, String indexKey) {
        List<GrantedAuthority> grantedAuths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
        final String account = customer.toAccountString();
        UsernamePasswordAuthenticationToken authenticationToken =
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(account, otpCode, grantedAuths);
        Map<String, String> details = new HashMap<>();
        details.put(CustomerDetailEnum.ID.getValue(), customer.getId().toString());
        details.put(CustomerDetailEnum.NAME.getValue(), customer.getName());
        details.put(CustomerDetailEnum.ACCOUNT.getValue(), account);
//        details.put(CustomerDetailEnum.CONTACT_TYPE.getValue(), customer.getContactType());
        authenticationToken.setDetails(details);
        return authenticationToken;
pamapi/src/main/java/com/pollex/pam/service/CustomerService.java
@@ -2,6 +2,9 @@
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;
@@ -18,31 +21,34 @@
@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 +60,28 @@
                        + " => status: " + otpTmp.getStatus());
            }
        }
    }
    public void updateLoggedCustomer(CustomerDTO customerDTO) {
        Long customerId = SecurityUtils.getCustomerId();
        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.getCustomerId();
        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();
    }
pamapi/src/main/java/com/pollex/pam/service/dto/CustomerDTO.java
比對新檔案
@@ -0,0 +1,31 @@
package com.pollex.pam.service.dto;
public class CustomerDTO {
    private String email;
    private String phone;
    private String name;
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
pamapi/src/main/java/com/pollex/pam/service/mapper/CustomerMapper.java
比對新檔案
@@ -0,0 +1,19 @@
package com.pollex.pam.service.mapper;
import com.pollex.pam.domain.Customer;
import com.pollex.pam.service.dto.CustomerDTO;
import org.springframework.stereotype.Service;
@Service
public class CustomerMapper {
    public CustomerDTO toDto(Customer customer) {
        CustomerDTO dto = new CustomerDTO();
        dto.setEmail(customer.getEmail());
        dto.setPhone(customer.getPhone());
        dto.setName(customer.getName());
        return dto;
    }
}
pamapi/src/main/java/com/pollex/pam/web/rest/CustomerInfoResource.java
比對新檔案
@@ -0,0 +1,33 @@
package com.pollex.pam.web.rest;
import com.pollex.pam.service.CustomerService;
import com.pollex.pam.service.dto.CustomerDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/customer/info")
public class CustomerInfoResource {
    @Autowired
    CustomerService customerService;
    @GetMapping("")
    public ResponseEntity<CustomerDTO> getLoggedCustomerInfo() {
        return new ResponseEntity<>(customerService.getLoggedCustomerDTO(), HttpStatus.OK);
    }
    @PutMapping("")
    public ResponseEntity<Void> updateLoggedCustomerInfo(@RequestBody CustomerDTO customerDTO) {
        boolean hasEmail = StringUtils.hasText(customerDTO.getEmail());
        boolean hasPhone = StringUtils.hasText(customerDTO.getPhone());
        Assert.isTrue(hasEmail || hasPhone, "the email and the phone both are empty!");
        customerService.updateLoggedCustomer(customerDTO);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}