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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user