This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package ru.soune.nocopy.exception;
|
||||
|
||||
public class CompanyAlreadyExist extends RuntimeException {
|
||||
public CompanyAlreadyExist(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import ru.soune.nocopy.dto.register.RegRequest;
|
||||
import ru.soune.nocopy.entity.referral.Referral;
|
||||
import ru.soune.nocopy.entity.user.AuthToken;
|
||||
import ru.soune.nocopy.entity.user.EmailVerificationToken;
|
||||
import ru.soune.nocopy.exception.CompanyAlreadyExist;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.exception.ValidationException;
|
||||
import ru.soune.nocopy.handler.validator.RegRequestValidator;
|
||||
@@ -45,7 +46,7 @@ public class RegRequestHandler implements RequestHandler {
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws ValidationException {
|
||||
RegRequest regRequest = objectMapper.convertValue(request.getMessageBody(), RegRequest.class);
|
||||
|
||||
try {
|
||||
if (userRepository.existsByEmail(regRequest.getEmail()) || userRepository.existsByPhone(regRequest.getPhone())) {
|
||||
RegAnswer regAnswer = new RegAnswer();
|
||||
regAnswer.setFieldErrors(Arrays.asList(Map.of("email", regRequest.getEmail())));
|
||||
@@ -88,6 +89,7 @@ public class RegRequestHandler implements RequestHandler {
|
||||
}
|
||||
|
||||
boolean verified = mailVerified.equals("true");
|
||||
|
||||
AuthToken authToken = authService.register(regRequest, verified);
|
||||
EmailVerificationToken emailToken = emailService.createEmailVerificationToken(authToken);
|
||||
RegAnswer regAnswer = new RegAnswer();
|
||||
@@ -124,5 +126,11 @@ public class RegRequestHandler implements RequestHandler {
|
||||
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(), regAnswer);
|
||||
|
||||
} catch (CompanyAlreadyExist e) {
|
||||
return new BaseResponse(request.getMsgId(), MessageCode.COMPANY_ALREADY_EXISTS.getCode(),
|
||||
MessageCode.COMPANY_ALREADY_EXISTS.getDescription(), Map.of(
|
||||
"inn", regRequest.getInn()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import ru.soune.nocopy.entity.user.AuthToken;
|
||||
import ru.soune.nocopy.entity.user.Permission;
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.entity.user.UserNotActive;
|
||||
import ru.soune.nocopy.exception.CompanyAlreadyExist;
|
||||
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
||||
import ru.soune.nocopy.exception.NotValidFieldException;
|
||||
import ru.soune.nocopy.repository.*;
|
||||
@@ -61,7 +62,11 @@ public class AuthService {
|
||||
AccountType accountType = AccountType.valueOf(registerRequest.getAccountType().toUpperCase());
|
||||
|
||||
if (accountType == AccountType.B2B && registerRequest.getCompanyName() != null) {
|
||||
try {
|
||||
createCompanyAccount(registerRequest, user);
|
||||
} catch (CompanyAlreadyExist exist) {
|
||||
throw new CompanyAlreadyExist("Company already exist");
|
||||
}
|
||||
} else if (accountType == AccountType.B2C && registerRequest.getCompanyName() == null) {
|
||||
createIndividualAccount(user);
|
||||
} else {
|
||||
@@ -173,7 +178,7 @@ public class AuthService {
|
||||
Optional<Company> byInn = companyRepository.findByInn(request.getInn());
|
||||
|
||||
if (byInn.isPresent()) {
|
||||
throw new IllegalStateException("Company was already created");
|
||||
throw new CompanyAlreadyExist("Company was already created");
|
||||
}
|
||||
|
||||
Company company = new Company();
|
||||
|
||||
Reference in New Issue
Block a user