Skip to content

Commit

Permalink
added status and role for buyer list
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelianhan007 committed Dec 2, 2024
1 parent 81cee3f commit 4c46ab4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public HttpResponse<Paging<UnifiedAssetDto>> search(
@RequestParam(value = "envId", required = false) String envId,
@RequestParam(value = "buyerId", required = false) String buyerId,
@RequestParam(value = "status", required = false) String status,
@RequestParam(value = "role", required = false) String role,
@RequestParam(value = "orderBy", required = false, defaultValue = "createdAt") String orderBy,
@RequestParam(value = "direction", required = false, defaultValue = "DESC")
Sort.Direction direction,
Expand All @@ -79,7 +80,8 @@ public HttpResponse<Paging<UnifiedAssetDto>> search(
envId,
buyerId,
status,
null,
role,
orderBy,
PageRequest.of(page, size, direction, FIELD_CREATE_AT_ORIGINAL)));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.consoleconnect.kraken.operator.controller.dto;

import com.consoleconnect.kraken.operator.auth.enums.UserRoleEnum;
import lombok.Data;

@Data
Expand All @@ -9,4 +10,5 @@ public class CreateBuyerRequest {
private String envId;

private long tokenExpiredInSeconds;
private String role = UserRoleEnum.USER.name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.consoleconnect.kraken.operator.controller.mapper.BuyerAssetDtoMapper;
import com.consoleconnect.kraken.operator.controller.model.Environment;
import com.consoleconnect.kraken.operator.controller.model.MgmtProperty;
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.entity.UnifiedAssetEntity;
import com.consoleconnect.kraken.operator.core.enums.AssetStatusEnum;
Expand All @@ -25,17 +26,15 @@
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -48,6 +47,7 @@ public class BuyerService extends AssetStatusManager {
private static final String BUYER_NAME = "Buyer";
private static final String BUYER_DESC = "Onboard buyer information";
public static final String ENV = "env";
private static final String ROLE = "role";

@Getter private final UnifiedAssetService unifiedAssetService;
private final UnifiedAssetRepository unifiedAssetRepository;
Expand Down Expand Up @@ -80,7 +80,10 @@ public BuyerAssetDto create(String productId, CreateBuyerRequest buyerOnboard, S

UnifiedAsset buyer =
createBuyer(
buyerOnboard.getBuyerId(), buyerOnboard.getEnvId(), buyerOnboard.getCompanyName());
buyerOnboard.getBuyerId(),
buyerOnboard.getEnvId(),
buyerOnboard.getCompanyName(),
buyerOnboard.getRole());
SyncMetadata syncMetadata = new SyncMetadata("", "", DateTime.nowInUTCString(), createdBy);
IngestionDataResult syncResult =
unifiedAssetService.syncAsset(productId, buyer, syncMetadata, true);
Expand All @@ -94,6 +97,48 @@ public BuyerAssetDto create(String productId, CreateBuyerRequest buyerOnboard, S
buyerCreated, buyerOnboard.getBuyerId(), buyerOnboard.getTokenExpiredInSeconds());
}

@Transactional(readOnly = true)
public Paging<UnifiedAssetDto> search(
String parentId,
String envId,
String buyerId,
String status,
String role,
String orderBy,
PageRequest pageRequest) {
if (parentId != null) {
parentId = unifiedAssetService.findOneByIdOrKey(parentId).getId().toString();
}
List<Tuple2> eqConditions = new ArrayList<>();
eqConditions.add(Tuple2.of("kind", PRODUCT_BUYER.getKind()));
if (StringUtils.isNotBlank(parentId)) {
eqConditions.add(Tuple2.of("parentId", parentId));
}

if (StringUtils.isNotBlank(status)
&& (AssetStatusEnum.ACTIVATED.getKind().equals(status)
|| AssetStatusEnum.DEACTIVATED.getKind().equals(status))) {
eqConditions.add(Tuple2.of("status", status));
}
List<Tuple2> labelConditions = new ArrayList<>();
if (StringUtils.isNotBlank(envId)) {
labelConditions.add(Tuple2.of(LABEL_ENV_ID, envId));
}
if (StringUtils.isNotBlank(buyerId)) {
labelConditions.add(Tuple2.of(LABEL_BUYER_ID, buyerId));
}
if (StringUtils.isNotBlank(role)) {
labelConditions.add(Tuple2.of("role", role));
}
if (StringUtils.isNotBlank(orderBy)) {
pageRequest =
PageRequest.of(
pageRequest.getPageNumber(), pageRequest.getPageSize(), Sort.Direction.DESC, orderBy);
}
return unifiedAssetService.findBySpecification(
eqConditions, labelConditions, null, pageRequest, null);
}

@Transactional(readOnly = true)
public Paging<UnifiedAssetDto> search(
String parentId,
Expand Down Expand Up @@ -179,12 +224,13 @@ private BuyerAssetDto.BuyerToken generateBuyerToken(
return buyerToken;
}

private UnifiedAsset createBuyer(String buyerId, String envId, String companyName) {
private UnifiedAsset createBuyer(String buyerId, String envId, String companyName, String role) {
String key = BUYER_KEY_PREFIX + System.currentTimeMillis();
UnifiedAsset unifiedAsset = UnifiedAsset.of(PRODUCT_BUYER.getKind(), key, BUYER_NAME);
unifiedAsset.getMetadata().setDescription(BUYER_DESC);
unifiedAsset.getMetadata().getLabels().put(LABEL_ENV_ID, envId);
unifiedAsset.getMetadata().getLabels().put(LABEL_BUYER_ID, buyerId);
unifiedAsset.getMetadata().getLabels().put(ROLE, role);
unifiedAsset
.getMetadata()
.getLabels()
Expand All @@ -195,6 +241,7 @@ private UnifiedAsset createBuyer(String buyerId, String envId, String companyNam
buyerInfo.setBuyerId(buyerId);
buyerInfo.setEnvId(envId);
buyerInfo.setCompanyName(companyName);
buyerInfo.setRole(role);
facets.setBuyerInfo(buyerInfo);

unifiedAsset.setFacets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import com.consoleconnect.kraken.operator.auth.enums.UserRoleEnum;
import com.consoleconnect.kraken.operator.config.TestApplication;
import com.consoleconnect.kraken.operator.controller.dto.BuyerAssetDto;
import com.consoleconnect.kraken.operator.controller.dto.CreateBuyerRequest;
Expand All @@ -12,6 +13,7 @@
import com.consoleconnect.kraken.operator.core.dto.Tuple2;
import com.consoleconnect.kraken.operator.core.dto.UnifiedAssetDto;
import com.consoleconnect.kraken.operator.core.enums.AssetKindEnum;
import com.consoleconnect.kraken.operator.core.enums.AssetStatusEnum;
import com.consoleconnect.kraken.operator.core.service.UnifiedAssetService;
import com.consoleconnect.kraken.operator.core.toolkit.AssetsConstants;
import com.consoleconnect.kraken.operator.core.toolkit.LabelConstants;
Expand Down Expand Up @@ -63,6 +65,7 @@ void givenBuyer_whenCreate_thenOK() {
HttpStatus.OK.value(),
null,
bodyStr -> {
log.info(bodyStr);
assertThat(bodyStr, hasJsonPath("$.data", notNullValue()));
assertThat(bodyStr, hasJsonPath("$.data.buyerToken", notNullValue()));
assertThat(bodyStr, hasJsonPath("$.data.buyerToken.accessToken", notNullValue()));
Expand All @@ -74,10 +77,16 @@ void givenBuyer_whenCreate_thenOK() {
void givenBuyer_whenSearch_thenOK() {
webTestClient.requestAndVerify(
HttpMethod.GET,
uriBuilder -> uriBuilder.path(BUYER_BASE_URL).build(),
uriBuilder ->
uriBuilder
.path(BUYER_BASE_URL)
.queryParam("status", AssetStatusEnum.ACTIVATED.getKind())
.queryParam("role", UserRoleEnum.USER.name())
.build(),
HttpStatus.OK.value(),
null,
bodyStr -> {
log.info(bodyStr);
assertThat(bodyStr, Matchers.notNullValue());
assertThat(bodyStr, hasJsonPath("$.data.data", hasSize(1)));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public static class BuyerInfo {
private String buyerId;
private String companyName;
private String envId;
private String role;
}
}

0 comments on commit 4c46ab4

Please sign in to comment.