Clean up customer accounts

This commit is contained in:
2026-03-29 22:37:18 -06:00
parent 3fbb108646
commit 5d3efa0af5
2 changed files with 38 additions and 11 deletions

View File

@@ -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);
}