保誠-保戶業務員媒合平台
Jack
2021-11-23 e7ff6eaf7ffd713d102f596b4e5d906504160ff8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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");
        }
        
    }
}