Merge branch 'CLS-284' into dev

This commit is contained in:
AKurakin 2023-05-10 12:19:22 +03:00
commit f001e551af
14 changed files with 749 additions and 115 deletions

View file

@ -0,0 +1,134 @@
package ru.spcex.clearing.company.config.validation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.company.relation.Relation;
import ru.clearing.platform.dictionary.ClearingCategoryDictionary;
import ru.clearing.platform.dictionary.WorkflowStatusDictionary;
import ru.spcex.clearing.company.error.CompanyErrors;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.company.RelationNewRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.company.RelationUpdateRequest;
import ru.spcex.clearing.validation.common.rules.DictionaryPresentRule;
import ru.spcex.clearing.validation.common.rules.IdPresentRule;
import ru.spcex.platform.classes.base.SpcexObjectBase;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.validation.ImdgValidationContext;
import ru.spcex.platform.utils.validation.IValidator;
import ru.spcex.platform.utils.validation.ValidatorImpl;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
@Configuration
public class RelationValidationConfig {
@Bean("relationNewRequestValidation")
public Function<RelationNewRequest, IValidator> relationNewRequestValidator(
Map<String, Imdg<? extends SpcexObjectBase>> imdgForValidation
) {
return relationNewRequest -> {
ImdgValidationContext<RelationNewRequest> context = new ImdgValidationContext<>();
context.setValidatedObject(relationNewRequest);
Consumer<String> addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s));
addImdg.accept(IMDGDistributedNames.Map_Company);
addImdg.accept(IMDGDistributedNames.Map_ClearingCategoryDictionary);
addImdg.accept(IMDGDistributedNames.Map_WorkflowStatusDictionary);
return new ValidatorImpl<>(context,
IdPresentRule.instance("companyId",
RelationNewRequest::getCompanyId,
IMDGDistributedNames.Map_Company,
Company.class,
CompanyErrors.RequiredFieldEmpty,
CompanyErrors.CompanyNotFound),
DictionaryPresentRule.instance("documentType",
RelationNewRequest::getClearingMemberCategory,
IMDGDistributedNames.Map_ClearingCategoryDictionary,
ClearingCategoryDictionary.class,
CompanyErrors.RequiredFieldEmpty,
CompanyErrors.WrongFieldValue),
DictionaryPresentRule.instance("serviceStatus",
RelationNewRequest::getServiceStatus,
IMDGDistributedNames.Map_WorkflowStatusDictionary,
WorkflowStatusDictionary.class,
CompanyErrors.RequiredFieldEmpty,
CompanyErrors.WrongFieldValue)
);
};
}
@Bean("relationUpdateRequestValidation")
public Function<RelationUpdateRequest, IValidator> relationUpdateRequestValidator(
Map<String, Imdg<? extends SpcexObjectBase>> imdgForValidation
) {
return relationUpdateRequest -> {
ImdgValidationContext<RelationUpdateRequest> context = new ImdgValidationContext<>();
context.setValidatedObject(relationUpdateRequest);
Consumer<String> addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s));
addImdg.accept(IMDGDistributedNames.Map_Relation);
addImdg.accept(IMDGDistributedNames.Map_Company);
// addImdg.accept(IMDGDistributedNames.Map_ClearingCategoryDictionary);
addImdg.accept(IMDGDistributedNames.Map_WorkflowStatusDictionary);
return new ValidatorImpl<>(context,
IdPresentRule.instance("id",
RelationUpdateRequest::getId,
IMDGDistributedNames.Map_Relation,
Relation.class,
CompanyErrors.RequiredFieldEmpty,
CompanyErrors.RelationNotFound,
relation -> {
if (relation.getConsumerId() == null) return null;
Imdg<Company> companyImdg = context.obtainMap(IMDGDistributedNames.Map_Company, Company.class);
Company company = companyImdg.getSingleObjectByID(relation.getConsumerId());
if (company == null) {
return CompanyErrors.CompanyNotFound;
} else {
return null;
}
}),
// IdPresentRule.instance("companyId",
// RelationUpdateRequest::getCompanyId,
// IMDGDistributedNames.Map_Company,
// Company.class,
// CompanyErrors.RequiredFieldEmpty,
// CompanyErrors.CompanyNotFound),
// DictionaryPresentRule.instance("documentType",
// RelationUpdateRequest::getClearingMemberCategory,
// IMDGDistributedNames.Map_ClearingCategoryDictionary,
// ClearingCategoryDictionary.class,
// CompanyErrors.RequiredFieldEmpty,
// CompanyErrors.WrongFieldValue),
DictionaryPresentRule.instance("serviceStatus",
RelationUpdateRequest::getServiceStatus,
IMDGDistributedNames.Map_WorkflowStatusDictionary,
WorkflowStatusDictionary.class,
CompanyErrors.RequiredFieldEmpty,
CompanyErrors.WrongFieldValue)
);
};
}
@Bean("relationDeleteRequestValidation")
public Function<CommonDeleteRequest, IValidator> relationDeleteRequestValidator(
Map<String, Imdg<? extends SpcexObjectBase>> imdgForValidation
) {
return relationDeleteRequest -> {
ImdgValidationContext<CommonDeleteRequest> context = new ImdgValidationContext<>();
context.setValidatedObject(relationDeleteRequest);
Consumer<String> addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s));
addImdg.accept(IMDGDistributedNames.Map_Relation);
return new ValidatorImpl<>(context,
IdPresentRule.instance("id",
CommonDeleteRequest::getId,
IMDGDistributedNames.Map_Relation,
Relation.class,
CompanyErrors.RequiredFieldEmpty,
CompanyErrors.RelationNotFound
)
);
};
}
}

View file

@ -7,6 +7,7 @@ import ru.clearing.classes.statics.data.account.ClientCode;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.company.CompanySymbols;
import ru.clearing.classes.statics.data.company.ClearingMemberCategory;
import ru.clearing.classes.statics.data.company.relation.Relation;
import ru.clearing.classes.statics.data.profile.Contact;
import ru.clearing.classes.statics.data.profile.ProfileDocument;
import ru.clearing.classes.statics.data.registry.TradingClearingRegistry;
@ -37,6 +38,7 @@ public class ValidationConfig {
addImdg.accept(IMDGDistributedNames.Map_DocumentTypeDictionary, DocumentTypeDictionary.class);
addImdg.accept(IMDGDistributedNames.Map_ContactTypeDictionary, ContactTypeDictionary.class);
addImdg.accept(IMDGDistributedNames.Map_Contact, Contact.class);
addImdg.accept(IMDGDistributedNames.Map_Relation, Relation.class);
addImdg.accept(IMDGDistributedNames.Map_WorkflowStatusDictionary, WorkflowStatusDictionary.class);
addImdg.accept(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class);

View file

@ -25,6 +25,9 @@ public enum CompanyErrors implements IErrorEnumId {
EditContactType(3021L), // Тип контакта компании %s не может быть изменен.
TradingClearingRegistryNotFound(3022L), // ТКР с %s не найден
AccountNotFound(3023L), // Счет %s не найден
ClearingMemberCategoryOnMKRAlreadyExist(3024L), // Договорные отношения %s в секции МКР уже созданы.
ClearingMemberCategoryOnFONDAlreadyExist(3025L), // Договорные отношения %s на фондовой секции уже созданы.
RelationNotFound(3026L), // Запись о договорных отношениях не найдена
;
private final Long id;

View file

@ -111,7 +111,7 @@ public class CompanyInfoService extends QueueConsumer implements InitializingBea
transaction.beginTransaction();
boolean txOk = false;
try {
companyService.relationHelper.onChangeWorkflowStatus(transaction, company, prevStatus, company.getWorkflowStatus());
companyService.relationService.onChangeWorkflowStatus(transaction, company, prevStatus, company.getWorkflowStatus());
Imdg<Company> txCompanyMap = transaction.getImdg(IMDGDistributedNames.Map_Company, Company.class);
txCompanyMap.update(company);
log.trace("Company {} updated", company.getId());

View file

@ -29,13 +29,11 @@ import ru.spcex.platform.imdg.api.ImdgId;
import ru.spcex.platform.imdg.api.ImdgProvider;
import ru.spcex.platform.imdg.api.ImdgTransaction;
import ru.spcex.platform.utils.enumeration.EnumMessage;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.error.ClearingBaseException;
import ru.spcex.platform.utils.error.ValidationException;
import ru.spcex.platform.utils.validation.IValidator;
import java.time.Instant;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
@ -56,7 +54,7 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
protected CompanySymbolService companySymbolService;
protected AccountNotificationHelper accountNotification;
protected RelationHelper relationHelper;
protected RelationService relationService;
@Autowired
public CompanyService(Consumer<String, Object> kafkaQueue, Producer<String, Object> kafkaProducer,
@ -73,7 +71,7 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
CompanySymbolService companySymbolService,
AccountNotificationHelper accountNotification,
RelationHelper relationHelper
RelationService relationService
) {
super(kafkaQueue, kafkaProducer);
this.imdgProvider = imdgProvider;
@ -89,7 +87,7 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
this.companySymbolService = companySymbolService;
companySymbolService.setCompanyService(this);
this.accountNotification = accountNotification;
this.relationHelper = relationHelper;
this.relationService = relationService;
companyIMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
}
@ -145,9 +143,9 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
companyMap.insert(company);
log.debug("company-new request processed, BaseRequest.id = {}, company.id={}",
companyNewRequestBaseRequest.getId(), company.getId());
relationHelper.createNewRelation(transaction, company);
relationService.createNewRelation(transaction, company);
if (!(WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus()))) { // Block
relationHelper.onChangeWorkflowStatus(transaction, company, null, company.getWorkflowStatus());
relationService.onChangeWorkflowStatus(transaction, company, null, company.getWorkflowStatus());
}
txOk = true;
} finally {
@ -180,10 +178,10 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
log.trace("Company {} not found", updateRequest.getId());
return requestHelper.makeErrorResponse(companyUpdateRequestBaseRequest, CompanyErrors.CompanyNotFound, updateRequest.getId());
}
if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) {
log.trace("Company {} not active: {}", company.getId(), company.getWorkflowStatus());
return requestHelper.makeErrorResponse(companyUpdateRequestBaseRequest, CompanyErrors.CompanyDisabled, updateRequest.getId());
}
// if (!WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) {
// log.trace("Company {} not active: {}", company.getId(), company.getWorkflowStatus());
// return requestHelper.makeErrorResponse(companyUpdateRequestBaseRequest, CompanyErrors.CompanyDisabled, updateRequest.getId());
// }
company.setUpdated(Instant.now());
company.setShortName(updateRequest.getShortName());
@ -199,7 +197,7 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
if (updateRequest.getWorkflowStatus() != null) {
company.setWorkflowStatus(updateRequest.getWorkflowStatus());
if (!Objects.equals(prevStatus, company.getWorkflowStatus())) {
relationHelper.onChangeWorkflowStatus(transaction, company, prevStatus, company.getWorkflowStatus());
relationService.onChangeWorkflowStatus(transaction, company, prevStatus, company.getWorkflowStatus());
} else {
log.trace("Status was not changed");
}
@ -286,7 +284,7 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
String prevStatus = company.getWorkflowStatus();
company.setWorkflowStatus(WorkflowStatus.Blocked.getKey());
relationHelper.onChangeWorkflowStatus(transaction, company, prevStatus, company.getWorkflowStatus());
relationService.onChangeWorkflowStatus(transaction, company, prevStatus, company.getWorkflowStatus());
log.debug("Update company.id={}", company.getId());
companyMap.update(company);
@ -339,7 +337,7 @@ public class CompanyService extends QueueConsumer implements InitializingBean {
company.setWorkflowStatus(WorkflowStatus.Blocked.getKey());
relationHelper.onChangeWorkflowStatusOnlyRelationChange(transaction, company, company.getWorkflowStatus());
relationService.onChangeWorkflowStatusOnlyRelationChange(transaction, company, company.getWorkflowStatus());
log.debug("Update company.id={}", company.getId());
companyMap.update(company);

View file

@ -1,97 +0,0 @@
package ru.spcex.clearing.company.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.company.relation.Relation;
import ru.spcex.clearing.company.error.CompanyErrors;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.platform.enumeration.ServiceProduct;
import ru.spcex.platform.enumeration.ServiceStatus;
import ru.spcex.platform.enumeration.WorkflowStatus;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgTransaction;
import ru.spcex.platform.utils.error.ValidationException;
import java.time.Instant;
import java.util.Collection;
import java.util.Objects;
@Service
public class RelationHelper {
protected static final Long SPVB_ID = 1L; // СПВБ
protected final Logger log = LoggerFactory.getLogger(getClass());
protected AccountNotificationHelper accountNotification;
@Autowired
public RelationHelper(AccountNotificationHelper accountNotification) {
this.accountNotification = accountNotification;
}
protected void createNewRelation(ImdgTransaction transaction, Company company) {
Relation relation = new Relation();
// relation.setId(idSequence.nextId());
relation.setCreated(Instant.now());
relation.setUpdated(relation.getCreated());
relation.setConsumerId(company.getId());
relation.setSupplierId(SPVB_ID); // 1 СПВБ
relation.setServiceStatus(WorkflowStatus.Active.getKey());
relation.setService(ru.spcex.platform.enumeration.Service.MKR.getKey()); // MKR
relation.setServiceProduct(ServiceProduct.ZERO.getKey());
log.debug("New Relation[{}] created.", relation.getId());
Imdg<Relation> relationMap = transaction.getImdg(IMDGDistributedNames.Map_Relation, Relation.class);
relationMap.insert(relation);
}
/**
* @param company
* @param oldStatus
* @param newStatus WorkflowStatus.Active - при возобналвении; WorkflowStatus.Blocked - при блокировки/расторжении
*/
public void onChangeWorkflowStatus(ImdgTransaction transaction, Company company, String oldStatus, String newStatus) throws ValidationException {
log.debug("Company id={} status changed from {} to {}",
company.getId(), oldStatus, company.getWorkflowStatus());
onChangeWorkflowStatusOnlyRelationChange(transaction, company, newStatus);
if (WorkflowStatus.Blocked.equalsByKey(newStatus)) {
// IV - сообщения на добавления документа расторжения договора
CompanyErrors hasError = accountNotification.accountTerminationNotification(company.getId());
if (hasError != null) {
log.warn("Can not block company, cause error {}", hasError);
throw new ValidationException(hasError);
}
}
}
protected void onChangeWorkflowStatusOnlyRelationChange(ImdgTransaction transaction, Company company, String newStatus) throws ValidationException {
assert Objects.equals(newStatus, company.getWorkflowStatus());
String query = String.format("consumerId=%s", company.getId());
Imdg<Relation> relationMap = transaction.getImdg(IMDGDistributedNames.Map_Relation, Relation.class);
Collection<Relation> relations = relationMap.getCollectionObjectsBySQL(query);
log.trace("Selected {} Relation by query: {}", relations.size(), query);
Instant now = Instant.now();
for (Relation relation : relations) {
boolean modified = false;
if (WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) {
if (!ServiceStatus.Reopened.equalsByKey(relation.getServiceStatus())) {
relation.setServiceStatus(ServiceStatus.Reopened.getKey());
modified = true;
}
} else if (WorkflowStatus.Blocked.equalsByKey(company.getWorkflowStatus())) {
if (!ServiceStatus.Closed.equalsByKey(relation.getServiceStatus())) {
relation.setServiceStatus(ServiceStatus.Closed.getKey());
modified = true;
}
}
if (modified) {
relation.setUpdated(now);
log.debug("Relation id={} updated", relation.getId());
relationMap.update(relation);
}
}
}
}

View file

@ -0,0 +1,331 @@
package ru.spcex.clearing.company.service;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.producer.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.company.relation.Relation;
import ru.spcex.clearing.company.error.CompanyErrors;
import ru.spcex.clearing.company.util.RequestHelper;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.BaseRequest;
import ru.spcex.clearing.platform.messaging.domain.Consts;
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.company.*;
import ru.spcex.clearing.platform.messaging.service.QueueConsumer;
import ru.spcex.clearing.platform.messaging.service.RequestInfoUpdate;
import ru.spcex.clearing.util.security.UserRoleVerification;
import ru.spcex.clearing.validation.common.ValidationHelper;
import ru.spcex.platform.enumeration.ClearingCategory;
import ru.spcex.platform.enumeration.ServiceProduct;
import ru.spcex.platform.enumeration.ServiceStatus;
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.ImdgTransaction;
import ru.spcex.platform.utils.error.ValidationException;
import ru.spcex.platform.utils.validation.IValidator;
import java.time.Instant;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
@Service
public class RelationService extends QueueConsumer implements InitializingBean {
protected static final Long SPVB_ID = 1L; // СПВБ
protected final Logger log = LoggerFactory.getLogger(getClass());
protected ImdgProvider imdgProvider;
protected ImdgId idSequence;
protected Imdg<Relation> relationMap;
protected Imdg<Company> companyMap;
protected AccountNotificationHelper accountNotification;
protected UserRoleVerification userRoleVerification;
protected RequestHelper requestHelper;
protected ValidationHelper validationHelper;
private Function<RelationNewRequest, IValidator> relationNewRequestValidator;
private Function<RelationUpdateRequest, IValidator> relationUpdateRequestValidator;
private Function<CommonDeleteRequest, IValidator> relationDeleteRequestValidator;
@Autowired
public RelationService(Consumer<String, Object> kafkaQueue,
Producer<String, Object> kafkaProducer,
ImdgProvider imdgProvider,
RequestHelper requestHelper,
UserRoleVerification userRoleVerification,
ValidationHelper validationHelper,
@Qualifier("relationNewRequestValidation") Function<RelationNewRequest, IValidator> relationNewRequestValidator,
@Qualifier("relationUpdateRequestValidation") Function<RelationUpdateRequest, IValidator> relationUpdateRequestValidator,
@Qualifier("relationDeleteRequestValidation") Function<CommonDeleteRequest, IValidator> relationDeleteRequestValidator,
AccountNotificationHelper accountNotification) {
super(kafkaQueue, kafkaProducer);
this.imdgProvider = imdgProvider;
this.idSequence = imdgProvider.getImdgIdGenerator();
this.accountNotification = accountNotification;
this.userRoleVerification = userRoleVerification;
this.validationHelper = validationHelper;
this.relationNewRequestValidator = relationNewRequestValidator;
this.relationUpdateRequestValidator = relationUpdateRequestValidator;
this.relationDeleteRequestValidator = relationDeleteRequestValidator;
relationMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Relation, Relation.class);
companyMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
this.requestHelper = requestHelper.setLogger(log);
}
// --- external API ---
@Override
public void afterPropertiesSet() {
// this.profileDocumentMap = imdgProvider.getImdg(IMDGDistributedNames.Map_ProfileDocument, ProfileDocument.class);
// this.companyMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
callback(RelationNewRequest.class)
.setFunction(this::relationNew)
.forDestination(Consts.DESTINATION_RELATION_NEW, callbacks::put);
callback(RelationUpdateRequest.class)
.setFunction(this::relationUpdate)
.forDestination(Consts.DESTINATION_RELATION_UPDATE, callbacks::put);
callback(CommonDeleteRequest.class)
.setFunction(this::relationDelete)
.forDestination(Consts.DESTINATION_RELATION_DELETE, callbacks::put);
init();
}
private synchronized RequestInfoUpdate relationNew(BaseRequest<RelationNewRequest> relationNewBaseRequest) {
RelationNewRequest req = relationNewBaseRequest.getRequestPayload();
log.debug("relation-new request received, BaseRequest.id = {}", relationNewBaseRequest.getId());
{ // Валидация
RequestInfoUpdate requestInfoUpdate = userRoleVerification.validateRoleAndGetResult(relationNewBaseRequest);
if (requestInfoUpdate != null) return requestInfoUpdate;
requestInfoUpdate = validationHelper.validateTillFirstError(relationNewBaseRequest, relationNewRequestValidator);
if (requestInfoUpdate != null) return requestInfoUpdate;
}
Company company = companyMap.getSingleObjectByID(req.getCompanyId());
Relation existRelation = searchRelation(req.getClearingMemberCategory(), req.getCompanyId());
if (existRelation != null && !WorkflowStatus.Blocked.equalsByKey(existRelation.getServiceStatus())) {
CompanyErrors errorType;
if (ru.spcex.platform.enumeration.Service.MKR.equalsByKey(existRelation.getService())) {
errorType = CompanyErrors.ClearingMemberCategoryOnMKRAlreadyExist;
} else if (ru.spcex.platform.enumeration.Service.FOND.equalsByKey(existRelation.getService())) {
errorType = CompanyErrors.ClearingMemberCategoryOnFONDAlreadyExist;
} else {
log.warn("Unknown relation[{}] service={}", existRelation.getId(), existRelation.getService());
errorType = CompanyErrors.GeneralError;
}
return requestHelper.makeErrorResponse(relationNewBaseRequest, errorType);
}
Relation relation;
if (existRelation == null) {
relation = new Relation();
relation.setId(idSequence.nextId());
relation.setCreated(Instant.now());
relation.setUpdated(relation.getCreated());
} else {
log.info("Found exist relation id={}, it will be update.", existRelation.getId());
relation = existRelation;
relation.setUpdated(Instant.now());
}
relation.setConsumerId(req.getCompanyId());
relation.setSupplierId(SPVB_ID); // 1 СПВБ
if (req.getServiceStatus() != null) {
relation.setServiceStatus(req.getServiceStatus());
} else {
if (existRelation != null) {
relation.setServiceStatus(ServiceStatus.Reopened.getKey());
log.debug("Request has no status. Set status by resume: {}", relation.getServiceStatus());
} else {
String toStatus = WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus()) ? ServiceStatus.Active.getKey() :
WorkflowStatus.Blocked.equalsByKey(company.getWorkflowStatus()) ? ServiceStatus.Closed.getKey() :
null;
log.debug("Request has no status. Set status by company[{}].status={}. Relation[{}].serviceSStatus={}",
company.getId(), company.getWorkflowStatus(), toStatus);
relation.setServiceStatus(toStatus);
}
}
relation.setService(ru.spcex.platform.enumeration.Service.MKR.getKey()); // MKR
relation.setServiceProduct(ServiceProduct.ZERO.getKey());
relation.setComment(req.getComment());
relationMap.insert(relation);
log.debug("New Relation[{}] created.", relation.getId());
return null;
}
private Relation searchRelation(String clearingMemberCategory, Long consumerId) {
String svc;
if (ClearingCategory.B.equalsByKey(clearingMemberCategory)
|| ClearingCategory.I.equalsByKey(clearingMemberCategory)
|| ClearingCategory.V.equalsByKey(clearingMemberCategory)
) {
svc = ru.spcex.platform.enumeration.Service.MKR.getKey();
} else if (ClearingCategory.C.equalsByKey(clearingMemberCategory)
|| ClearingCategory.F.equalsByKey(clearingMemberCategory)
) {
svc = ru.spcex.platform.enumeration.Service.FOND.getKey();
} else {
log.warn("Unexpected relation Relation={}", clearingMemberCategory);
return null;
}
Relation existRelation = relationMap.getSingleObjectByFieldValues(Map.of(
"consumerId", consumerId,
"service", svc
));
return existRelation;
}
private synchronized RequestInfoUpdate relationUpdate(BaseRequest<RelationUpdateRequest> relationUpdateBaseRequest) {
RelationUpdateRequest req = relationUpdateBaseRequest.getRequestPayload();
log.debug("relation-update request received, BaseRequest.id = {}", relationUpdateBaseRequest.getId());
{ // Валидация
RequestInfoUpdate requestInfoUpdate = userRoleVerification.validateRoleAndGetResult(relationUpdateBaseRequest);
if (requestInfoUpdate != null) return requestInfoUpdate;
requestInfoUpdate = validationHelper.validateTillFirstError(relationUpdateBaseRequest, relationUpdateRequestValidator);
if (requestInfoUpdate != null) return requestInfoUpdate;
}
Relation relation = relationMap.getSingleObjectByID(req.getId());
relation.setUpdated(relation.getCreated());
if (req.getServiceStatus() != null) {
log.debug("To relation {} set serivceStatus=\"{}\" by user request.", relation.getId(), req.getServiceStatus());
relation.setServiceStatus(req.getServiceStatus());
} else {
if (relation.getConsumerId() == null) {
log.warn("Relation[{}]. consumerId was null", relation.getId());
}
Company company = companyMap.getSingleObjectByID(relation.getConsumerId());
String toStatus = WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus()) ? ServiceStatus.Active.getKey() :
WorkflowStatus.Blocked.equalsByKey(company.getWorkflowStatus()) ? ServiceStatus.Closed.getKey() :
null;
if (ServiceStatus.Active.equalsByKey(toStatus) && ServiceStatus.Closed.equalsByKey(relation.getServiceStatus())) {
relation.setServiceStatus(ServiceStatus.Reopened.getKey());
log.debug("Set status by resume: {}", relation.getServiceStatus());
} else {
relation.setServiceStatus(toStatus);
log.debug("Request has no status. Set status by company[{}].status={}. Relation[{}].serviceSStatus={}",
company.getId(), company.getWorkflowStatus(), toStatus, relation.getServiceStatus());
}
}
relation.setComment(req.getComment());
relationMap.insert(relation);
log.debug("Update Relation[{}] created.", relation.getId());
return null;
}
private synchronized RequestInfoUpdate relationDelete(BaseRequest<CommonDeleteRequest> relationDeleteBaseRequest) {
CommonDeleteRequest req = relationDeleteBaseRequest.getRequestPayload();
log.debug("relation-delete request received, BaseRequest.id = {}", relationDeleteBaseRequest.getId());
{ // Валидация
RequestInfoUpdate requestInfoUpdate = userRoleVerification.validateRoleAndGetResult(relationDeleteBaseRequest);
if (requestInfoUpdate != null) return requestInfoUpdate;
requestInfoUpdate = validationHelper.validateTillFirstError(relationDeleteBaseRequest, relationDeleteRequestValidator);
if (requestInfoUpdate != null) return requestInfoUpdate;
}
Relation relation = relationMap.getSingleObjectByID(req.getId());
if (WorkflowStatus.Blocked.equalsByKey(relation.getServiceStatus())) {
log.warn("Relation {} already blocked.", relation.getId());
} else {
relation.setUpdated(relation.getCreated());
relation.setServiceStatus(WorkflowStatus.Blocked.getKey());
relationMap.insert(relation);
log.debug("Relation[{}] block.", relation.getId());
}
return null;
}
// --- internal API ---
protected void createNewRelation(ImdgTransaction transaction, Company company) {
Relation relation = new Relation();
// relation.setId(idSequence.nextId());
relation.setCreated(Instant.now());
relation.setUpdated(relation.getCreated());
relation.setConsumerId(company.getId());
relation.setSupplierId(SPVB_ID); // 1 СПВБ
relation.setServiceStatus(WorkflowStatus.Active.getKey());
relation.setService(ru.spcex.platform.enumeration.Service.MKR.getKey()); // MKR
relation.setServiceProduct(ServiceProduct.ZERO.getKey());
log.debug("New Relation[{}] created.", relation.getId());
Imdg<Relation> relationMap = transaction.getImdg(IMDGDistributedNames.Map_Relation, Relation.class);
relationMap.insert(relation);
}
/**
* @param company
* @param oldStatus
* @param newStatus WorkflowStatus.Active - при возобналвении; WorkflowStatus.Blocked - при блокировки/расторжении
*/
public void onChangeWorkflowStatus(ImdgTransaction transaction, Company company, String oldStatus, String newStatus) throws ValidationException {
log.debug("Company id={} status changed from {} to {}",
company.getId(), oldStatus, company.getWorkflowStatus());
onChangeWorkflowStatusOnlyRelationChange(transaction, company, newStatus);
if (WorkflowStatus.Blocked.equalsByKey(newStatus)) {
// IV - сообщения на добавления документа расторжения договора
CompanyErrors hasError = accountNotification.accountTerminationNotification(company.getId());
if (hasError != null) {
log.warn("Can not block company, cause error {}", hasError);
throw new ValidationException(hasError);
}
}
}
protected synchronized void onChangeWorkflowStatusOnlyRelationChange(ImdgTransaction transaction, Company company, String newStatus) throws ValidationException {
assert Objects.equals(newStatus, company.getWorkflowStatus());
String query = String.format("consumerId=%s", company.getId());
Imdg<Relation> relationMap = transaction.getImdg(IMDGDistributedNames.Map_Relation, Relation.class);
Collection<Relation> relations = relationMap.getCollectionObjectsBySQL(query);
log.trace("Selected {} Relation by query: {}", relations.size(), query);
Instant now = Instant.now();
for (Relation relation : relations) {
boolean modified = false;
if (WorkflowStatus.Active.equalsByKey(company.getWorkflowStatus())) {
if (!ServiceStatus.Reopened.equalsByKey(relation.getServiceStatus())) {
relation.setServiceStatus(ServiceStatus.Reopened.getKey());
modified = true;
}
} else if (WorkflowStatus.Blocked.equalsByKey(company.getWorkflowStatus())) {
if (!ServiceStatus.Closed.equalsByKey(relation.getServiceStatus())) {
relation.setServiceStatus(ServiceStatus.Closed.getKey());
modified = true;
}
}
if (modified) {
relation.setUpdated(now);
log.debug("Relation id={} updated", relation.getId());
relationMap.update(relation);
}
}
}
}

View file

@ -74,6 +74,7 @@ public class RequestHelper {
public RequestInfoUpdate makeErrorResponse(BaseRequest<?> request, EnumMessage msg) {
String errorMsg = msg == null? "" : messageResolver.resolve(msg);
log.info("{}", errorMsg);
return new RequestInfoUpdate()
.setId(request.getId())
.setStatus(Status.Error)

View file

@ -50,7 +50,7 @@ import static ru.spcex.clearing.test.config.ImdgTestConfig.waitAvailableImdgProv
ValidationConfig.class,
CompanySymbolService.class,
AccountNotificationHelper.class,
RelationHelper.class,
RelationService.class,
BeanConfiguration.class,
KafkaTestConfig.class,

View file

@ -54,7 +54,7 @@ import static ru.spcex.clearing.test.config.ImdgTestConfig.waitAvailableImdgProv
ValidationConfig.class,
CompanySymbolService.class,
AccountNotificationHelper.class,
RelationHelper.class,
RelationService.class,
CompanySymbolService.class,
CompanySymbolValidationConfig.class,

View file

@ -50,7 +50,7 @@ import static ru.spcex.clearing.test.config.ImdgTestConfig.waitAvailableImdgProv
CompanyService.class,
CompanyValidationConfig.class,
AccountNotificationHelper.class,
RelationHelper.class,
RelationService.class,
KafkaTestConfig.class,
ImdgTestConfig.class})

View file

@ -0,0 +1,214 @@
package ru.spcex.clearing.company.service;
import org.apache.kafka.clients.consumer.MockConsumer;
import org.apache.kafka.clients.producer.MockProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.company.relation.Relation;
import ru.clearing.platform.dictionary.ClearingCategoryDictionary;
import ru.clearing.platform.dictionary.WorkflowStatusDictionary;
import ru.spcex.clearing.company.config.BeanConfiguration;
import ru.spcex.clearing.company.config.validation.RelationValidationConfig;
import ru.spcex.clearing.company.config.validation.ValidationConfig;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.Consts;
import ru.spcex.clearing.platform.messaging.domain.cud.common.CommonDeleteRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.company.RelationNewRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.company.RelationUpdateRequest;
import ru.spcex.clearing.test.MatcherFactory;
import ru.spcex.clearing.test.TestUtils;
import ru.spcex.clearing.test.config.ImdgTestConfig;
import ru.spcex.clearing.test.config.KafkaTestConfig;
import ru.spcex.platform.enumeration.ClearingCategory;
import ru.spcex.platform.enumeration.WorkflowStatus;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider;
import javax.annotation.PostConstruct;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static ru.spcex.clearing.test.MatcherFactory.usingIgnoringFieldsComparator;
import static ru.spcex.clearing.test.TestUtils.*;
import static ru.spcex.clearing.test.config.ImdgTestConfig.currentID;
import static ru.spcex.clearing.test.config.ImdgTestConfig.waitAvailableImdgProviderAndAddAdminWithDefaultId;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {
BeanConfiguration.class,
ValidationConfig.class,
RelationService.class,
AccountNotificationHelper.class,
RelationValidationConfig.class,
KafkaTestConfig.class,
ImdgTestConfig.class})
class RelationServiceTest {
public static final MatcherFactory.Matcher<Relation> RELATION_MATCHER = usingIgnoringFieldsComparator("created", "updated");
private static final int PARTITION = 0;
private static final Long ID = currentID.getAndIncrement();
@Autowired
RelationService relationService;
@Autowired
@Qualifier("hazelcastServiceTest")
private ImdgProvider hazelcastServiceTest;
private Imdg<Relation> relationMap;
private Imdg<Company> companyMap;
@Captor
private ArgumentCaptor<ProducerRecord> producerRecord;
@SpyBean
private MockProducer<String, Object> mockProducer;
private long COMPANY_ID;
private final long RELATION_ID = ID;
@PostConstruct
private void init() {
waitAvailableImdgProviderAndAddAdminWithDefaultId();
this.relationMap = hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_Relation, Relation.class);
this.companyMap = hazelcastServiceTest.getImdg(IMDGDistributedNames.Map_Company, Company.class);
Company testCompany = new Company();
testCompany.setWorkflowStatus(WorkflowStatus.Active.getKey());
COMPANY_ID = companyMap.insert(testCompany);
Imdg<WorkflowStatusDictionary> workflowStatusDictionaryImdg = hazelcastServiceTest.getImdg(
IMDGDistributedNames.Map_WorkflowStatusDictionary,
WorkflowStatusDictionary.class
);
WorkflowStatusDictionary workflowStatusDictionary = new WorkflowStatusDictionary();
workflowStatusDictionary.setId(1L);
workflowStatusDictionary.setCode(WorkflowStatus.Active.getKey());
workflowStatusDictionary.setName("active");
workflowStatusDictionaryImdg.insert(workflowStatusDictionary);
workflowStatusDictionary = new WorkflowStatusDictionary();
workflowStatusDictionary.setId(2L);
workflowStatusDictionary.setCode(WorkflowStatus.Blocked.getKey());
workflowStatusDictionary.setName("not active");
workflowStatusDictionaryImdg.insert(workflowStatusDictionary);
Imdg<ClearingCategoryDictionary> clearingCategoryDictionaryImdg = hazelcastServiceTest.getImdg(
IMDGDistributedNames.Map_ClearingCategoryDictionary,
ClearingCategoryDictionary.class
);
ClearingCategoryDictionary clearingCategoryDictionary = new ClearingCategoryDictionary();
clearingCategoryDictionary.setId(1L);
clearingCategoryDictionary.setCode(ClearingCategory.I.getKey());
clearingCategoryDictionary.setName("I");
clearingCategoryDictionaryImdg.insert(clearingCategoryDictionary);
TestUtils.FutureRecordMetadata future = spy(TestUtils.FutureRecordMetadata.class);
doReturn(future).when(mockProducer).send(producerRecord.capture());
}
@Test
void newRelation() {
//ARRANGE
RelationNewRequest relationNewRequest = new RelationNewRequest();
relationNewRequest.setCompanyId(COMPANY_ID);
relationNewRequest.setServiceStatus("ACTV");
relationNewRequest.setClearingMemberCategory("I");
relationNewRequest.setComment(null);
Relation predictableRelation = new Relation();
predictableRelation.setId(RELATION_ID);
predictableRelation.setConsumerId(relationNewRequest.companyId);
predictableRelation.setService("MKR");
predictableRelation.setServiceProduct("ZERO");
predictableRelation.setSupplierId(1L);
predictableRelation.setServiceStatus("ACTV");
//ACT
String jsonString = getJsonStringForNew(relationNewRequest, ID);
addRecordToKafka((MockConsumer) relationService.getConsumer(), Consts.DESTINATION_RELATION_NEW, PARTITION, 0, jsonString);
//ASSERT
waitingWhenAddedRecordAndCheckIt(ID, mockProducer, producerRecord);
Relation resultNew = relationMap.getSingleObjectBySQL("serviceStatus=ACTV"); // String.format("supplierId = %d", COMPANY_ID));
predictableRelation.setId(resultNew.getId());
RELATION_MATCHER.assertMatch(resultNew, predictableRelation);
}
@Test
void updateRelation() {
//ARRANGE
Relation existsRelation = new Relation();
existsRelation.setId(ID);
existsRelation.setConsumerId(COMPANY_ID);
existsRelation.setSupplierId(1L);
existsRelation.setComment("Test ONE");
existsRelation.setServiceStatus("ACTV");
existsRelation.setServiceProduct("PROD");
existsRelation.setService("MKR");
relationMap.insert(existsRelation);
Relation predictableRelation = new Relation();
predictableRelation.setId(ID);
predictableRelation.setConsumerId(COMPANY_ID);
predictableRelation.setSupplierId(1L);
predictableRelation.setComment("Another Me - ONE");
predictableRelation.setServiceStatus("ACTV");
predictableRelation.setServiceProduct("PROD");
predictableRelation.setService("MKR");
RelationUpdateRequest relationUpdateRequest = new RelationUpdateRequest();
relationUpdateRequest.setId(RELATION_ID);
relationUpdateRequest.setComment("Another Me - ONE");
relationUpdateRequest.setServiceStatus("ACTV");
//ACT
String jsonString = getJsonStringForUpdate(relationUpdateRequest, ID);
addRecordToKafka((MockConsumer) relationService.getConsumer(), Consts.DESTINATION_RELATION_UPDATE, PARTITION, 0, jsonString);
//ASSERT
waitingWhenAddedRecordAndCheckIt(ID, mockProducer, producerRecord);
Relation resultUpdate = relationMap.getSingleObjectBySQL(String.format("id = %d", RELATION_ID));
// predictableProfileDocument.setId(resultUpdate.getId());
RELATION_MATCHER.assertMatch(resultUpdate, predictableRelation);
}
@Test
void deleteProfileDocument() {
//ARRANGE
Relation existsRelation = new Relation();
existsRelation.setId(ID);
existsRelation.setConsumerId(COMPANY_ID);
existsRelation.setSupplierId(1L);
existsRelation.setComment("Test ONE");
existsRelation.setServiceStatus("ACTV");
existsRelation.setServiceProduct("PROD");
existsRelation.setService("MKR");
relationMap.insert(existsRelation);
CommonDeleteRequest profileDocumentDeleteRequest = new CommonDeleteRequest();
profileDocumentDeleteRequest.setId(RELATION_ID);
//ACT
String jsonString = getJsonStringForUpdate(profileDocumentDeleteRequest, ID);
addRecordToKafka((MockConsumer) relationService.getConsumer(), Consts.DESTINATION_RELATION_DELETE, PARTITION, 0, jsonString);
//ASSERT
waitingWhenAddedRecordAndCheckIt(ID, mockProducer, producerRecord);
Relation resultUpdate = relationMap.getSingleObjectByID(RELATION_ID);
// Assertions.assertNull(resultUpdate);
assertEquals(WorkflowStatus.Blocked.getKey(), resultUpdate.getServiceStatus());
}
}

View file

@ -72,7 +72,9 @@ public interface Consts {
@Deprecated
String ACCOUNT_NEW_SDF01 = "account-new-sdf01";
String DESTINATION_RELATION_NEW = "relation-new";
String DESTINATION_RELATION_UPDATE = "relation-update";
String DESTINATION_RELATION_DELETE = "relation-delete";
String DESTINATION_PROFILE_DOCUMENT_NEW = "profile-document-new";
String DESTINATION_PROFILE_DOCUMENT_UPDATE = "profile-document-update";
String DESTINATION_PROFILE_DOCUMENT_DELETE = "profile-document-delete";

View file

@ -0,0 +1,46 @@
package ru.spcex.clearing.platform.messaging.domain.cud.company;
import com.fasterxml.jackson.annotation.JsonProperty;
public class RelationNewRequest {
@JsonProperty
public Long companyId;
@JsonProperty
private String serviceStatus;
@JsonProperty
private String clearingMemberCategory;
@JsonProperty
private String comment;
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
public String getServiceStatus() {
return serviceStatus;
}
public void setServiceStatus(String serviceStatus) {
this.serviceStatus = serviceStatus;
}
public String getClearingMemberCategory() {
return clearingMemberCategory;
}
public void setClearingMemberCategory(String clearingMemberCategory) {
this.clearingMemberCategory = clearingMemberCategory;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}