fix validation
This commit is contained in:
parent
95ad4837e1
commit
c51d22a615
14 changed files with 477 additions and 359 deletions
|
|
@ -1,40 +1,29 @@
|
|||
package ru.spcex.clearing.account.config.validation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.account.ClientCode;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.clearing.classes.statics.data.registry.TradingClearingRegistry;
|
||||
import ru.clearing.platform.dictionary.ServiceStatusDictionary;
|
||||
import ru.spcex.clearing.account.errors.AccountError;
|
||||
import ru.spcex.clearing.account.validation.ClientCodeStoreObjects;
|
||||
import ru.spcex.clearing.account.validation.BackendClientCodeValidationRule;
|
||||
import ru.spcex.clearing.account.validation.GatewayClientCodeValidationRule;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.ClientCodeNewRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.ClientCodeUpdateRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.TkrAccount;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.gateway.MoneyAccountMsgRequest;
|
||||
import ru.spcex.clearing.validation.common.rules.DictionaryPresentRule;
|
||||
import ru.spcex.clearing.validation.common.rules.FieldRequiredRule;
|
||||
import ru.spcex.clearing.validation.common.rules.IdPresentRule;
|
||||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
import ru.spcex.platform.enumeration.CurrencyCode;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
||||
import ru.spcex.platform.imdg.validation.ImdgValidationContext;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.validation.IValidationRule;
|
||||
import ru.spcex.platform.utils.validation.IValidator;
|
||||
import ru.spcex.platform.utils.validation.ValidatorImpl;
|
||||
|
||||
|
|
@ -50,6 +39,7 @@ public class ClientCodeValidationConfig {
|
|||
addImdg.accept(IMDGDistributedNames.Map_Company);
|
||||
addImdg.accept(IMDGDistributedNames.Map_Account);
|
||||
addImdg.accept(IMDGDistributedNames.Map_TradingClearingRegistry);
|
||||
addImdg.accept(IMDGDistributedNames.Map_TradingClearingRegistryList);
|
||||
addImdg.accept(IMDGDistributedNames.Map_ServiceStatusDictionary);
|
||||
return new ValidatorImpl<>(context,
|
||||
FieldRequiredRule.instance("companyId", ClientCodeNewRequest::getCompanyId, AccountError.RequiredFieldEmpty),
|
||||
|
|
@ -80,11 +70,9 @@ public class ClientCodeValidationConfig {
|
|||
acc -> Objects.equals(context.getValidatedObject().getCompanyId(), acc.getCompanyId())
|
||||
? null : AccountError.AccountNotFound
|
||||
),
|
||||
new ExistAllCurrencyAccountId<>("currencyAccountList",
|
||||
ClientCodeNewRequest::getCurrencyAccountList
|
||||
),
|
||||
|
||||
DictionaryPresentRule.instance("stauts",
|
||||
BackendClientCodeValidationRule.AllAccountsPresent,
|
||||
BackendClientCodeValidationRule.TcrIsNotPresentByAccount,
|
||||
DictionaryPresentRule.instance("status",
|
||||
ClientCodeNewRequest::getStatus,
|
||||
IMDGDistributedNames.Map_ServiceStatusDictionary,
|
||||
ServiceStatusDictionary.class,
|
||||
|
|
@ -106,7 +94,7 @@ public class ClientCodeValidationConfig {
|
|||
addImdg.accept(IMDGDistributedNames.Map_Account);
|
||||
addImdg.accept(IMDGDistributedNames.Map_TradingClearingRegistry);
|
||||
addImdg.accept(IMDGDistributedNames.Map_ServiceStatusDictionary);
|
||||
return new ValidatorImpl<ImdgValidationContext<ClientCodeUpdateRequest>>(context,
|
||||
return new ValidatorImpl<>(context,
|
||||
IdPresentRule.instance("id",
|
||||
ClientCodeUpdateRequest::getId,
|
||||
IMDGDistributedNames.Map_ClientCode,
|
||||
|
|
@ -143,10 +131,7 @@ public class ClientCodeValidationConfig {
|
|||
acc -> Objects.equals(context.getValidatedObject().getCompanyId(), acc.getCompanyId())
|
||||
? null : AccountError.AccountNotFound
|
||||
),
|
||||
new ExistAllCurrencyAccountId<>("currencyAccountList",
|
||||
ClientCodeUpdateRequest::getCurrencyAccountList
|
||||
),
|
||||
|
||||
BackendClientCodeValidationRule.AllAccountsPresent,
|
||||
DictionaryPresentRule.instance("stauts",
|
||||
ClientCodeUpdateRequest::getStatus,
|
||||
IMDGDistributedNames.Map_ServiceStatusDictionary,
|
||||
|
|
@ -166,7 +151,6 @@ public class ClientCodeValidationConfig {
|
|||
Consumer<String> addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s));
|
||||
addImdg.accept(IMDGDistributedNames.Map_ClientCode);
|
||||
return new ValidatorImpl<>(context,
|
||||
// FieldRequiredRule.instance("id", CommonDeleteRequest::getId, CompanyErrors.RequiredFieldEmpty),
|
||||
IdPresentRule.instance("id",
|
||||
CommonDeleteRequest::getId,
|
||||
IMDGDistributedNames.Map_ClientCode,
|
||||
|
|
@ -189,208 +173,12 @@ public class ClientCodeValidationConfig {
|
|||
addImdg.accept(IMDGDistributedNames.Map_TradingClearingRegistry);
|
||||
return new ValidatorImpl<>(context,
|
||||
FieldRequiredRule.instance("client_code", TkrAccount::getClientCode, AccountError.RequiredFieldEmpty),
|
||||
new ExistCompanyByTradingCode<>("trading_code",
|
||||
TkrAccount::getTradingCode),
|
||||
new ExistAccountByValue<>("money_account",
|
||||
TkrAccount::getMoneyAccounts),
|
||||
new ExistDepoAccountByValue<>("depo_account",
|
||||
TkrAccount::getDepoAccount),
|
||||
new ExistTcrByCode<>("tcr_code")
|
||||
GatewayClientCodeValidationRule.CompanyPresent,
|
||||
GatewayClientCodeValidationRule.AllAccountsPresent,
|
||||
GatewayClientCodeValidationRule.DepoAccountsPresent,
|
||||
GatewayClientCodeValidationRule.TcrIsNotPresent
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
public static class ExistAllCurrencyAccountId<R> implements IValidationRule<ImdgValidationContext<R>> {
|
||||
String fieldName;
|
||||
Function<R, List<Long>> idGetter;
|
||||
|
||||
public ExistAllCurrencyAccountId(String fieldName, Function<R, List<Long>> idGetter) {
|
||||
this.fieldName = fieldName;
|
||||
this.idGetter = idGetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<R> context) {
|
||||
R validatedObject = context.getValidatedObject();
|
||||
List<Long> ids = idGetter.apply(validatedObject);
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return empty(); // необязательное поле
|
||||
}
|
||||
Imdg<Account> imdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
|
||||
for (Long id : ids) {
|
||||
if (id == null) { // null значения в массиве не ожидаются
|
||||
return of(AccountError.RequiredFieldEmpty, fieldName);
|
||||
}
|
||||
Account byIdObject = imdg.getSingleObjectByID(id);
|
||||
if (byIdObject == null)
|
||||
return of(AccountError.AccountNotFound, id, fieldName);
|
||||
}
|
||||
|
||||
return empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ruleName() {
|
||||
return getClass().getSimpleName() + "{" + fieldName + "}";
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExistTcrByCode<R> implements IValidationRule<ImdgValidationContext<R>> {
|
||||
String fieldName;
|
||||
|
||||
public ExistTcrByCode(String fieldName) {
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<R> context) {
|
||||
Map<String, Account> accountByCurrency = context.getStoredObject(ClientCodeStoreObjects.MAPPED_TO_CURRENCY_ACCOUNTS);
|
||||
Account moneyAccount = accountByCurrency.get(CurrencyCode.RUB.getKey());
|
||||
if (moneyAccount == null) {
|
||||
return of(AccountError.AccountNotFound, fieldName);
|
||||
}
|
||||
Imdg<TradingClearingRegistry> imdg = context.obtainMap(IMDGDistributedNames.Map_TradingClearingRegistry,
|
||||
TradingClearingRegistry.class);
|
||||
|
||||
TradingClearingRegistry tcr = imdg.getFirstObjectByFieldValues(
|
||||
Map.of(
|
||||
"moneyAccountId", moneyAccount.getId(),
|
||||
"status", Status.Active.getKey()
|
||||
)
|
||||
);
|
||||
if (tcr != null) {
|
||||
return of(AccountError.TCR_ALREADY_EXIST_GTW, fieldName);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ruleName() {
|
||||
return getClass().getSimpleName() + "{" + fieldName + "}";
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExistCompanyByTradingCode<R> implements IValidationRule<ImdgValidationContext<R>> {
|
||||
String fieldName;
|
||||
Function<R, Long> tradingCodeGetter;
|
||||
|
||||
public ExistCompanyByTradingCode(String fieldName, Function<R, Long> tradingCodeGetter) {
|
||||
this.fieldName = fieldName;
|
||||
this.tradingCodeGetter = tradingCodeGetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<R> context) {
|
||||
R validatedObject = context.getValidatedObject();
|
||||
Long tradingCode = tradingCodeGetter.apply(validatedObject);
|
||||
if (tradingCode == null) {
|
||||
return of(AccountError.RequiredFieldEmpty, fieldName);
|
||||
}
|
||||
|
||||
Imdg<Company> companyImdg = context.obtainMap(IMDGDistributedNames.Map_Company, Company.class);
|
||||
Company company = companyImdg.getFirstObjectByFieldValues(
|
||||
Map.of("trading_code", String.valueOf(tradingCode))
|
||||
);
|
||||
if (company == null) {
|
||||
return of(AccountError.COMPANY_NOT_FOUND_GTW, fieldName);
|
||||
}
|
||||
|
||||
context.storeObject(ClientCodeStoreObjects.COMPANY, company);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ruleName() {
|
||||
return getClass().getSimpleName() + "{" + fieldName + "}";
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExistAccountByValue<R> implements IValidationRule<ImdgValidationContext<R>> {
|
||||
String fieldName;
|
||||
Function<R, List<MoneyAccountMsgRequest>> accountsListValueGetter;
|
||||
|
||||
public ExistAccountByValue(String fieldName, Function<R, List<MoneyAccountMsgRequest>> accountsListValueGetter) {
|
||||
this.fieldName = fieldName;
|
||||
this.accountsListValueGetter = accountsListValueGetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<R> context) {
|
||||
R validatedObject = context.getValidatedObject();
|
||||
List<MoneyAccountMsgRequest> accountList = accountsListValueGetter.apply(validatedObject);
|
||||
if (accountList == null || accountList.isEmpty()) {
|
||||
return of(AccountError.RequiredFieldEmpty, fieldName);
|
||||
}
|
||||
List<String> currencies = accountList.stream().map(MoneyAccountMsgRequest::getCurrCode).toList();
|
||||
if (!currencies.contains(CurrencyCode.RUB.getKey())) {
|
||||
return of(AccountError.RequiredFieldEmpty, fieldName);
|
||||
}
|
||||
Imdg<Account> imdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
|
||||
Map<String, Account> accountByCurrency = new HashMap<>();
|
||||
for (MoneyAccountMsgRequest moneyAccountMsg : accountList) {
|
||||
ImdgPredicateBuilder predicateBuilder = imdg.predicateBuilder();
|
||||
ImdgPredicate currencyPredicate;
|
||||
ImdgPredicate accountPredicate = predicateBuilder.equals("account", moneyAccountMsg.getAccount());
|
||||
if (moneyAccountMsg.getCurrCode().equals(CurrencyCode.RUB.getKey())) {
|
||||
currencyPredicate = predicateBuilder.or(
|
||||
predicateBuilder.equals("currency", CurrencyCode.RUB.getKey()),
|
||||
predicateBuilder.isNull("currency")
|
||||
);
|
||||
} else {
|
||||
currencyPredicate = predicateBuilder.equals("currency", moneyAccountMsg.getCurrCode());
|
||||
}
|
||||
Account account = imdg.getFirstObjectByPredicate(
|
||||
predicateBuilder.and(accountPredicate, currencyPredicate)
|
||||
);
|
||||
if (account == null) {
|
||||
return of(AccountError.ACCOUNT_NOT_FOUND_GTW, moneyAccountMsg.getAccount(), fieldName);
|
||||
}
|
||||
accountByCurrency.put(account.getCurrency(), account);
|
||||
}
|
||||
context.storeObject(ClientCodeStoreObjects.MAPPED_TO_CURRENCY_ACCOUNTS, accountByCurrency);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ruleName() {
|
||||
return getClass().getSimpleName() + "{" + fieldName + "}";
|
||||
}
|
||||
}
|
||||
|
||||
public static class ExistDepoAccountByValue<R> implements IValidationRule<ImdgValidationContext<R>> {
|
||||
String fieldName;
|
||||
Function<R, String> depoAccountGetter;
|
||||
|
||||
public ExistDepoAccountByValue(String fieldName, Function<R, String> depoAccountGetter) {
|
||||
this.fieldName = fieldName;
|
||||
this.depoAccountGetter = depoAccountGetter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<R> context) {
|
||||
R validatedObject = context.getValidatedObject();
|
||||
String depoAccountVal = depoAccountGetter.apply(validatedObject);
|
||||
if (!StringUtils.hasText(depoAccountVal)) {
|
||||
context.storeObject(ClientCodeStoreObjects.DEPO_ACCOUNT,Optional.empty());
|
||||
return empty();
|
||||
}
|
||||
|
||||
Imdg<Account> imdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
|
||||
Account depoAccount = imdg.getFirstObjectByFieldValues(
|
||||
Map.of("account", depoAccountVal)
|
||||
);
|
||||
if (depoAccount == null) {
|
||||
context.storeObject(ClientCodeStoreObjects.DEPO_ACCOUNT,Optional.empty());
|
||||
return of(AccountError.DEPO_ACCOUNT_NOT_FOUND_GTW, fieldName);
|
||||
}
|
||||
|
||||
context.storeObject(ClientCodeStoreObjects.DEPO_ACCOUNT, depoAccount);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ruleName() {
|
||||
return getClass().getSimpleName() + "{" + fieldName + "}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ public enum AccountError implements IErrorEnumId {
|
|||
COMPANY_NOT_FOUND_GTW(5030L),
|
||||
TCR_ALREADY_EXIST_GTW(5031L),
|
||||
DEPO_ACCOUNT_NOT_FOUND_GTW(5032L),
|
||||
TCR_LIST_ALREADY_EXIST_GTW(5033L),
|
||||
TradingClearingRegistryNotFound(3022L),
|
||||
CurrencyNotFound(1016L),
|
||||
;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
package ru.spcex.clearing.account.model;
|
||||
|
||||
import java.util.List;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.clearing.classes.statics.data.registry.TradingClearingRegistry;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
|
||||
public class ClientCodeBusiness {
|
||||
private Status code;
|
||||
private Company company;
|
||||
private Account moneyAccount;
|
||||
private Account depoAccount;
|
||||
private List<Account> currentAccounts;
|
||||
private TradingClearingRegistry tradingClearingRegistry;
|
||||
private Status status;
|
||||
|
||||
public ClientCodeBusiness(Status code,
|
||||
Company company, Account moneyAccount, Account depoAccount, List<Account> currentAccounts, TradingClearingRegistry tradingClearingRegistry, Status status) {
|
||||
this.code = code;
|
||||
this.company = company;
|
||||
this.moneyAccount = moneyAccount;
|
||||
this.depoAccount = depoAccount;
|
||||
this.currentAccounts = currentAccounts;
|
||||
this.tradingClearingRegistry = tradingClearingRegistry;
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
package ru.spcex.clearing.account.model;
|
||||
|
||||
import java.util.Optional;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.account.DepoAccount;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.enumeration.TradingClearingRegistryType;
|
||||
|
||||
public class TradingClearingRegistryBusiness {
|
||||
private Company company;
|
||||
private Account moneyAccount;
|
||||
private Status status;
|
||||
private Optional<DepoAccount> depoAccount;
|
||||
private Optional<TradingClearingRegistryType> tradingClearingRegistryType;
|
||||
|
||||
private TradingClearingRegistryBusiness() {
|
||||
this.depoAccount = Optional.empty();
|
||||
this.tradingClearingRegistryType = Optional.empty();
|
||||
}
|
||||
|
||||
public static TradingClearingRegistryBusiness builder() {
|
||||
return new TradingClearingRegistryBusiness();
|
||||
}
|
||||
|
||||
public TradingClearingRegistryBusiness company(Company company) {
|
||||
this.company = company;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TradingClearingRegistryBusiness moneyAccount(Account moneyAccount) {
|
||||
this.moneyAccount = moneyAccount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TradingClearingRegistryBusiness depoAccount(DepoAccount depoAccount) {
|
||||
this.depoAccount = Optional.of(depoAccount);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TradingClearingRegistryBusiness status(Status status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
public TradingClearingRegistryBusiness tradingClearingRegistryType(TradingClearingRegistryType type) {
|
||||
this.tradingClearingRegistryType = Optional.of(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TradingClearingRegistryBusiness build() {
|
||||
TradingClearingRegistryBusiness tradingClearingRegistryBusiness = new TradingClearingRegistryBusiness();
|
||||
tradingClearingRegistryBusiness.company = this.company;
|
||||
tradingClearingRegistryBusiness.status = this.status;
|
||||
tradingClearingRegistryBusiness.depoAccount = this.depoAccount;
|
||||
tradingClearingRegistryBusiness.moneyAccount = this.moneyAccount;
|
||||
tradingClearingRegistryBusiness.tradingClearingRegistryType = this.tradingClearingRegistryType;
|
||||
return tradingClearingRegistryBusiness;
|
||||
}
|
||||
|
||||
public Company getCompany() {
|
||||
return company;
|
||||
}
|
||||
|
||||
public Account getMoneyAccount() {
|
||||
return moneyAccount;
|
||||
}
|
||||
|
||||
public Optional<DepoAccount> getDepoAccount() {
|
||||
return depoAccount;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public Optional<TradingClearingRegistryType> getTradingClearingRegistryType() {
|
||||
return tradingClearingRegistryType;
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,6 @@ import java.util.Optional;
|
|||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import org.apache.kafka.clients.consumer.Consumer;
|
||||
import org.apache.kafka.clients.producer.Producer;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
|
|
@ -21,7 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.account.ClientCode;
|
||||
import ru.clearing.classes.statics.data.company.CompanySymbols;
|
||||
|
|
@ -34,11 +32,9 @@ import ru.spcex.clearing.platform.messaging.domain.Consts;
|
|||
import ru.spcex.clearing.platform.messaging.domain.cud.account.ClientCodeNewRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.ClientCodeUpdateRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.TkrAccount;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.TkrAccountsGatewayRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.gateway.MoneyAccountMsgRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.gateway.MoneyAccountMsgResponse;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.gateway.SendTkrRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.gateway.Tkr;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.registry.TradingClearingRegistryListNewRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.registry.TradingClearingRegistryListUpdateRequest;
|
||||
|
|
@ -52,16 +48,11 @@ import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
|||
import ru.spcex.clearing.util.security.UserRoleVerification;
|
||||
import ru.spcex.clearing.util.services.RequestHelper;
|
||||
import ru.spcex.clearing.validation.common.ValidationHelper;
|
||||
import ru.spcex.platform.enumeration.AccountType;
|
||||
import ru.spcex.platform.enumeration.CompanySymbol;
|
||||
import ru.spcex.platform.enumeration.CurrencyCode;
|
||||
import ru.spcex.platform.enumeration.TradingClearingRegistryType;
|
||||
import ru.spcex.platform.enumeration.WorkflowStatus;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.ImdgId;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
||||
import ru.spcex.platform.utils.log.ExceptionUtils;
|
||||
|
|
@ -131,9 +122,9 @@ public class ClientCodeService extends QueueConsumer implements InitializingBean
|
|||
public void afterPropertiesSet() {
|
||||
imdgProvider.waitAvailable();
|
||||
|
||||
callback(ClientCodeNewRequest.class)
|
||||
.setFunction(this::clientCodeNew)
|
||||
.forDestination(Consts.DESTINATION_CLIENT_CODE_NEW, callbacks::put);
|
||||
// callback(ClientCodeNewRequest.class)
|
||||
// .setFunction(this::clientCodeNew)
|
||||
// .forDestination(Consts.DESTINATION_CLIENT_CODE_NEW, callbacks::put);
|
||||
//note переехало в ClientCodeMessageListener[новая версия сервиса]
|
||||
// callback(TkrAccountsGatewayRequest.class)
|
||||
// .setFunction(this::clientCodeNewFromGateway)
|
||||
|
|
@ -181,109 +172,6 @@ public class ClientCodeService extends QueueConsumer implements InitializingBean
|
|||
return null;
|
||||
}
|
||||
|
||||
protected RequestInfoUpdate clientCodeNew(BaseRequest<ClientCodeNewRequest> userRequest) {
|
||||
return clientCodeNew0(userRequest, false);
|
||||
}
|
||||
|
||||
protected RequestInfoUpdate clientCodeNewFromGateway(BaseRequest<TkrAccountsGatewayRequest> tkrRequest) {
|
||||
TkrAccountsGatewayRequest tkr = tkrRequest.getRequestPayload();
|
||||
RequestInfoUpdate requestInfoUpdate = null;
|
||||
Tkr errorTkr = null;
|
||||
List<Tkr> successTkr = new ArrayList<>();
|
||||
//Сначала нужно проверить на валидности все присланные счета на добавление
|
||||
for (TkrAccount tkrAccount : tkr.getAccounts()) {
|
||||
BaseRequest requestForValidation = new BaseRequest();
|
||||
requestForValidation.setRequestPayload(tkrAccount);
|
||||
requestInfoUpdate = validationHelper.validateTillFirstError(requestForValidation,
|
||||
tkrAccountsGatewayValidator);
|
||||
if (requestInfoUpdate != null) {
|
||||
errorTkr = crateErrorTkrToGateway(tkrAccount, Optional.of(requestInfoUpdate.getMessage()));
|
||||
errorTkr.setMoneyAccounts(tkrAccount.getMoneyAccounts().stream()
|
||||
.map(this::convertAccountToResponse)
|
||||
.collect(Collectors.toList()));
|
||||
break;
|
||||
}
|
||||
Tkr tkrToGateway = crateErrorTkrToGateway(tkrAccount, Optional.empty());
|
||||
tkrToGateway.setMoneyAccounts(tkrAccount.getMoneyAccounts().stream()
|
||||
.map(this::convertAccountToResponse)
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
//Если хоть в одном была ошибка, берем ее отправляем
|
||||
if (errorTkr != null) {
|
||||
log.debug("Send request to gateway with error tkr cause: {}", requestInfoUpdate.getMessage());
|
||||
SendTkrRequest sendTkrRequest = new SendTkrRequest();
|
||||
sendTkrRequest.getTkrs().add(errorTkr);
|
||||
sendTkrRequest.getTkrs().addAll(successTkr);
|
||||
kafkaSender.sendRequestToQueue(Consts.ACCOUNTS_TO_GATEWAY, sendTkrRequest);
|
||||
return null;
|
||||
}
|
||||
|
||||
for (TkrAccount tkrAccount : tkr.getAccounts()) {
|
||||
ClientCodeNewRequest clientCodeNewRequest = new ClientCodeNewRequest();
|
||||
clientCodeNewRequest.setCode(tkrAccount.getClientCode());
|
||||
if (tkrAccount.getCompanyId() != null) {
|
||||
CompanySymbols companySymbols = companySymbolsImdg.getFirstObjectByFieldValues(
|
||||
Map.of(
|
||||
"companySymbol", CompanySymbol.UUID.getKey(),
|
||||
"companySymbolValue", tkrAccount.getCompanyId()
|
||||
)
|
||||
);
|
||||
if (companySymbols != null) {
|
||||
clientCodeNewRequest.setCompanyId(companySymbols.getCompanyId());
|
||||
}
|
||||
}
|
||||
if (StringUtils.hasText(tkrAccount.getDepoAccount())) {
|
||||
Account account = accountImdg.getFirstObjectByFieldValues(
|
||||
Map.of(
|
||||
"account", tkrAccount.getDepoAccount(),
|
||||
"accountType", AccountType.Depo.getKey()
|
||||
)
|
||||
);
|
||||
if (account != null) {
|
||||
clientCodeNewRequest.setDepoAccountId(account.getId());
|
||||
}
|
||||
}
|
||||
List<Long> currencyIds = new ArrayList<>();
|
||||
for (MoneyAccountMsgRequest moneyAccountMsg : tkrAccount.getMoneyAccounts()) {
|
||||
ImdgPredicateBuilder predicateBuilder = accountImdg.predicateBuilder();
|
||||
ImdgPredicate currencyPredicate;
|
||||
ImdgPredicate accountPredicate = predicateBuilder.equals("account", moneyAccountMsg.getAccount());
|
||||
if (moneyAccountMsg.getCurrCode().equals(CurrencyCode.RUB.getKey())) {
|
||||
currencyPredicate = predicateBuilder.or(
|
||||
predicateBuilder.equals("currency", CurrencyCode.RUB.getKey()),
|
||||
predicateBuilder.isNull("currency")
|
||||
);
|
||||
} else {
|
||||
currencyPredicate = predicateBuilder.equals("currency", moneyAccountMsg.getCurrCode());
|
||||
}
|
||||
Account account = accountImdg.getFirstObjectByPredicate(
|
||||
predicateBuilder.and(accountPredicate, currencyPredicate)
|
||||
);
|
||||
if (account == null) {
|
||||
log.warn("Account not found: currency: {}; account: {}", moneyAccountMsg.getCurrCode(),
|
||||
moneyAccountMsg.getAccount());
|
||||
continue;
|
||||
}
|
||||
if (!StringUtils.hasText(account.getCurrency()) || CurrencyCode.RUB.equalsByKey(account.getCurrency())) {
|
||||
clientCodeNewRequest.setMoneyAccountId(account.getId());
|
||||
} else {
|
||||
currencyIds.add(account.getId());
|
||||
}
|
||||
}
|
||||
if (!currencyIds.isEmpty()) {
|
||||
clientCodeNewRequest.setCurrencyAccountList(currencyIds);
|
||||
}
|
||||
BaseRequest<ClientCodeNewRequest> request = new BaseRequest<>();
|
||||
request.setRequestPayload(clientCodeNewRequest);
|
||||
requestInfoUpdate = clientCodeNew0(request, false);
|
||||
if (requestInfoUpdate != null) {
|
||||
log.debug("processing result is: {}", requestInfoUpdate.getMessage());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected RequestInfoUpdate clientCodeNew0(BaseRequest<ClientCodeNewRequest> userRequest, boolean fromTCRList) {
|
||||
log.debug("ClientCodeNewRequest received {}", userRequest.getId());
|
||||
|
||||
|
|
|
|||
|
|
@ -75,6 +75,41 @@ public class ClientCodeFacade implements IClearingFacade {
|
|||
}
|
||||
}
|
||||
|
||||
// public void createClientCode(ClientCodeBusiness businessRequest) {
|
||||
// log.trace("Start process creating new client code");
|
||||
// TradingClearingRegistry tradingClearingRegistry;
|
||||
// {
|
||||
// log.debug("Trading Clearing Registry is not exist, creating...");
|
||||
// TradingClearingRegistryBusiness creationTcrRequest = new TradingClearingRegistryBusiness();
|
||||
// creationTcrRequest.setCompanyId(request.getCompanyId());
|
||||
// creationTcrRequest.setMoneyAccountId(request.getMoneyAccountId());
|
||||
// creationTcrRequest.setDepoAccountId(request.getDepoAccountId());
|
||||
// creationTcrRequest.setTradingClearingRegistryType(TradingClearingRegistryType.Client_B.getKey());
|
||||
// tradingClearingRegistry = tradingClearingRegistryFacade.createTradingClearingRegistry(creationTcrRequest, null);
|
||||
// }
|
||||
// {
|
||||
// if (request.getCurrencyAccountList() != null && !request.getCurrencyAccountList().isEmpty()) {
|
||||
// TradingClearingRegistryListNewRequest tcrListNew = new TradingClearingRegistryListNewRequest();
|
||||
// tcrListNew.setTradingClearingRegistryId(tradingClearingRegistry.getId());
|
||||
// tcrListNew.setCurrencyAccountList(request.getCurrencyAccountList());
|
||||
// List<Long> tkrListIds = tradingClearingRegistryListFacade.createTradingClearingRegistryList(tcrListNew, null);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// {
|
||||
// ClientCode clientCode = new ClientCode();
|
||||
// clientCode.setCreated(Instant.now());
|
||||
// clientCode.setUpdated(clientCode.getCreated());
|
||||
//
|
||||
// clientCode.setCompanyId(request.getCompanyId());
|
||||
// clientCode.setCode(request.getCode());
|
||||
// clientCode.setMoneyAccountId(request.getMoneyAccountId());
|
||||
// clientCode.setDepoAccountId(request.getDepoAccountId());
|
||||
// clientCode.setStatus(request.getStatus());
|
||||
// clientCodeImdg.insert(clientCode);
|
||||
// log.debug("successfully processed, new clientCode id {}", clientCode.getId());
|
||||
// }
|
||||
// }
|
||||
public void lock() {
|
||||
// tradingClearingRegistryFacade.lock()
|
||||
// tradingClearingRegistryListFacade.lock()
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import ru.clearing.classes.statics.data.account.ClearingAccount;
|
|||
import ru.clearing.classes.statics.data.account.DepoAccount;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.clearing.classes.statics.data.registry.TradingClearingRegistry;
|
||||
import ru.spcex.clearing.account.model.TradingClearingRegistryBusiness;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.Consts;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.clearing.CreateRegistryRequest;
|
||||
|
|
@ -103,6 +104,66 @@ public class TradingClearingRegistryFacade {
|
|||
return tradingClearingRegistry;
|
||||
}
|
||||
|
||||
public TradingClearingRegistry createTradingClearingRegistry(TradingClearingRegistryBusiness businessRequest) {
|
||||
Long id = tradingClearingRegistryImdg.nextIDSequenceFor();
|
||||
TradingClearingRegistry tradingClearingRegistry = new TradingClearingRegistry();
|
||||
tradingClearingRegistry.setId(id);
|
||||
tradingClearingRegistry.setCompanyId(businessRequest.getCompany().getId());
|
||||
tradingClearingRegistry.setMoneyAccountId(businessRequest.getMoneyAccount().getId());
|
||||
businessRequest.getDepoAccount().ifPresent(depoAccount ->
|
||||
tradingClearingRegistry.setDepoAccountId(depoAccount.getId()));
|
||||
;
|
||||
if (businessRequest.getTradingClearingRegistryType().isEmpty()){
|
||||
if (businessRequest.getDepoAccount().isPresent()){
|
||||
|
||||
}
|
||||
}
|
||||
// ClearingAccount clearingAccount = request.getMoneyAccountId() != null ?
|
||||
// clearingAccountImdg.getFirstObjectByFieldValues(Map.of("accountId", request.getMoneyAccountId())) : null;
|
||||
//
|
||||
// if (request.getStatus() == null) {
|
||||
// tradingClearingRegistry.setStatus(ServiceStatus.Active.getKey());
|
||||
// log.trace("TCR status in request not set. Use default: {}", tradingClearingRegistry.getStatus());
|
||||
// } else {
|
||||
// tradingClearingRegistry.setStatus(request.getStatus());
|
||||
// log.trace("TCR status in request set: {}", tradingClearingRegistry.getStatus());
|
||||
// }
|
||||
//
|
||||
// String tradingRegistryType;
|
||||
// if (request.getTradingClearingRegistryType() != null) {
|
||||
// tradingRegistryType = request.getTradingClearingRegistryType();
|
||||
// } else if (depoAccount != null) {
|
||||
// tradingRegistryType = depoAccount.getDepoAccountType();
|
||||
// } else {
|
||||
// if (clearingAccount != null) tradingRegistryType = clearingAccount.getClearingAccountType();
|
||||
// else tradingRegistryType = TradingClearingRegistryType.Owner_A.getKey();
|
||||
// }
|
||||
// tradingClearingRegistry.setTradingClearingRegistryType(tradingRegistryType);
|
||||
|
||||
//http://jira.mfd.msk:8088/browse/CLS-631#comment-60200
|
||||
TradingClearingRegistryPurpose registryPurpose = TradingClearingRegistryPurpose.C;
|
||||
tradingClearingRegistry.setTradingClearingRegistryPurpose(registryPurpose.getKey());
|
||||
|
||||
Company company = businessRequest.getCompany();
|
||||
// Long seqId = companySequenceNextId(company.getId(), registryPurpose, tradingRegistryType);
|
||||
// String code = makeCode(company.getClearingCode(), registryPurpose, tradingRegistryType, seqId);
|
||||
// log.debug("For new TCR.id={} of companyId={} next sequence={}; code={}", id, company.getId(), seqId, code);
|
||||
// tradingClearingRegistry.setCode(code);
|
||||
|
||||
Instant now = Instant.now();
|
||||
tradingClearingRegistry.setCreated(now);
|
||||
tradingClearingRegistry.setUpdated(now);
|
||||
|
||||
tradingClearingRegistryImdg.insert(tradingClearingRegistry);
|
||||
|
||||
log.info("New TCR.id={} was created.", tradingClearingRegistry.getId());
|
||||
sendNotificationToReportSvc(tradingClearingRegistry);
|
||||
sendNotificationToClearingSvc(tradingClearingRegistry);
|
||||
|
||||
log.debug("successfully processed, id {}", id);
|
||||
return tradingClearingRegistry;
|
||||
}
|
||||
|
||||
private Long companySequenceNextId(Long companyId,
|
||||
TradingClearingRegistryPurpose registryPurpose,
|
||||
String tradingRegistryType) {
|
||||
|
|
|
|||
|
|
@ -28,7 +28,9 @@ import ru.spcex.clearing.platform.messaging.domain.cud.gateway.SendTkrRequest;
|
|||
import ru.spcex.clearing.platform.messaging.domain.cud.gateway.Tkr;
|
||||
import ru.spcex.clearing.platform.messaging.service.QueueConsumer;
|
||||
import ru.spcex.clearing.platform.messaging.service.RequestInfoUpdate;
|
||||
import ru.spcex.clearing.platform.messaging.service.Status;
|
||||
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
||||
import ru.spcex.clearing.util.security.UserRoleVerification;
|
||||
import ru.spcex.platform.enumeration.CurrencyCode;
|
||||
import ru.spcex.platform.enumeration.TradingClearingRegistryType;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
|
|
@ -43,6 +45,7 @@ public class ClientCodeMessageListener extends QueueConsumer implements Initiali
|
|||
private final ClientCodeFacade clientCodeFacade;
|
||||
private final ClientCodeValidator clientCodeValidator;
|
||||
private final GatewayRequestCreator gatewayRequestCreator;
|
||||
private final UserRoleVerification userRoleVerification;
|
||||
|
||||
public ClientCodeMessageListener(Consumer<String, Object> kafkaQueue,
|
||||
Producer<String, Object> kafkaProducer,
|
||||
|
|
@ -50,22 +53,32 @@ public class ClientCodeMessageListener extends QueueConsumer implements Initiali
|
|||
ImdgProvider imdgProvider,
|
||||
ClientCodeFacade clientCodeFacade,
|
||||
ClientCodeValidator clientCodeValidator,
|
||||
GatewayRequestCreator gatewayRequestCreator) {
|
||||
GatewayRequestCreator gatewayRequestCreator,
|
||||
UserRoleVerification userRoleVerification) {
|
||||
super(kafkaQueue, kafkaProducer);
|
||||
this.kafkaSender = kafkaSender;
|
||||
this.imdgProvider = imdgProvider;
|
||||
this.clientCodeFacade = clientCodeFacade;
|
||||
this.clientCodeValidator = clientCodeValidator;
|
||||
this.gatewayRequestCreator = gatewayRequestCreator;
|
||||
this.userRoleVerification = userRoleVerification;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
imdgProvider.waitAvailable();
|
||||
|
||||
//from backend-api requests
|
||||
callback(ClientCodeNewRequest.class)
|
||||
.setFunction(this::clientCodeNewFromBackend)
|
||||
.forDestination(Consts.DESTINATION_CLIENT_CODE_NEW, callbacks::put);
|
||||
//from gateway requests
|
||||
callback(TkrAccountsGatewayRequest.class)
|
||||
.setFunction(this::clientCodeNewFromGateway)
|
||||
.forDestination(Consts.DESTINATION_CLIENT_CODE_NEW_FROM_GATEWAY, callbacks::put);
|
||||
callback(TkrAccountsGatewayRequest.class)
|
||||
.setFunction(this::clientCodeNewFromGateway)
|
||||
.forDestination(Consts.DESTINATION_CLIENT_CODE_UPDATE_FROM_GATEWAY, callbacks::put);
|
||||
init();
|
||||
}
|
||||
|
||||
|
|
@ -139,6 +152,28 @@ public class ClientCodeMessageListener extends QueueConsumer implements Initiali
|
|||
return null;
|
||||
}
|
||||
|
||||
private RequestInfoUpdate clientCodeNewFromBackend(BaseRequest<ClientCodeNewRequest> clientCodeNewRequest) {
|
||||
RequestInfoUpdate requestInfoUpdate = userRoleVerification.validateRoleAndGetResult(clientCodeNewRequest);
|
||||
if (requestInfoUpdate != null) return requestInfoUpdate;
|
||||
ClientCodeNewRequest request = clientCodeNewRequest.getRequestPayload();
|
||||
ValidationResult validationResult = clientCodeValidator.checkBackendNewRequest(request);
|
||||
if (!validationResult.isValid()) {
|
||||
return makeErrorResponse(clientCodeNewRequest, validationResult);
|
||||
}
|
||||
|
||||
clientCodeFacade.createClientCode(request, validationResult.validator());
|
||||
return null;
|
||||
}
|
||||
|
||||
private RequestInfoUpdate makeErrorResponse(BaseRequest<ClientCodeNewRequest> clientCodeNewRequest,
|
||||
ValidationResult validationResult) {
|
||||
RequestInfoUpdate requestInfoUpdate = new RequestInfoUpdate();
|
||||
requestInfoUpdate.setId(clientCodeNewRequest.getId());
|
||||
requestInfoUpdate.setStatus(Status.Error);
|
||||
validationResult.errorMsg().ifPresent(requestInfoUpdate::setMessage);
|
||||
return requestInfoUpdate;
|
||||
}
|
||||
|
||||
private TradingClearingRegistry selectTradingClearingRegistry(Long companyId, Long moneyAccountId, Long depoAccountId) {
|
||||
Map<String, Comparable<?>> query = new HashMap<>();
|
||||
query.put("companyId", companyId);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import ru.spcex.clearing.platform.messaging.domain.cud.account.ClientCodeNewRequ
|
|||
import ru.spcex.clearing.platform.messaging.domain.cud.account.ClientCodeUpdateRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.TkrAccount;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
|
||||
import ru.spcex.clearing.util.security.UserRoleVerification;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
||||
import ru.spcex.platform.utils.validation.IValidator;
|
||||
|
|
@ -18,17 +19,20 @@ import ru.spcex.platform.utils.validation.IValidator;
|
|||
@Service
|
||||
public class ClientCodeValidator {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private final UserRoleVerification userRoleVerification;
|
||||
private final Function<ClientCodeNewRequest, IValidator> clientCodeNewRequestValidator;
|
||||
private final Function<ClientCodeUpdateRequest, IValidator> clientCodeUpdateRequestValidator;
|
||||
private final Function<CommonDeleteRequest, IValidator> clientCodeDeleteRequestValidator;
|
||||
private final Function<TkrAccount, IValidator> tkrAccountsGatewayValidator;
|
||||
private final IMessageResolver messageResolver;
|
||||
|
||||
public ClientCodeValidator(Function<ClientCodeNewRequest, IValidator> clientCodeNewRequestValidator,
|
||||
public ClientCodeValidator(UserRoleVerification userRoleVerification,
|
||||
Function<ClientCodeNewRequest, IValidator> clientCodeNewRequestValidator,
|
||||
Function<ClientCodeUpdateRequest, IValidator> clientCodeUpdateRequestValidator,
|
||||
Function<CommonDeleteRequest, IValidator> clientCodeDeleteRequestValidator,
|
||||
Function<TkrAccount, IValidator> tkrAccountsGatewayValidator,
|
||||
IMessageResolver messageResolver) {
|
||||
this.userRoleVerification = userRoleVerification;
|
||||
this.clientCodeNewRequestValidator = clientCodeNewRequestValidator;
|
||||
this.clientCodeUpdateRequestValidator = clientCodeUpdateRequestValidator;
|
||||
this.clientCodeDeleteRequestValidator = clientCodeDeleteRequestValidator;
|
||||
|
|
@ -37,7 +41,15 @@ public class ClientCodeValidator {
|
|||
}
|
||||
|
||||
public ValidationResult checkGatewayRequest(TkrAccount gatewayAccountRequest) {
|
||||
IValidator validator = tkrAccountsGatewayValidator.apply(gatewayAccountRequest);
|
||||
return standardValidation(tkrAccountsGatewayValidator, gatewayAccountRequest);
|
||||
}
|
||||
|
||||
public ValidationResult checkBackendNewRequest(ClientCodeNewRequest clientCodeNewRequest) {
|
||||
return standardValidation(clientCodeNewRequestValidator, clientCodeNewRequest);
|
||||
}
|
||||
|
||||
private <T> ValidationResult standardValidation(Function<T, IValidator> requestValidator, T request) {
|
||||
IValidator validator = requestValidator.apply(request);
|
||||
Optional<EnumMessage> error = validator.tillFirstError();
|
||||
AtomicReference<Optional<String>> errorMsg = new AtomicReference<>();
|
||||
error.ifPresent(err -> errorMsg.set(Optional.of(messageResolver.resolve(err))));
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package ru.spcex.clearing.account.validation;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
|
|
@ -12,9 +14,6 @@ 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() {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
package ru.spcex.clearing.account.validation;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.registry.TradingClearingRegistry;
|
||||
import ru.clearing.classes.statics.data.registry.TradingClearingRegistryList;
|
||||
import ru.spcex.clearing.account.errors.AccountError;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.ClientCodeNewRequest;
|
||||
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;
|
||||
|
||||
public enum BackendClientCodeValidationRule implements IValidationRule<ImdgValidationContext<ClientCodeNewRequest>> {
|
||||
AllAccountsPresent() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<ClientCodeNewRequest> context) {
|
||||
ClientCodeNewRequest validatedObject = context.getValidatedObject();
|
||||
List<Long> ids = validatedObject.getCurrencyAccountList();
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return empty(); // необязательное поле
|
||||
}
|
||||
Imdg<Account> imdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
|
||||
for (Long id : ids) {
|
||||
if (id == null) { // null значения в массиве не ожидаются
|
||||
return of(AccountError.RequiredFieldEmpty, "accountId");
|
||||
}
|
||||
Account byIdObject = imdg.getSingleObjectByID(id);
|
||||
if (byIdObject == null)
|
||||
return of(AccountError.AccountNotFound, id, "accountId");
|
||||
}
|
||||
|
||||
return empty();
|
||||
}
|
||||
},
|
||||
TcrIsNotPresentByAccount() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<ClientCodeNewRequest> context) {
|
||||
ClientCodeNewRequest validatedObject = context.getValidatedObject();
|
||||
List<Long> currencyIds = validatedObject.getCurrencyAccountList();
|
||||
Long moneyAccountId = validatedObject.getMoneyAccountId();
|
||||
|
||||
Imdg<TradingClearingRegistry> tradingClearingRegistryImdg =
|
||||
context.obtainMap(IMDGDistributedNames.Map_TradingClearingRegistry, TradingClearingRegistry.class);
|
||||
Imdg<TradingClearingRegistryList> tradingClearingRegistryListImdg =
|
||||
context.obtainMap(IMDGDistributedNames.Map_TradingClearingRegistryList, TradingClearingRegistryList.class);
|
||||
|
||||
TradingClearingRegistry tcrByMoneyAccount = tradingClearingRegistryImdg.getFirstObjectByFieldValues(
|
||||
Map.of(
|
||||
"moneyAccountId", moneyAccountId,
|
||||
"status", Status.Active.getKey()
|
||||
));
|
||||
if (tcrByMoneyAccount != null) {
|
||||
return of(AccountError.TCR_ALREADY_EXIST_GTW);
|
||||
}
|
||||
|
||||
for (Long id : currencyIds) {
|
||||
TradingClearingRegistryList tcrList = tradingClearingRegistryListImdg.getFirstObjectByFieldValues(
|
||||
Map.of(
|
||||
"accountId", id,
|
||||
"status", Status.Active.getKey()
|
||||
));
|
||||
if (tcrList != null) {
|
||||
return of(AccountError.TCR_LIST_ALREADY_EXIST_GTW);
|
||||
}
|
||||
}
|
||||
|
||||
return empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,130 @@
|
|||
package ru.spcex.clearing.account.validation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.clearing.classes.statics.data.registry.TradingClearingRegistry;
|
||||
import ru.spcex.clearing.account.errors.AccountError;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.TkrAccount;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.gateway.MoneyAccountMsgRequest;
|
||||
import ru.spcex.platform.enumeration.CurrencyCode;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
||||
import ru.spcex.platform.imdg.validation.ImdgValidationContext;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.validation.IValidationRule;
|
||||
|
||||
public enum GatewayClientCodeValidationRule implements IValidationRule<ImdgValidationContext<TkrAccount>> {
|
||||
CompanyPresent() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<TkrAccount> context) {
|
||||
TkrAccount validatedObject = context.getValidatedObject();
|
||||
Long tradingCode = validatedObject.getTradingCode();
|
||||
if (tradingCode == null) {
|
||||
return of(AccountError.RequiredFieldEmpty, "trading_code");
|
||||
}
|
||||
|
||||
Imdg<Company> companyImdg = context.obtainMap(IMDGDistributedNames.Map_Company, Company.class);
|
||||
Company company = companyImdg.getFirstObjectByFieldValues(
|
||||
Map.of("trading_code", String.valueOf(tradingCode))
|
||||
);
|
||||
if (company == null) {
|
||||
return of(AccountError.COMPANY_NOT_FOUND_GTW, "trading_code");
|
||||
}
|
||||
|
||||
context.storeObject(ClientCodeStoreObjects.COMPANY, company);
|
||||
return Optional.empty();
|
||||
}
|
||||
},
|
||||
AllAccountsPresent() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<TkrAccount> context) {
|
||||
TkrAccount validatedObject = context.getValidatedObject();
|
||||
List<MoneyAccountMsgRequest> accountList = validatedObject.getMoneyAccounts();
|
||||
if (accountList == null || accountList.isEmpty()) {
|
||||
return of(AccountError.RequiredFieldEmpty, "money_account");
|
||||
}
|
||||
List<String> currencies = accountList.stream().map(MoneyAccountMsgRequest::getCurrCode).toList();
|
||||
if (!currencies.contains(CurrencyCode.RUB.getKey())) {
|
||||
return of(AccountError.RequiredFieldEmpty, "money_account");
|
||||
}
|
||||
Imdg<Account> imdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
|
||||
Map<String, Account> accountByCurrency = new HashMap<>();
|
||||
for (MoneyAccountMsgRequest moneyAccountMsg : accountList) {
|
||||
ImdgPredicateBuilder predicateBuilder = imdg.predicateBuilder();
|
||||
ImdgPredicate currencyPredicate;
|
||||
ImdgPredicate accountPredicate = predicateBuilder.equals("account", moneyAccountMsg.getAccount());
|
||||
if (moneyAccountMsg.getCurrCode().equals(CurrencyCode.RUB.getKey())) {
|
||||
currencyPredicate = predicateBuilder.or(
|
||||
predicateBuilder.equals("currency", CurrencyCode.RUB.getKey()),
|
||||
predicateBuilder.isNull("currency")
|
||||
);
|
||||
} else {
|
||||
currencyPredicate = predicateBuilder.equals("currency", moneyAccountMsg.getCurrCode());
|
||||
}
|
||||
Account account = imdg.getFirstObjectByPredicate(
|
||||
predicateBuilder.and(accountPredicate, currencyPredicate)
|
||||
);
|
||||
if (account == null) {
|
||||
return of(AccountError.ACCOUNT_NOT_FOUND_GTW, moneyAccountMsg.getAccount(), "money_account");
|
||||
}
|
||||
accountByCurrency.put(account.getCurrency(), account);
|
||||
}
|
||||
context.storeObject(ClientCodeStoreObjects.MAPPED_TO_CURRENCY_ACCOUNTS, accountByCurrency);
|
||||
return Optional.empty();
|
||||
}
|
||||
},
|
||||
DepoAccountsPresent() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<TkrAccount> context) {
|
||||
TkrAccount validatedObject = context.getValidatedObject();
|
||||
String depoAccountVal = validatedObject.getDepoAccount();
|
||||
if (!StringUtils.hasText(depoAccountVal)) {
|
||||
context.storeObject(ClientCodeStoreObjects.DEPO_ACCOUNT, Optional.empty());
|
||||
return empty();
|
||||
}
|
||||
|
||||
Imdg<Account> imdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
|
||||
Account depoAccount = imdg.getFirstObjectByFieldValues(
|
||||
Map.of("account", depoAccountVal)
|
||||
);
|
||||
if (depoAccount == null) {
|
||||
context.storeObject(ClientCodeStoreObjects.DEPO_ACCOUNT, Optional.empty());
|
||||
return of(AccountError.DEPO_ACCOUNT_NOT_FOUND_GTW, "depo_account");
|
||||
}
|
||||
|
||||
context.storeObject(ClientCodeStoreObjects.DEPO_ACCOUNT, depoAccount);
|
||||
return Optional.empty();
|
||||
}
|
||||
},
|
||||
TcrIsNotPresent() {
|
||||
@Override
|
||||
public Optional<EnumMessage> validate(ImdgValidationContext<TkrAccount> context) {
|
||||
Map<String, Account> accountByCurrency = context.getStoredObject(ClientCodeStoreObjects.MAPPED_TO_CURRENCY_ACCOUNTS);
|
||||
Account moneyAccount = accountByCurrency.get(CurrencyCode.RUB.getKey());
|
||||
if (moneyAccount == null) {
|
||||
return of(AccountError.AccountNotFound);
|
||||
}
|
||||
Imdg<TradingClearingRegistry> imdg = context.obtainMap(IMDGDistributedNames.Map_TradingClearingRegistry,
|
||||
TradingClearingRegistry.class);
|
||||
|
||||
TradingClearingRegistry tcr = imdg.getFirstObjectByFieldValues(
|
||||
Map.of(
|
||||
"moneyAccountId", moneyAccount.getId(),
|
||||
"status", Status.Active.getKey()
|
||||
)
|
||||
);
|
||||
if (tcr != null) {
|
||||
return of(AccountError.TCR_ALREADY_EXIST_GTW, tcr.getCode(), "code");
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -41,8 +41,7 @@ public class ValidationHelper {
|
|||
return new RequestInfoUpdate()
|
||||
.setId(userRequest.getId())
|
||||
.setStatus(Status.Error)
|
||||
.setMessage(errorMsg)
|
||||
.setEnumMessage(validationError.get());
|
||||
.setMessage(errorMsg);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -1,12 +1,9 @@
|
|||
package ru.spcex.clearing.platform.messaging.service;
|
||||
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
|
||||
public class RequestInfoUpdate {
|
||||
private Long id;
|
||||
private Status status;
|
||||
private String message;
|
||||
private EnumMessage enumMessage;
|
||||
|
||||
public RequestInfoUpdate() {}
|
||||
|
||||
|
|
@ -43,15 +40,6 @@ public class RequestInfoUpdate {
|
|||
return this;
|
||||
}
|
||||
|
||||
public EnumMessage getEnumMessage() {
|
||||
return enumMessage;
|
||||
}
|
||||
|
||||
public RequestInfoUpdate setEnumMessage(EnumMessage enumMessage) {
|
||||
this.enumMessage = enumMessage;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RequestInfoUpdate{" +
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue