company-service: CompanyRoleSetService (в ТЗ не указаны запросы)

This commit is contained in:
AKurakin 2023-04-05 22:52:41 +03:00
parent 8c43a6e4ae
commit cd093c81eb
3 changed files with 188 additions and 0 deletions

View file

@ -0,0 +1,149 @@
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.CompanyRoleSet;
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.CompanyRoleSetNewRequest;
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.UserRole;
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.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.error.ValidationException;
import ru.spcex.platform.utils.validation.IValidator;
import java.util.Map;
@Service
public class CompanyRoleSetService extends QueueConsumer implements InitializingBean {
private final Logger log = LoggerFactory.getLogger(getClass());
private final RequestHelper requestHelper;
private final ImdgProvider imdgProvider;
// private final Imdg<Company> companyMap;
private final Imdg<CompanyRoleSet> companyRoleSetMap;
private final ImdgId idSequence;
protected UserRoleVerification userRoleVerification;
protected IMessageResolver messageResolver;
private ValidationHelper validationHelper;
// private Function<CompanyRoleSetNewRequest, IValidator> companyNewRequestValidator;
@Autowired
public CompanyRoleSetService(Consumer<String, Object> kafkaQueue, Producer<String, Object> kafkaProducer,
ImdgProvider imdgProvider,
IMessageResolver messageResolver,
UserRoleVerification userRoleVerification,
ValidationHelper validationHelper
// @Qualifier("CompanyRoleSetRequestValidator")
// Function<CompanyRoleSetNewRequest, IValidator> companyNewRequestValidator,
) {
super(kafkaQueue, kafkaProducer);
this.imdgProvider = imdgProvider;
this.messageResolver = messageResolver;
this.requestHelper = new RequestHelper(log, messageResolver);
this.companyRoleSetMap = imdgProvider.getImdg(IMDGDistributedNames.Map_CompanyRoleSet, CompanyRoleSet.class);
this.idSequence = imdgProvider.getImdgIdGenerator();
this.userRoleVerification = userRoleVerification;
this.validationHelper = validationHelper;
// this.companyDeleteRequestValidator = companyDeleteRequestValidator;
}
@Override
public void afterPropertiesSet() {
callback(CompanyRoleSetNewRequest.class)
.setFunction(request -> requestHelper.requestFunction(this::createCompanyRoleSet, request))
.forDestination(Consts.DESTINATION_COMPANY_ROLE_SET_NEW, callbacks::put);
// callback(CompanyRoleSetNewRequest.class)
// .setConsumer(request -> requestHelper.requestFunction(this::createCompanyRoleSet, request))
// .forDestination(Consts.DESTINATION_COMPANY_ROLE_SET_UPDATE, callbacks::put);
callback(CommonDeleteRequest.class)
.setFunction(request -> requestHelper.requestFunction(this::deleteCompanyRoleSet, request))
.forDestination(Consts.DESTINATION_COMPANY_ROLE_SET_DELETE
, callbacks::put);
init();
}
protected void validateRole(BaseRequest<?> req) throws ValidationException {
Long requestor = req.getUserId();
if (requestor != null && !userRoleVerification.userHasRole(requestor, UserRole.Admin)) {
log.trace("User {} has no role to allow this action", requestor);
throw new ValidationException(CompanyErrors.UserVerifyDenial);
} else {
log.trace("Request without userId");
}
}
private RequestInfoUpdate createCompanyRoleSet(BaseRequest<CompanyRoleSetNewRequest> companyRoleNewRequestBaseRequest) throws ValidationException {
CompanyRoleSetNewRequest req = companyRoleNewRequestBaseRequest.getRequestPayload();
log.debug("companyroleset-new request received, BaseRequest.id = {}", companyRoleNewRequestBaseRequest.getId());
{ // Валидация, ValidationException
validateRole(companyRoleNewRequestBaseRequest);
// RequestInfoUpdate requestInfoUpdate = validationHelper.validateTillFirstError(companyRoleNewRequestBaseRequest, companyNewRequestValidator);
// if (requestInfoUpdate != null) return requestInfoUpdate;
}
//todo какие проверки нужны? 1. есть компания, 2. есть справочники
CompanyRoleSet newRole = companyRoleSetMap.getSingleObjectByFieldValues(Map.of(
"companyId", req.getCompanyId(),
"companyRole", req.getCompanyRole()
));
boolean isNew;
if (newRole == null) {
isNew = true;
newRole = new CompanyRoleSet();
newRole.setId(idSequence.nextId());
newRole.setCompanyId(req.getCompanyId());
log.trace("New CompanyRoleSet[{}] for company {}", newRole.getId(), req.getCompanyId());
} else {
isNew = false;
log.trace("Update exist CompanyRoleSet[{}] for company {}", newRole.getId(), req.getCompanyId());
// return requestHelper.makeErrorResponse(companyRoleNewRequestBaseRequest, CompanyErrors.RecordAlreadyExist, newRole.getId());
}
newRole.setCompanyRole(req.getCompanyRole());
newRole.setWorkflowStatus(req.getWorkflowStatus());
if (isNew)
companyRoleSetMap.insert(newRole);
else
companyRoleSetMap.update(newRole);
return null;
}
private RequestInfoUpdate deleteCompanyRoleSet(BaseRequest<CommonDeleteRequest> companyRoleSetDeleteRequest) throws ValidationException {
CommonDeleteRequest request = companyRoleSetDeleteRequest.getRequestPayload();
log.debug("companyRoleSetDeleteRequest received id = {}", request.getId());
{ // Валидация, ValidationException
validateRole(companyRoleSetDeleteRequest);
if (request.getId() == null) {
return requestHelper.makeErrorResponse(companyRoleSetDeleteRequest, CompanyErrors.RequiredFieldEmpty, "id");
}
}
CompanyRoleSet role = companyRoleSetMap.getSingleObjectByID(request.getId());
if (role == null) {
return requestHelper.makeErrorResponse(companyRoleSetDeleteRequest, CompanyErrors.RecordNotFound, request.getId());
}
companyRoleSetMap.delete(role);
log.debug("CompanyRoleSet {} for company {} delete", role.getId(), role.getCompanyId());
return null;
}
}

View file

@ -41,6 +41,9 @@ public interface Consts {
String DESTINATION_COMPANY_INFO_NEW = "company-info-new";
String DESTINATION_COMPANY_INFO_UPDATE = "company-info-update";
String DESTINATION_COMPANY_SYMBOL_UPDATE = "company-symbol-update";
String DESTINATION_COMPANY_ROLE_SET_NEW = "companyroleset-new";
String DESTINATION_COMPANY_ROLE_SET_DELETE = "companyroleset-delete";
String DESTINATION_COMPANY_ROLE_SET_UPDATE = "companyroleset-update";
String DESTINATION_CONTACT_NEW = "contact-new";
String DESTINATION_CONTACT_UPDATE = "contact-update";
String DESTINATION_CLEARING_MEMBER_CATEGORY_NEW = "clearing-member-category-new";

View file

@ -0,0 +1,36 @@
package ru.spcex.clearing.platform.messaging.domain.cud.company;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CompanyRoleSetNewRequest {
@JsonProperty
Long companyId;
@JsonProperty
String companyRole;
@JsonProperty
String workflowStatus;
public Long getCompanyId() {
return companyId;
}
public void setCompanyId(Long companyId) {
this.companyId = companyId;
}
public String getCompanyRole() {
return companyRole;
}
public void setCompanyRole(String companyRole) {
this.companyRole = companyRole;
}
public String getWorkflowStatus() {
return workflowStatus;
}
public void setWorkflowStatus(String workflowStatus) {
this.workflowStatus = workflowStatus;
}
}