Clean up customer accounts
This commit is contained in:
@@ -60,15 +60,7 @@ public class CustomerService {
|
||||
customer.setEmail(request.getEmail());
|
||||
|
||||
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));
|
||||
User user = createLinkedUser(customer);
|
||||
|
||||
Customer linkedCustomer = userBusinessLinkageService.ensureLinkedCustomer(user);
|
||||
syncLinkedUser(linkedCustomer);
|
||||
@@ -93,9 +85,14 @@ public class CustomerService {
|
||||
|
||||
@Transactional
|
||||
public void deleteCustomer(Long id) {
|
||||
if (!customerRepository.existsById(id)) {
|
||||
throw new ResourceNotFoundException("Customer not found with id: " + id);
|
||||
Customer customer = customerRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Customer not found with id: " + id));
|
||||
|
||||
if (customer.getUserId() != null && userRepository.existsById(customer.getUserId())) {
|
||||
userRepository.deleteById(customer.getUserId());
|
||||
return;
|
||||
}
|
||||
|
||||
customerRepository.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user