Fix user linking
This commit is contained in:
@@ -13,6 +13,9 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import static org.springframework.http.HttpStatus.CONFLICT;
|
||||
|
||||
@Service
|
||||
public class CustomerService {
|
||||
@@ -57,6 +60,12 @@ public class CustomerService {
|
||||
customer = customerRepository.save(customer);
|
||||
Customer savedCustomer = customer;
|
||||
User user = userRepository.findByEmail(savedCustomer.getEmail())
|
||||
.map(existing -> {
|
||||
if (existing.getRole() != User.Role.CUSTOMER) {
|
||||
throw new ResponseStatusException(CONFLICT, "Email already exists for a different account type");
|
||||
}
|
||||
return existing;
|
||||
})
|
||||
.orElseGet(() -> createLinkedUser(savedCustomer));
|
||||
|
||||
Customer linkedCustomer = userBusinessLinkageService.ensureLinkedCustomer(user);
|
||||
|
||||
Reference in New Issue
Block a user