sync customer phone
This commit is contained in:
@@ -6,6 +6,7 @@ import com.petshop.backend.dto.customer.CustomerResponse;
|
||||
import com.petshop.backend.entity.Customer;
|
||||
import com.petshop.backend.exception.ResourceNotFoundException;
|
||||
import com.petshop.backend.repository.CustomerRepository;
|
||||
import com.petshop.backend.repository.UserRepository;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -15,9 +16,11 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
public class CustomerService {
|
||||
|
||||
private final CustomerRepository customerRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
public CustomerService(CustomerRepository customerRepository) {
|
||||
public CustomerService(CustomerRepository customerRepository, UserRepository userRepository) {
|
||||
this.customerRepository = customerRepository;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public Page<CustomerResponse> getAllCustomers(String query, Pageable pageable) {
|
||||
@@ -45,6 +48,7 @@ public class CustomerService {
|
||||
customer.setPhone(request.getPhone());
|
||||
|
||||
customer = customerRepository.save(customer);
|
||||
syncLinkedUser(customer);
|
||||
return mapToResponse(customer);
|
||||
}
|
||||
|
||||
@@ -59,6 +63,7 @@ public class CustomerService {
|
||||
customer.setPhone(request.getPhone());
|
||||
|
||||
customer = customerRepository.save(customer);
|
||||
syncLinkedUser(customer);
|
||||
return mapToResponse(customer);
|
||||
}
|
||||
|
||||
@@ -86,4 +91,16 @@ public class CustomerService {
|
||||
customer.getUpdatedAt()
|
||||
);
|
||||
}
|
||||
|
||||
private void syncLinkedUser(Customer customer) {
|
||||
if (customer.getUserId() == null) {
|
||||
return;
|
||||
}
|
||||
userRepository.findById(customer.getUserId()).ifPresent(user -> {
|
||||
user.setEmail(customer.getEmail());
|
||||
user.setPhone(customer.getPhone());
|
||||
user.setFullName((customer.getFirstName() + " " + customer.getLastName()).trim());
|
||||
userRepository.save(user);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user