dev add rest for company, add link for users , update reg users
Test Workflow / test (push) Successful in 2s
Test Workflow / test (push) Successful in 2s
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
package ru.soune.nocopy.service;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import ru.soune.nocopy.entity.company.Company;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.exception.ValidationException;
|
||||
import ru.soune.nocopy.exception.ResourceNotFoundException;
|
||||
import ru.soune.nocopy.handler.validator.RegRequestValidator;
|
||||
import ru.soune.nocopy.repository.CompanyRepository;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class CompanyService {
|
||||
|
||||
private final CompanyRepository companyRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final RegRequestValidator regRequestValidator;
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Company getCompanyById(String id) {
|
||||
return companyRepository.findById(id)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Company not found with id: " + id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Company getCompanyByName(String companyName) {
|
||||
return companyRepository.findByCompanyName(companyName)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("Company not found with name: " + companyName));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Company> getAllCompanies() {
|
||||
return companyRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Page<Company> getAllCompanies(Pageable pageable) {
|
||||
return companyRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Company> searchCompaniesByName(String name) {
|
||||
return companyRepository.findByCompanyNameContainingIgnoreCase(name);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Company updateCompany(String id, String companyName, String phone, String address, String email) {
|
||||
Company company = getCompanyById(id);
|
||||
|
||||
if (companyName != null) {
|
||||
company.setCompanyName(companyName);
|
||||
}
|
||||
if (phone != null) {
|
||||
company.setPhone(phone);
|
||||
}
|
||||
if (address != null) {
|
||||
company.setAddress(address);
|
||||
}
|
||||
if (email != null) {
|
||||
company.setEmail(email);
|
||||
}
|
||||
|
||||
company.setUpdateDate(LocalDateTime.now());
|
||||
|
||||
validateCompanyData(company);
|
||||
|
||||
return companyRepository.save(company);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Company addCompany(String companyName, String phone, String address, String email) {
|
||||
Company company = new Company();
|
||||
company.setCompanyName(companyName);
|
||||
company.setPhone(phone);
|
||||
company.setAddress(address);
|
||||
company.setEmail(email);
|
||||
company.setRegisterDate(LocalDateTime.now());
|
||||
|
||||
validateCompanyData(company);
|
||||
|
||||
return companyRepository.save(company);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteCompany(String id) {
|
||||
List<User> users = userRepository.findByCompanyId(id);
|
||||
users.forEach(user -> {
|
||||
user.setCompany(null);
|
||||
userRepository.save(user);
|
||||
});
|
||||
|
||||
companyRepository.deleteById(id);
|
||||
log.info("Company deleted: {}", id);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<User> getCompanyUsers(String companyId) {
|
||||
return userRepository.findByCompanyId(companyId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public long countCompanyUsers(String companyId) {
|
||||
return userRepository.countByCompanyId(companyId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addUserToCompany(String companyId, Long userId) {
|
||||
Company company = getCompanyById(companyId);
|
||||
User user = userRepository.findById(userId)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + userId));
|
||||
|
||||
if (user.getCompany() != null) {
|
||||
throw new IllegalArgumentException("User already belongs to another company");
|
||||
}
|
||||
|
||||
user.setCompany(company);
|
||||
userRepository.save(user);
|
||||
|
||||
log.info("User {} added to company {}", userId, companyId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addUserToCompany(Company company, User user) {
|
||||
if (user.getCompany() != null) {
|
||||
throw new IllegalArgumentException("User already belongs to another company");
|
||||
}
|
||||
|
||||
user.setCompany(company);
|
||||
userRepository.save(user);
|
||||
|
||||
log.info("User {} added to company {}", user.getId(), company.getId());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteUserFromCompany(User user) {
|
||||
user.setCompany(null);
|
||||
userRepository.save(user);
|
||||
|
||||
log.info("User {} removed from company", user.getId());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteUserFromCompany(Long userId) {
|
||||
User user = userRepository.findById(userId)
|
||||
.orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + userId));
|
||||
|
||||
user.setCompany(null);
|
||||
userRepository.save(user);
|
||||
|
||||
log.info("User {} removed from company", userId);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public boolean companyExistsByName(String companyName) {
|
||||
return companyRepository.existsByCompanyName(companyName);
|
||||
}
|
||||
|
||||
private void validateCompanyData(Company company) {
|
||||
BindingResult bindingResult = new BeanPropertyBindingResult(company, "company");
|
||||
|
||||
if (company.getCompanyName() != null) {
|
||||
regRequestValidator.validateCompanyName(company.getCompanyName(), bindingResult);
|
||||
}
|
||||
if (company.getEmail() != null) {
|
||||
regRequestValidator.validateEmail(company.getEmail(), bindingResult);
|
||||
}
|
||||
if (company.getPhone() != null) {
|
||||
regRequestValidator.validatePhone(company.getPhone(), bindingResult);
|
||||
}
|
||||
|
||||
if (bindingResult.hasErrors()) {
|
||||
Map<String, String> fieldErrors = bindingResult.getFieldErrors()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(
|
||||
FieldError::getField,
|
||||
fieldError -> fieldError.getDefaultMessage() != null
|
||||
? fieldError.getDefaultMessage()
|
||||
: "Validation error"));
|
||||
|
||||
throw new ValidationException(bindingResult, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -372,7 +372,7 @@ public class FileEntityService {
|
||||
.fileName(fileEntity.getOriginalFileName())
|
||||
.ownerName(user.getFullName())
|
||||
.ownerEmail(user.getEmail())
|
||||
.ownerCompany(user.getCompany())
|
||||
.ownerCompany(user.getCompany().getCompanyName())
|
||||
//TODO fix after add logic for check for protect file
|
||||
.checksCount(0)
|
||||
.fileUploadDate(fileEntity.getCreatedAt())
|
||||
|
||||
@@ -5,14 +5,17 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.dto.*;
|
||||
import ru.soune.nocopy.dto.register.AccountType;
|
||||
import ru.soune.nocopy.dto.register.LoginAnswer;
|
||||
import ru.soune.nocopy.dto.register.LoginRequest;
|
||||
import ru.soune.nocopy.dto.register.RegRequest;
|
||||
import ru.soune.nocopy.entity.company.Company;
|
||||
import ru.soune.nocopy.entity.user.AuthToken;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||
import ru.soune.nocopy.repository.CompanyRepository;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
import ru.soune.nocopy.service.file.CheckCounterService;
|
||||
|
||||
@@ -30,6 +33,8 @@ public class AuthService {
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
private final CompanyRepository companyRepository;
|
||||
|
||||
private final CheckCounterService checkCounterService;
|
||||
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
@@ -43,8 +48,14 @@ public class AuthService {
|
||||
user.setActive(true);
|
||||
user.setEmailVerified(true);
|
||||
|
||||
if (registerRequest.getCompanyName() != null) {
|
||||
user.setCompany(registerRequest.getCompanyName());
|
||||
if (registerRequest.getCompanyName() != null &&
|
||||
AccountType.valueOf(registerRequest.getAccountType().toUpperCase()).equals(AccountType.B2B)) {
|
||||
Company company = new Company();
|
||||
company.setCompanyName(registerRequest.getCompanyName());
|
||||
|
||||
companyRepository.save(company);
|
||||
|
||||
user.setCompany(company);
|
||||
}
|
||||
|
||||
if (registerRequest.getPhone() != null) {
|
||||
|
||||
@@ -61,10 +61,6 @@ public class UserService {
|
||||
user.setFullName(request.getFullName());
|
||||
}
|
||||
|
||||
if (request.getCompany() != null) {
|
||||
user.setCompany(request.getCompany());
|
||||
}
|
||||
|
||||
if (request.getEmail() != null && !request.getEmail().equals(user.getEmail())) {
|
||||
if (userRepository.existsByEmail(request.getEmail())) {
|
||||
throw new InvalidUserEmail("Email already exists: " + request.getEmail());
|
||||
@@ -87,7 +83,7 @@ public class UserService {
|
||||
return UserDTO.builder()
|
||||
.fullName(user.getFullName())
|
||||
.email(user.getEmail())
|
||||
.company(user.getCompany())
|
||||
.company(user.getCompany().getCompanyName())
|
||||
.phone(user.getPhone())
|
||||
.genderType(user.getGenderType())
|
||||
.birthday(user.getBirthday())
|
||||
|
||||
Reference in New Issue
Block a user