account-service http://jira.mfd.msk:8088/browse/CLS-231 валидация
This commit is contained in:
parent
299ecd9b26
commit
59fbe93045
4 changed files with 207 additions and 8 deletions
|
|
@ -0,0 +1,64 @@
|
|||
package ru.spcex.clearing.account.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.account.AccountBalance;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.clearing.classes.statics.data.company.CompanySymbols;
|
||||
import ru.spcex.clearing.account.validation.AccountValidationRule;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest;
|
||||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.imdg.validation.ImdgValidationContext;
|
||||
import ru.spcex.platform.utils.validation.IValidator;
|
||||
import ru.spcex.platform.utils.validation.ValidatorImpl;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Configuration
|
||||
public class ValidationConfig {
|
||||
private final Map<String, Imdg<? extends SpcexObjectBase>> imdgs;
|
||||
|
||||
public ValidationConfig(ImdgProvider imdgProvider) {
|
||||
this.imdgs = new HashMap<>();
|
||||
BiConsumer<String, Class<? extends SpcexObjectBase>> addImdg = (s, aClass) -> imdgs.put(s, imdgProvider.getImdg(s, aClass));
|
||||
addImdg.accept(IMDGDistributedNames.Map_Account, Account.class);
|
||||
addImdg.accept(IMDGDistributedNames.Map_Company, Company.class);
|
||||
addImdg.accept(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class);
|
||||
addImdg.accept(IMDGDistributedNames.Map_AccountBalance, AccountBalance.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* чтобы во всех валидаторах был один экземпляр Imdg
|
||||
*/
|
||||
private Imdg<?> getImdg(String key) {
|
||||
return imdgs.get(key);
|
||||
}
|
||||
|
||||
@Bean("bankAccountNewRequestValidator")
|
||||
public Function<BankAccountNewRequest, IValidator> bankAccountNewRequestValidator() {
|
||||
return bankAccountNewRequest -> {
|
||||
ImdgValidationContext<BankAccountNewRequest> context = new ImdgValidationContext<>();
|
||||
context.setValidatedObject(bankAccountNewRequest);
|
||||
Consumer<String> addImdg = (s) -> context.addImdg(s, getImdg(s));
|
||||
addImdg.accept(IMDGDistributedNames.Map_Account);
|
||||
addImdg.accept(IMDGDistributedNames.Map_Company);
|
||||
|
||||
return new ValidatorImpl<>(context,
|
||||
AccountValidationRule.RequiredFields,
|
||||
AccountValidationRule.RubRequiredFields,
|
||||
AccountValidationRule.AccountIsNew,
|
||||
AccountValidationRule.CompanyPresent
|
||||
);
|
||||
};
|
||||
//todo после слияния ветки CLR_51_57 переписать на использование DictionaryPresentRule, FieldRequiredRule
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -3,7 +3,12 @@ package ru.spcex.clearing.account.errors;
|
|||
import ru.spcex.platform.utils.enumeration.IEnumId;
|
||||
|
||||
public enum AccountError implements IEnumId {
|
||||
AccountAlreadyExist(5010L);
|
||||
WrongFieldValue(5004L),
|
||||
AccountAlreadyExist(5010L),
|
||||
CompanyNotFound(5013L),
|
||||
CompanyNotActive(5014L),
|
||||
|
||||
;
|
||||
private final Long id;
|
||||
|
||||
AccountError(Long id) {
|
||||
|
|
|
|||
|
|
@ -27,9 +27,11 @@ import ru.spcex.platform.imdg.api.Imdg;
|
|||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
||||
import ru.spcex.platform.utils.validation.IValidator;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Service
|
||||
public class BankAccountService extends QueueConsumer implements InitializingBean {
|
||||
|
|
@ -39,16 +41,26 @@ public class BankAccountService extends QueueConsumer implements InitializingBea
|
|||
private final Imdg<Relation> relationMap;
|
||||
private final IMessageResolver messageResolver;
|
||||
|
||||
// private final Imdg<User> userImdg;
|
||||
// private final Imdg<UserRoleSession> userRoleSessionImdg;
|
||||
private final Function<BankAccountNewRequest, IValidator> bankAccountNewRequestValidator;
|
||||
|
||||
@Autowired
|
||||
public BankAccountService(Consumer<String, Object> kafkaQueue,
|
||||
Producer<String, Object> kafkaProducer,
|
||||
ImdgProvider imdgProvider,
|
||||
IMessageResolver messageResolver) {
|
||||
IMessageResolver messageResolver,
|
||||
|
||||
Function<BankAccountNewRequest, IValidator> bankAccountNewRequestValidator) {
|
||||
super(kafkaQueue, kafkaProducer);
|
||||
this.bankAccountMap = imdgProvider.getImdg(IMDGDistributedNames.Map_BankAccount, BankAccount.class);
|
||||
this.accountMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
||||
this.relationMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Relation, Relation.class);
|
||||
this.messageResolver = messageResolver;
|
||||
|
||||
// this.userImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_User, User.class);
|
||||
// this.userRoleSessionImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_UserRoleSession, UserRoleSession.class);
|
||||
this.bankAccountNewRequestValidator = bankAccountNewRequestValidator;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -65,14 +77,29 @@ public class BankAccountService extends QueueConsumer implements InitializingBea
|
|||
init();
|
||||
}
|
||||
|
||||
// protected Optional<EnumMessage> checkUserRole(Long userId) {
|
||||
// User user = userImdg.getSingleObjectByID(userId);
|
||||
// if (user == null) {
|
||||
// return Optional.of(new EnumMessage(AccountError.userNotFound(5007)));
|
||||
// }
|
||||
// UserRoleSession role=userRoleSessionImdg.getSingleObjectBySql("userId="+userId+" and userRoleSessions.userRole='ADMN'");
|
||||
// if (role == null) {
|
||||
// return Optional.of(new EnumMessage(AccountError.userNotFound(5001))); // Нет прав на проведение данной операции».
|
||||
// }
|
||||
// return Optional.empty();
|
||||
// }
|
||||
|
||||
private RequestInfoUpdate bankAccountNew(BaseRequest<BankAccountNewRequest> userRequest) {
|
||||
BankAccountNewRequest req = userRequest.getRequestPayload();
|
||||
|
||||
Collection<Account> accountsByKey =
|
||||
accountMap.getCollectionObjectsBySQL(String.format("account = %s", req.account));
|
||||
if (!accountsByKey.isEmpty()) {
|
||||
String errorMsg = messageResolver.resolve(new EnumMessage(AccountError.AccountAlreadyExist));
|
||||
log.error("cannot process MoneyMarketSecurityNewRequest id={}: {}", userRequest.getId(), errorMsg);
|
||||
// // проверка прав
|
||||
// checkUserRole(req.getUserId());
|
||||
// валидация
|
||||
IValidator validator = bankAccountNewRequestValidator.apply(req);
|
||||
Optional<EnumMessage> error = validator.tillFirstError();
|
||||
if (error.isPresent()) {
|
||||
log.warn("BankAccountNewRequest[{}] validation error: {}", userRequest.getId(), error.get());
|
||||
String errorMsg = messageResolver.resolve(error.get());
|
||||
return new RequestInfoUpdate()
|
||||
.setId(userRequest.getId())
|
||||
.setStatus(Status.Error)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,103 @@
|
|||
package ru.spcex.clearing.account.validation;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.spcex.clearing.account.errors.AccountError;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewRequest;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.validation.ImdgValidationContext;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.validation.IValidationRule;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public enum AccountValidationRule implements IValidationRule<ImdgValidationContext<BankAccountNewRequest>> {
|
||||
|
||||
CompanyPresent() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<BankAccountNewRequest> context) {
|
||||
BankAccountNewRequest bankAccount = context.getValidatedObject();
|
||||
Imdg<Company> companyImdg = context.obtainMap(IMDGDistributedNames.Map_Company, Company.class);
|
||||
Company company = companyImdg.getSingleObjectByID(bankAccount.getCompanyId());
|
||||
if (company == null) {
|
||||
return of(AccountError.CompanyNotFound);
|
||||
}
|
||||
if (!Status.Active.equalsByKey(company.getWorkflowStatus())) {
|
||||
return of(AccountError.CompanyNotActive);
|
||||
}
|
||||
// context.storeObject(ValidationStored.Company, company);
|
||||
return empty();
|
||||
}
|
||||
},
|
||||
AccountIsNew() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<BankAccountNewRequest> context) {
|
||||
BankAccountNewRequest bankAccountRequest = context.getValidatedObject();
|
||||
Imdg<Account> accountImdg = context.obtainMap(IMDGDistributedNames.Map_AccountBalance, Account.class);
|
||||
Account account = accountImdg.getSingleObjectByFieldValues(Map.of("account", bankAccountRequest.getAccount(),
|
||||
"accountStatus", Status.Active.getKey()));
|
||||
if (account != null) {
|
||||
return of(AccountError.AccountAlreadyExist);
|
||||
}
|
||||
return empty();
|
||||
}
|
||||
},
|
||||
|
||||
RequiredFields() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<BankAccountNewRequest> context) {
|
||||
BankAccountNewRequest accountReq = context.getValidatedObject();
|
||||
if (StringUtils.isEmpty(accountReq.getCurrency())) {
|
||||
return of(AccountError.WrongFieldValue, "Currency");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getBankIdentificationCode())) {
|
||||
return of(AccountError.WrongFieldValue, "bankIdentificationCode");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getBankName())) {
|
||||
return of(AccountError.WrongFieldValue, "bankName");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getAccount())) {
|
||||
return of(AccountError.WrongFieldValue, "account");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getDestination())) {
|
||||
return of(AccountError.WrongFieldValue, "destination");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getCompanyId())) {
|
||||
return of(AccountError.WrongFieldValue, "companyId");
|
||||
}
|
||||
|
||||
return empty();
|
||||
}
|
||||
},
|
||||
|
||||
RubRequiredFields() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<BankAccountNewRequest> context) {
|
||||
BankAccountNewRequest accountReq = context.getValidatedObject();
|
||||
if ("RUB".equals(accountReq.getCurrency())) {
|
||||
if (StringUtils.isEmpty(accountReq.getCorrespondentAccount())) {
|
||||
return of(AccountError.WrongFieldValue, "correspondentAccount");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getCorrespondentAccountName())) {
|
||||
return of(AccountError.WrongFieldValue, "correspondentAccountName");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getTaxpayerIdentificationNumber())) {
|
||||
return of(AccountError.WrongFieldValue, "taxpayerIdentificationNumber");
|
||||
}
|
||||
if (StringUtils.isEmpty(accountReq.getTaxRegistrationReasonCode())) {
|
||||
return of(AccountError.WrongFieldValue, "taxRegistrationReasonCode");
|
||||
}
|
||||
}
|
||||
return empty();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public String ruleName() {
|
||||
return "AccountValidationRule." + name();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue