fix crud operations on BankAccount

This commit is contained in:
etreschenkov 2023-01-13 14:00:27 +03:00
parent 62abb0d58b
commit 12479c89b7
9 changed files with 184 additions and 20 deletions

View file

@ -24,6 +24,10 @@
<groupId>ru.spcex.platform</groupId> <groupId>ru.spcex.platform</groupId>
<artifactId>platform-imdg-api-hazelcast-impl</artifactId> <artifactId>platform-imdg-api-hazelcast-impl</artifactId>
</dependency> </dependency>
<dependency>
<groupId>ru.spcex.platform</groupId>
<artifactId>platform-enum</artifactId>
</dependency>
<dependency> <dependency>
<groupId>ru.spcex.clearing</groupId> <groupId>ru.spcex.clearing</groupId>
<artifactId>classes</artifactId> <artifactId>classes</artifactId>

View file

@ -0,0 +1,14 @@
package ru.spcex.clearing.account.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
@Configuration
public class ErrorResolverConfig {
@Bean
public IMessageResolver messageResolver() {
return new SimpleMessageResolver();
}
}

View file

@ -0,0 +1,17 @@
package ru.spcex.clearing.account.errors;
import ru.spcex.platform.utils.enumeration.IEnumId;
public enum AccountError implements IEnumId {
AccountAlreadyExist(5010L);
private final Long id;
AccountError(Long id) {
this.id = id;
}
@Override
public Long getId() {
return id;
}
}

View file

@ -7,7 +7,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.clearing.classes.statics.data.account.Account;
import ru.clearing.classes.statics.data.account.BankAccount; import ru.clearing.classes.statics.data.account.BankAccount;
import ru.clearing.classes.statics.data.company.relation.Relation;
import ru.spcex.clearing.account.errors.AccountError;
import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.BaseRequest; import ru.spcex.clearing.platform.messaging.domain.BaseRequest;
import ru.spcex.clearing.platform.messaging.domain.Consts; import ru.spcex.clearing.platform.messaging.domain.Consts;
@ -15,36 +18,67 @@ import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewReq
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountUpdateRequest; import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountUpdateRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest; import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
import ru.spcex.clearing.platform.messaging.service.QueueConsumer; 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.platform.enumeration.AccountStatus;
import ru.spcex.platform.enumeration.AccountType;
import ru.spcex.platform.enumeration.Allowed;
import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider; import ru.spcex.platform.imdg.api.ImdgProvider;
import ru.spcex.platform.utils.enumeration.EnumMessage;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import java.time.Instant;
import java.util.Collection;
@Service @Service
public class BankAccountService extends QueueConsumer implements InitializingBean { public class BankAccountService extends QueueConsumer implements InitializingBean {
private final Logger log = LoggerFactory.getLogger(getClass()); private final Logger log = LoggerFactory.getLogger(getClass());
private final Imdg<BankAccount> bankAccountMap; private final Imdg<BankAccount> bankAccountMap;
private final Imdg<Account> accountMap;
private final Imdg<Relation> relationMap;
private final IMessageResolver messageResolver;
@Autowired @Autowired
public BankAccountService(Consumer<String, Object> kafkaQueue, Producer<String, Object> kafkaProducer, ImdgProvider imdgProvider) { public BankAccountService(Consumer<String, Object> kafkaQueue,
Producer<String, Object> kafkaProducer,
ImdgProvider imdgProvider,
IMessageResolver messageResolver) {
super(kafkaQueue, kafkaProducer); super(kafkaQueue, kafkaProducer);
this.bankAccountMap = imdgProvider.getImdg(IMDGDistributedNames.Map_BankAccount, BankAccount.class); 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;
} }
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
callback(BankAccountNewRequest.class) callback(BankAccountNewRequest.class)
.setConsumer(this::bankAccountNew) .setFunction(this::bankAccountNew)
.forDestination(Consts.DESTINATION_BANK_ACCOUNT_NEW, callbacks::put); .forDestination(Consts.DESTINATION_BANK_ACCOUNT_NEW, callbacks::put);
callback(BankAccountUpdateRequest.class) callback(BankAccountUpdateRequest.class)
.setConsumer(this::bankAccountUpdate) .setFunction(this::bankAccountUpdate)
.forDestination(Consts.DESTINATION_BANK_ACCOUNT_UPDATE, callbacks::put); .forDestination(Consts.DESTINATION_BANK_ACCOUNT_UPDATE, callbacks::put);
callback(CommonDeleteRequest.class) callback(CommonDeleteRequest.class)
.setConsumer(this::bankAccountDelete) .setFunction(this::bankAccountDelete)
.forDestination(Consts.DESTINATION_BANK_ACCOUNT_DELETE, callbacks::put); .forDestination(Consts.DESTINATION_BANK_ACCOUNT_DELETE, callbacks::put);
init(); init();
} }
private void bankAccountNew(BaseRequest<BankAccountNewRequest> userRequest) { private RequestInfoUpdate bankAccountNew(BaseRequest<BankAccountNewRequest> userRequest) {
BankAccountNewRequest req = userRequest.getRequestPayload(); 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);
return new RequestInfoUpdate()
.setId(userRequest.getId())
.setStatus(Status.Error)
.setMessage(errorMsg);
}
log.debug("BankAccountNewRequest received"); log.debug("BankAccountNewRequest received");
BankAccount bankAccount = new BankAccount(); BankAccount bankAccount = new BankAccount();
bankAccount.setBankIdentificationCode(req.getBankIdentificationCode()); bankAccount.setBankIdentificationCode(req.getBankIdentificationCode());
@ -57,11 +91,31 @@ public class BankAccountService extends QueueConsumer implements InitializingBea
bankAccount.setTaxRegistrationReasonCode(req.getTaxRegistrationReasonCode()); bankAccount.setTaxRegistrationReasonCode(req.getTaxRegistrationReasonCode());
bankAccount.setAccount(req.getAccount()); bankAccount.setAccount(req.getAccount());
Account account = new Account();
account.setAccount(req.account);
account.setAccountType(AccountType.Bank.getKey());
String relationSqlCondition = String.format("consumerId = %s and service = %s", req.companyId,
ru.spcex.platform.enumeration.Service.MKR.getKey());
Relation relationByCompany = relationMap.getSingleObjectBySQL(relationSqlCondition);
if (relationByCompany != null) {
account.setRelationId(relationByCompany.getId());
account.setCompanyId(relationByCompany.getConsumerId());
} else {
log.warn("Not found relation by condition: {}", relationSqlCondition);
}
account.setAccountStatus(AccountStatus.ACTIVE.getKey());
account.setProcessingSign(Allowed.ALLOWED.getKey());
account.setCreated(Instant.now());
account.setUpdated(Instant.now());
accountMap.insert(account);
bankAccountMap.insert(bankAccount); bankAccountMap.insert(bankAccount);
log.debug("successfully processed, new id {}", bankAccount.getId()); log.debug("successfully processed, new id {}", bankAccount.getId());
return null;
} }
private void bankAccountUpdate(BaseRequest<BankAccountUpdateRequest> userRequest) { private RequestInfoUpdate bankAccountUpdate(BaseRequest<BankAccountUpdateRequest> userRequest) {
BankAccountUpdateRequest req = userRequest.getRequestPayload(); BankAccountUpdateRequest req = userRequest.getRequestPayload();
log.debug("BankAccountUpdateRequest received id = {}", req.getId()); log.debug("BankAccountUpdateRequest received id = {}", req.getId());
BankAccount bankAccount = bankAccountMap.getSingleObjectByID(req.getId()); BankAccount bankAccount = bankAccountMap.getSingleObjectByID(req.getId());
@ -75,17 +129,29 @@ public class BankAccountService extends QueueConsumer implements InitializingBea
bankAccount.setTaxRegistrationReasonCode(req.getTaxRegistrationReasonCode()); bankAccount.setTaxRegistrationReasonCode(req.getTaxRegistrationReasonCode());
bankAccount.setAccount(req.getAccount()); bankAccount.setAccount(req.getAccount());
Account account = accountMap.getSingleObjectByID(bankAccount.getAccountId());
account.setAccount(req.account);
account.setUpdated(Instant.now());
accountMap.update(account);
bankAccountMap.update(bankAccount); bankAccountMap.update(bankAccount);
log.debug("successfully update, existing bankAccount with id {}", bankAccount.getId()); log.debug("successfully update, existing bankAccount with id {}", bankAccount.getId());
return null;
} }
private void bankAccountDelete(BaseRequest<CommonDeleteRequest> userRequest) { private RequestInfoUpdate bankAccountDelete(BaseRequest<CommonDeleteRequest> userRequest) {
CommonDeleteRequest req = userRequest.getRequestPayload(); CommonDeleteRequest req = userRequest.getRequestPayload();
log.debug("CommonDeleteRequest received id = {}", req.getId()); log.debug("CommonDeleteRequest received id = {}", req.getId());
BankAccount bankAccount = bankAccountMap.getSingleObjectByID(req.getId()); BankAccount bankAccount = bankAccountMap.getSingleObjectByID(req.getId());
Account account = accountMap.getSingleObjectByID(bankAccount.getAccountId());
account.setAccountStatus(AccountStatus.BLOCKED.getKey());
account.setUpdated(Instant.now());
accountMap.update(account);
bankAccountMap.delete(bankAccount); bankAccountMap.delete(bankAccount);
log.debug("successfully delete, existing bankAccount with id {}", bankAccount.getId()); log.debug("successfully delete, existing bankAccount with id {}", bankAccount.getId());
return null;
} }
} }

View file

@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import ru.clearing.classes.statics.data.account.BankAccount; import ru.clearing.classes.statics.data.account.BankAccount;
import ru.spcex.clearing.account.config.ErrorResolverConfig;
import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration; import ru.spcex.clearing.account.config.HazelcastServiceTestConfiguration;
import ru.spcex.clearing.account.utils.MatcherFactory.Matcher; import ru.spcex.clearing.account.utils.MatcherFactory.Matcher;
import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.imdg.IMDGDistributedNames;
@ -27,6 +28,7 @@ import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountNewReq
import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountUpdateRequest; import ru.spcex.clearing.platform.messaging.domain.cud.account.BankAccountUpdateRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest; import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService; import ru.spcex.platform.imdg.iml.hazelcast.service.HazelcastService;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -35,7 +37,7 @@ import static ru.spcex.clearing.account.utils.MatcherFactory.usingIgnoringFields
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { @ContextConfiguration(classes = {
HazelcastServiceTestConfiguration.class}) HazelcastServiceTestConfiguration.class, ErrorResolverConfig.class})
public class BankAccountServiceTest { public class BankAccountServiceTest {
public static final Matcher<BankAccount> BANK_ACCOUNT_MATCHER = usingIgnoringFieldsComparator(); public static final Matcher<BankAccount> BANK_ACCOUNT_MATCHER = usingIgnoringFieldsComparator();
private static final int PARTITION = 0; private static final int PARTITION = 0;
@ -47,6 +49,8 @@ public class BankAccountServiceTest {
@Autowired @Autowired
@Qualifier("hazelcastServiceTest") @Qualifier("hazelcastServiceTest")
private HazelcastService hazelcastServiceTest; private HazelcastService hazelcastServiceTest;
@Autowired
private IMessageResolver messageResolver;
private MockConsumer<String, Object> mockConsumer; private MockConsumer<String, Object> mockConsumer;
private MockProducer<String, Object> mockProducer; private MockProducer<String, Object> mockProducer;
@ -120,7 +124,8 @@ public class BankAccountServiceTest {
//ACT //ACT
//service set up //service set up
BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer, hazelcastServiceTest); BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer,
hazelcastServiceTest, messageResolver);
Thread.sleep(10000); Thread.sleep(10000);
//callbacks set up //callbacks set up
bankAccountService.afterPropertiesSet(); bankAccountService.afterPropertiesSet();
@ -221,7 +226,8 @@ public class BankAccountServiceTest {
//ACT //ACT
//service set up //service set up
BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer, hazelcastServiceTest); BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer,
hazelcastServiceTest, messageResolver);
Thread.sleep(10000); Thread.sleep(10000);
//callbacks set up //callbacks set up
bankAccountService.afterPropertiesSet(); bankAccountService.afterPropertiesSet();
@ -305,7 +311,8 @@ public class BankAccountServiceTest {
//ACT //ACT
//service set up //service set up
BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer, hazelcastServiceTest); BankAccountService bankAccountService = new BankAccountService(mockConsumer, mockProducer,
hazelcastServiceTest, messageResolver);
Thread.sleep(10000); Thread.sleep(10000);
//callbacks set up //callbacks set up
bankAccountService.afterPropertiesSet(); bankAccountService.afterPropertiesSet();

View file

@ -0,0 +1,18 @@
package ru.spcex.platform.enumeration;
import ru.spcex.platform.utils.enumeration.IEnumKey;
public enum AccountStatus implements IEnumKey {
ACTIVE("ACTV"), BLOCKED("BLKD"), CLOSE("CLOS");
private final String key;
AccountStatus(String key) {
this.key = key;
}
@Override
public String getKey() {
return key;
}
}

View file

@ -0,0 +1,18 @@
package ru.spcex.platform.enumeration;
import ru.spcex.platform.utils.enumeration.IEnumKey;
public enum Allowed implements IEnumKey {
ALLOWED("ALWD"), DENIED("DEND");
private final String key;
Allowed(String key) {
this.key = key;
}
@Override
public String getKey() {
return key;
}
}

View file

@ -0,0 +1,18 @@
package ru.spcex.platform.enumeration;
import ru.spcex.platform.utils.enumeration.IEnumKey;
public enum Service implements IEnumKey {
MKR("MKR");
private final String key;
Service(String key) {
this.key = key;
}
@Override
public String getKey() {
return key;
}
}

View file

@ -4,23 +4,25 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class BankAccountNewRequest { public class BankAccountNewRequest {
@JsonProperty @JsonProperty
public String bankIdentificationCode;//Банковский идентификационный код (БИК) public String bankIdentificationCode;
@JsonProperty @JsonProperty
public String bankName;//Наименование банка public String bankName;
@JsonProperty @JsonProperty
public String correspondentAccount;//Корреспондентский счет public String correspondentAccount;
@JsonProperty @JsonProperty
public String correspondentAccountName;//Наименование корреспондентского счета public String correspondentAccountName;
@JsonProperty @JsonProperty
public String currency;//Идентификатор валюты public String currency;
@JsonProperty @JsonProperty
public String destination;//Назначение public String destination;
@JsonProperty @JsonProperty
public String taxpayerIdentificationNumber;//Идентификационный номер налогоплательщика (ИНН) public String taxpayerIdentificationNumber;
@JsonProperty @JsonProperty
public String taxRegistrationReasonCode;//Код причины постановки (КПП) public String taxRegistrationReasonCode;
@JsonProperty @JsonProperty
public String account;//Номер счета public String account;
@JsonProperty
public Long companyId;
public String getBankIdentificationCode() { public String getBankIdentificationCode() {
return bankIdentificationCode; return bankIdentificationCode;