Fix user linking

This commit is contained in:
2026-03-29 21:52:45 -06:00
parent 1bab36f727
commit 24041f4242
3 changed files with 32 additions and 0 deletions

View File

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

View File

@@ -41,11 +41,13 @@ WHERE e.user_id IS NULL
UPDATE customer c
JOIN users u ON u.email = c.email
AND u.role = 'CUSTOMER'
SET c.user_id = u.id
WHERE c.user_id IS NULL;
UPDATE employee e
JOIN users u ON u.email = e.email
AND u.role IN ('STAFF', 'ADMIN')
SET e.user_id = u.id
WHERE e.user_id IS NULL;