Skip to content

Commit

Permalink
[MODORDERS-1209-2]. Update Send Claims and Piece Batch Update models …
Browse files Browse the repository at this point in the history
…with new properties
  • Loading branch information
BKadirkhodjaev committed Dec 18, 2024
1 parent 777b6da commit 631d839
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class PieceBatchStatusUpdateHolder extends BasePieceFlowHolder {
@Getter
private Piece.ReceivingStatus receivingStatus;
@Getter
private Integer claimingInterval;
@Getter
private String internalNote;
@Getter
private String externalNote;
@Getter
private List<Piece> pieces;
private String poLineId;

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/folio/rest/impl/PiecesAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ public void getOrdersPiecesRequests(List<String> pieceIds, String status, Map<St
@Override
public void putOrdersPiecesBatchStatus(PieceBatchStatusCollection pieceBatchStatusCollection, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
pieceUpdateFlowManager.updatePiecesStatuses(pieceBatchStatusCollection.getPieceIds(), pieceBatchStatusCollection.getReceivingStatus(), new RequestContext(vertxContext, okapiHeaders))
var pieceIds = pieceBatchStatusCollection.getPieceIds();
var receivingStatus = pieceBatchStatusCollection.getReceivingStatus();
var claimingInterval = pieceBatchStatusCollection.getClaimingInterval();
pieceUpdateFlowManager.updatePiecesStatuses(pieceIds, receivingStatus, claimingInterval, null, null, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(v -> asyncResultHandler.handle(succeededFuture(buildNoContentResponse())))
.onFailure(t -> handleErrorResponse(asyncResultHandler, t));
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/folio/service/pieces/PieceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import org.folio.models.pieces.PieceBatchStatusUpdateHolder;
import org.folio.orders.utils.PoLineCommonUtil;
import org.folio.rest.jaxrs.model.CompositePoLine;
import org.folio.rest.jaxrs.model.Eresource;
Expand Down Expand Up @@ -82,4 +83,16 @@ public static boolean updatePieceStatus(Piece piece, Piece.ReceivingStatus oldSt
return isStatusChanged;
}

public static boolean updatePieceStatus(Piece piece, PieceBatchStatusUpdateHolder holder) {
var isStatusChanged = !piece.getReceivingStatus().equals(holder.getReceivingStatus());
if (isStatusChanged) {
piece.setStatusUpdatedDate(new Date());
}
piece.setReceivingStatus(holder.getReceivingStatus());
piece.setClaimingInterval(holder.getClaimingInterval());
piece.setInternalNote(holder.getInternalNote());
piece.setExternalNote(holder.getExternalNote());
return isStatusChanged;
}

}
21 changes: 11 additions & 10 deletions src/main/java/org/folio/service/pieces/PiecesClaimingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Future<ClaimingResults> sendClaims(ClaimingCollection claimingCollection,
return Future.succeededFuture(createEmptyClaimingResults(CANNOT_FIND_PIECES_WITH_LATE_STATUS_TO_PROCESS.getValue()));
}
log.info("sendClaims:: Using pieces by vendor id map, map: {}", pieceIdsByVendorIds);
return createJobsByVendor(config, pieceIdsByVendorIds, requestContext);
return createJobsByVendor(claimingCollection, config, pieceIdsByVendorIds, requestContext);
});
})
.onFailure(t -> log.error("sendClaims:: Failed send claims: {}", JsonObject.mapFrom(claimingCollection).encodePrettily(), t));
Expand Down Expand Up @@ -151,15 +151,15 @@ private static Map<String, List<String>> transformAndGroupPieceIdsByVendorId(Lis
.groupingBy(Pair::getKey, mapping(Pair::getValue, toList()));
}

private Future<ClaimingResults> createJobsByVendor(JsonObject config, Map<String, List<String>> pieceIdsByVendorId,
RequestContext requestContext) {
private Future<ClaimingResults> createJobsByVendor(ClaimingCollection claimingCollection, JsonObject config,
Map<String, List<String>> pieceIdsByVendorId, RequestContext requestContext) {
log.info("createJobsByVendor:: Creating jobs by vendor, vendors by pieces count: {}", pieceIdsByVendorId.size());
if (CollectionUtils.isEmpty(pieceIdsByVendorId)) {
log.info("createJobsByVendor:: No jobs are created, pieceIdsByVendorId is empty");
return Future.succeededFuture(new ClaimingResults()
.withClaimingPieceResults(createErrorClaimingResults(pieceIdsByVendorId, CANNOT_GROUP_PIECES_BY_VENDOR_MESSAGE.getValue())));
}
return collectResultsOnSuccess(createUpdatePiecesAndJobFutures(config, pieceIdsByVendorId, requestContext))
return collectResultsOnSuccess(createUpdatePiecesAndJobFutures(claimingCollection, config, pieceIdsByVendorId, requestContext))
.map(updatedPieceLists -> {
if (CollectionUtils.isEmpty(updatedPieceLists)) {
log.info("createJobsByVendor:: No pieces were processed for claiming");
Expand All @@ -173,16 +173,16 @@ private Future<ClaimingResults> createJobsByVendor(JsonObject config, Map<String
});
}

private List<Future<List<String>>> createUpdatePiecesAndJobFutures(JsonObject config, Map<String, List<String>> pieceIdsByVendorId,
RequestContext requestContext) {
private List<Future<List<String>>> createUpdatePiecesAndJobFutures(ClaimingCollection claimingCollection, JsonObject config,
Map<String, List<String>> pieceIdsByVendorId, RequestContext requestContext) {
var updatePiecesAndJobFutures = new ArrayList<Future<List<String>>>();
pieceIdsByVendorId.forEach((vendorId, pieceIds) -> config.stream()
.filter(pieceIdsByVendorIdEntry -> isExportTypeClaimsAndCorrectVendorId(vendorId, pieceIdsByVendorIdEntry)
&& Objects.nonNull(pieceIdsByVendorIdEntry.getValue()))
.forEach(pieceIdsByVendorIdEntry -> {
log.info("createJobsByVendor:: Preparing job integration detail for vendor, vendor id: {}, pieces: {}, job key: {}",
vendorId, pieceIds.size(), pieceIdsByVendorIdEntry.getKey());
updatePiecesAndJobFutures.add(updatePiecesAndCreateJob(pieceIds, pieceIdsByVendorIdEntry, requestContext));
updatePiecesAndJobFutures.add(updatePiecesAndCreateJob(claimingCollection, pieceIds, pieceIdsByVendorIdEntry, requestContext));
}));
return updatePiecesAndJobFutures;
}
Expand All @@ -208,10 +208,11 @@ private static boolean isExportTypeClaimsAndCorrectVendorId(String vendorId, Map
return pieceIdsByVendorIdEntry.getKey().startsWith(String.format("%s_%s", EXPORT_TYPE_CLAIMS, vendorId));
}

private Future<List<String>> updatePiecesAndCreateJob(List<String> pieceIds, Map.Entry<String, Object> pieceIdsByVendorIdEntry,
RequestContext requestContext) {
private Future<List<String>> updatePiecesAndCreateJob(ClaimingCollection claimingCollection, List<String> pieceIds,
Map.Entry<String, Object> pieceIdsByVendorIdEntry, RequestContext requestContext) {
log.info("updatePiecesAndCreateJob:: Updating pieces and creating a job, job key: {}, count: {}", pieceIdsByVendorIdEntry.getKey(), pieceIds.size());
return pieceUpdateFlowManager.updatePiecesStatuses(pieceIds, PieceBatchStatusCollection.ReceivingStatus.CLAIM_SENT, requestContext)
return pieceUpdateFlowManager.updatePiecesStatuses(pieceIds, PieceBatchStatusCollection.ReceivingStatus.CLAIM_SENT,
claimingCollection.getClaimingInterval(), claimingCollection.getInternalNote(), claimingCollection.getExternalNote(), requestContext)
.compose(v -> createJob(pieceIdsByVendorIdEntry.getKey(), pieceIdsByVendorIdEntry.getValue(), pieceIds, requestContext).map(pieceIds));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ public Future<Void> updatePiece(Piece pieceToUpdate, boolean createItem, boolean
.mapEmpty();
}

public Future<Void> updatePiecesStatuses(List<String> pieceIds, PieceBatchStatusCollection.ReceivingStatus receivingStatus, RequestContext requestContext) {
public Future<Void> updatePiecesStatuses(List<String> pieceIds, PieceBatchStatusCollection.ReceivingStatus receivingStatus,
Integer claimingInterval, String internalNote, String externalNote, RequestContext requestContext) {
var newStatus = Piece.ReceivingStatus.fromValue(receivingStatus.value());
return isOperationRestricted(pieceIds, requestContext)
.compose(v -> pieceStorageService.getPiecesByIds(pieceIds, requestContext))
.compose(pieces -> validateFetchedPiecesQuantity(pieces, pieceIds))
.map(pieces -> pieces.stream().collect(Collectors.groupingBy(Piece::getPoLineId)))
.map(piecesByPoLineId -> piecesByPoLineId.entrySet().stream()
.map(entry -> new PieceBatchStatusUpdateHolder(newStatus, entry.getValue(), entry.getKey()))
.map(entry -> new PieceBatchStatusUpdateHolder(newStatus, claimingInterval, internalNote, externalNote, entry.getValue(), entry.getKey()))
.map(holder -> basePieceFlowHolderBuilder.updateHolderWithOrderInformation(holder, requestContext)
.compose(v -> updatePoLine(holder, requestContext))
.compose(v -> updatePiecesStatusesByPoLine(holder, requestContext)))
Expand Down Expand Up @@ -171,7 +172,7 @@ private Future<List<Piece>> validateFetchedPiecesQuantity(List<Piece> pieces, Li

private Future<Void> updatePiecesStatusesByPoLine(PieceBatchStatusUpdateHolder holder, RequestContext requestContext) {
var isAnyPiecesUpdated = holder.getPieces().stream()
.map(piece -> updatePieceStatus(piece, piece.getReceivingStatus(), holder.getReceivingStatus()))
.map(piece -> updatePieceStatus(piece, holder))
.reduce(Boolean.FALSE, Boolean::logicalOr); // Don't replace .map() with .anyMatch(), as it needs to iterate over all elements
if (!Boolean.TRUE.equals(isAnyPiecesUpdated)) {
return Future.succeededFuture();
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/folio/rest/impl/PiecesClaimingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ void testPostPiecesClaim(String name, int vendorIdx, int poLineIdx, int pieceIdx
assertThat(jobCreations, hasSize(dto.jobCreations));
assertThat(jobExecutions, hasSize(dto.jobExecutions));
assertThat(response.getClaimingPieceResults().size(), equalTo(dto.claimingResults));
pieceUpdates.forEach(pieceUpdate -> logger.info("Updated piece: {}", pieceUpdate.encodePrettily()));

var claimedPieceIds = jobCreations.stream()
.peek(job -> logger.info("Created job: {}", JsonObject.mapFrom(job).encodePrettily()))
.map(job -> job.getJsonObject(EXPORT_TYPE_SPECIFIC_PARAMETERS.getValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void testSendClaims_emptyClaimingPieceIds(VertxTestContext testContext) {

@Test
void testSendClaims_noConfigEntries(VertxTestContext testContext) {
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1"));
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1")).withClaimingInterval(1);
var requestContext = mock(RequestContext.class);

when(configurationEntriesCache.loadConfiguration(any(), any())).thenReturn(Future.succeededFuture(new JsonObject()));
Expand All @@ -97,7 +97,7 @@ void testSendClaims_noConfigEntries(VertxTestContext testContext) {

@Test
void testSendClaims_noPiecesFound(VertxTestContext testContext) {
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1"));
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1")).withClaimingInterval(1);
var requestContext = mock(RequestContext.class);

when(configurationEntriesCache.loadConfiguration(any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("key", "value")));
Expand All @@ -113,15 +113,15 @@ void testSendClaims_noPiecesFound(VertxTestContext testContext) {

@Test
void testSendClaims_pieceStatusNotLate(VertxTestContext testContext) {
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1"));
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1")).withClaimingInterval(1);
var requestContext = mock(RequestContext.class);

when(configurationEntriesCache.loadConfiguration(any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("CLAIMS_vendorId", createIntegrationDetail())));
when(pieceStorageService.getPiecesByIds(any(), any())).thenReturn(Future.succeededFuture(List.of(new Piece().withId("pieceId1").withPoLineId("poLineId1").withReceivingStatus(Piece.ReceivingStatus.RECEIVED))));
when(purchaseOrderLineService.getOrderLineById(any(), any())).thenReturn(Future.succeededFuture(new PoLine().withPurchaseOrderId("orderId1")));
when(purchaseOrderStorageService.getPurchaseOrderById(any(), any())).thenReturn(Future.succeededFuture(new PurchaseOrder().withVendor("vendorId")));
when(organizationService.getVendorById(any(), any())).thenReturn(Future.succeededFuture(new Organization().withId("vendorId").withIsVendor(true)));
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any())).thenReturn(Future.succeededFuture());
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture());
when(restClient.post(eq(resourcesPath(DATA_EXPORT_SPRING_CREATE_JOB)), any(), any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("status", "CREATED")));
when(restClient.postEmptyResponse(any(), any(), any())).thenReturn(Future.succeededFuture());

Expand All @@ -135,15 +135,15 @@ void testSendClaims_pieceStatusNotLate(VertxTestContext testContext) {

@Test
void testSendClaims_success(VertxTestContext testContext) {
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1"));
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1")).withClaimingInterval(1);
var requestContext = mock(RequestContext.class);

when(configurationEntriesCache.loadConfiguration(any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("CLAIMS_vendorId", createIntegrationDetail())));
when(pieceStorageService.getPiecesByIds(any(), any())).thenReturn(Future.succeededFuture(List.of(new Piece().withId("pieceId1").withPoLineId("poLineId1").withReceivingStatus(Piece.ReceivingStatus.LATE))));
when(purchaseOrderLineService.getOrderLineById(any(), any())).thenReturn(Future.succeededFuture(new PoLine().withPurchaseOrderId("orderId1")));
when(purchaseOrderStorageService.getPurchaseOrderById(any(), any())).thenReturn(Future.succeededFuture(new PurchaseOrder().withVendor("vendorId")));
when(organizationService.getVendorById(any(), any())).thenReturn(Future.succeededFuture(new Organization().withId("vendorId").withIsVendor(true)));
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any())).thenReturn(Future.succeededFuture());
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture());
when(restClient.post(eq(resourcesPath(DATA_EXPORT_SPRING_CREATE_JOB)), any(), any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("status", "CREATED")));
when(restClient.postEmptyResponse(any(), any(), any())).thenReturn(Future.succeededFuture());

Expand All @@ -158,7 +158,7 @@ void testSendClaims_success(VertxTestContext testContext) {

@Test
void testSendClaims_successWithTwoPieces(VertxTestContext testContext) {
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1", "pieceId2"));
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1", "pieceId2")).withClaimingInterval(1);
var requestContext = mock(RequestContext.class);

when(configurationEntriesCache.loadConfiguration(any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("CLAIMS_vendorId", createIntegrationDetail())));
Expand All @@ -169,7 +169,7 @@ void testSendClaims_successWithTwoPieces(VertxTestContext testContext) {
when(purchaseOrderLineService.getOrderLineById(any(), any())).thenReturn(Future.succeededFuture(new PoLine().withPurchaseOrderId("orderId1")));
when(purchaseOrderStorageService.getPurchaseOrderById(any(), any())).thenReturn(Future.succeededFuture(new PurchaseOrder().withVendor("vendorId")));
when(organizationService.getVendorById(any(), any())).thenReturn(Future.succeededFuture(new Organization().withId("vendorId").withIsVendor(true)));
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any())).thenReturn(Future.succeededFuture());
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture());
when(restClient.post(eq(resourcesPath(DATA_EXPORT_SPRING_CREATE_JOB)), any(), any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("status", "CREATED")));
when(restClient.postEmptyResponse(any(), any(), any())).thenReturn(Future.succeededFuture());

Expand All @@ -186,7 +186,7 @@ void testSendClaims_successWithTwoPieces(VertxTestContext testContext) {

@Test
void testSendClaims_successWithMultipleOrganizations(VertxTestContext testContext) {
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1", "pieceId2", "pieceId3", "pieceId4", "pieceId5"));
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1", "pieceId2", "pieceId3", "pieceId4", "pieceId5")).withClaimingInterval(1);
var requestContext = mock(RequestContext.class);

when(configurationEntriesCache.loadConfiguration(any(), any())).thenReturn(Future.succeededFuture(new JsonObject()
Expand Down Expand Up @@ -216,7 +216,7 @@ void testSendClaims_successWithMultipleOrganizations(VertxTestContext testContex
return Future.succeededFuture(new Organization().withId(vendorId).withIsVendor(true));
});

when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any())).thenReturn(Future.succeededFuture());
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture());
when(restClient.post(eq(resourcesPath(DATA_EXPORT_SPRING_CREATE_JOB)), any(), any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("status", "CREATED")));
when(restClient.postEmptyResponse(any(), any(), any())).thenReturn(Future.succeededFuture());

Expand All @@ -241,7 +241,7 @@ void testSendClaims_successWithMultipleOrganizations(VertxTestContext testContex

@Test
void testSendClaims_successWithMultipleOrganizationsAndOneIntegrationDetail(VertxTestContext testContext) {
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1", "pieceId2", "pieceId3", "pieceId4", "pieceId5"));
var claimingCollection = new ClaimingCollection().withClaimingPieceIds(List.of("pieceId1", "pieceId2", "pieceId3", "pieceId4", "pieceId5")).withClaimingInterval(1);
var requestContext = mock(RequestContext.class);

when(configurationEntriesCache.loadConfiguration(any(), any())).thenReturn(Future.succeededFuture(new JsonObject()
Expand Down Expand Up @@ -270,7 +270,7 @@ void testSendClaims_successWithMultipleOrganizationsAndOneIntegrationDetail(Vert
return Future.succeededFuture(new Organization().withId(vendorId).withIsVendor(true));
});

when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any())).thenReturn(Future.succeededFuture());
when(pieceUpdateFlowManager.updatePiecesStatuses(any(), any(), any(), any(), any(), any())).thenReturn(Future.succeededFuture());
when(restClient.post(eq(resourcesPath(DATA_EXPORT_SPRING_CREATE_JOB)), any(), any(), any())).thenReturn(Future.succeededFuture(new JsonObject().put("status", "CREATED")));
when(restClient.postEmptyResponse(any(), any(), any())).thenReturn(Future.succeededFuture());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ void shouldUpdatePiecesStatusesSuccessfully() {
doReturn(succeededFuture()).when(pieceStorageService).updatePiece(any(), eq(requestContext));
doNothing().when(pieceService).receiptConsistencyPiecePoLine(anyString(), eq(requestContext));

Future<Void> result = pieceUpdateFlowManager.updatePiecesStatuses(pieceIds, receivingStatus, requestContext);
Future<Void> result = pieceUpdateFlowManager.updatePiecesStatuses(pieceIds, receivingStatus, null, null, null, requestContext);

assertTrue(result.succeeded());
}
Expand Down
Loading

0 comments on commit 631d839

Please sign in to comment.