-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MODORDER-1087]-Delete received pieces in bulk #952
base: master
Are you sure you want to change the base?
Changes from 1 commit
55b0a7a
6db3963
8771a90
3ab692c
ad25b1a
0bf580a
7981d47
b4a0f4d
5ced63d
03aa3cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
package org.folio.service.pieces.flows.delete; | ||
|
||
import static org.folio.orders.utils.ProtectedOperationType.DELETE; | ||
import static org.folio.service.orders.utils.HelperUtils.collectResultsOnSuccess; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.models.pieces.PieceDeletionHolder; | ||
|
@@ -13,6 +18,7 @@ | |
import org.folio.service.ProtectionService; | ||
import org.folio.service.pieces.PieceStorageService; | ||
import org.folio.service.pieces.flows.BasePieceFlowHolderBuilder; | ||
import org.folio.rest.jaxrs.model.PieceCollection; | ||
|
||
import io.vertx.core.Future; | ||
|
||
|
@@ -79,4 +85,26 @@ protected Future<Void> updatePoLine(PieceDeletionHolder holder, RequestContext r | |
: pieceDeleteFlowPoLineService.updatePoLine(holder, requestContext); | ||
} | ||
|
||
public Future<List<Void>> batchDeletePiece (PieceCollection entity, RequestContext requestContext) { | ||
List <String> ids = new ArrayList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should keep an existing logic that Delete by Id endpoint uses with many existing checks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, thank you for pointing it out, will do! :) |
||
entity.getPieces().stream().forEach(v -> ids.add(v.getId())); | ||
List<Future<PieceDeletionHolder>> deletionHolders = ids.stream() | ||
.map(pieceId -> { | ||
PieceDeletionHolder holder = new PieceDeletionHolder().withDeleteHolding(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could not hardcode deleteHolding to true, it should be fetched from request param as doing in Delete By Id endpoint There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for pointing it out, I will correct. |
||
return pieceStorageService.getPieceById(pieceId, requestContext) | ||
.map(pieceToDelete -> { | ||
holder.withPieceToDelete(pieceToDelete); | ||
return holder; | ||
}); | ||
}) | ||
.toList(); | ||
return collectResultsOnSuccess(deletionHolders) | ||
.compose(holders -> { | ||
List<Future<Void>> deleteFutures = holders.stream() | ||
.map(holder -> pieceStorageService.deletePiece(holder.getPieceToDelete().getId(), true, requestContext)) | ||
.toList(); | ||
return collectResultsOnSuccess(deleteFutures); | ||
}); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
import org.folio.rest.jaxrs.model.Eresource; | ||
import org.folio.rest.jaxrs.model.Location; | ||
import org.folio.rest.jaxrs.model.Piece; | ||
import org.folio.rest.jaxrs.model.PieceCollection; | ||
import org.folio.rest.jaxrs.model.PoLine; | ||
import org.folio.rest.jaxrs.model.PurchaseOrder; | ||
import org.folio.rest.jaxrs.model.Title; | ||
|
@@ -368,6 +369,67 @@ void shouldUpdateLineQuantityIfPoLineIsNotPackageAndManualPieceCreateFalseAndInv | |
verify(basePieceFlowHolderBuilder).updateHolderWithOrderInformation(holder, requestContext); | ||
} | ||
|
||
@Test | ||
void shouldDeletePiecesInBatch() { | ||
String orderId = UUID.randomUUID().toString(); | ||
String holdingId = UUID.randomUUID().toString(); | ||
String lineId = UUID.randomUUID().toString(); | ||
String itemId = UUID.randomUUID().toString(); | ||
String locationId = UUID.randomUUID().toString(); | ||
String titleId = UUID.randomUUID().toString(); | ||
JsonObject holding = new JsonObject(); | ||
holding.put(ID, holdingId); | ||
holding.put(HOLDING_PERMANENT_LOCATION_ID, locationId); | ||
JsonObject item = new JsonObject().put(ID, itemId); | ||
item.put(ITEM_STATUS, new JsonObject().put(ITEM_STATUS_NAME, ItemStatus.ON_ORDER.value())); | ||
Piece piece = new Piece().withId(UUID.randomUUID().toString()).withPoLineId(lineId) | ||
.withHoldingId(holdingId).withFormat(Piece.Format.ELECTRONIC); | ||
Location loc = new Location().withHoldingId(holdingId).withQuantityElectronic(1).withQuantity(1); | ||
Cost cost = new Cost().withQuantityElectronic(1) | ||
.withListUnitPriceElectronic(1d).withExchangeRate(1d).withCurrency("USD") | ||
.withPoLineEstimatedPrice(1d); | ||
PoLine poLine = new PoLine().withIsPackage(false).withCheckinItems(false).withOrderFormat(PoLine.OrderFormat.ELECTRONIC_RESOURCE) | ||
.withEresource(new Eresource().withCreateInventory(Eresource.CreateInventory.INSTANCE_HOLDING)) | ||
.withPurchaseOrderId(orderId).withId(lineId).withLocations(List.of(loc)).withCost(cost); | ||
PurchaseOrder purchaseOrder = new PurchaseOrder().withId(orderId).withWorkflowStatus(PurchaseOrder.WorkflowStatus.OPEN); | ||
Title title = new Title().withId(titleId); | ||
List<Piece> ids = new ArrayList<>(); | ||
ids.add(piece); | ||
PieceCollection pieceCollection = new PieceCollection(); | ||
pieceCollection.withPieces(ids); | ||
doReturn(succeededFuture(piece)).when(pieceStorageService).getPieceById(piece.getId(), requestContext); | ||
doReturn(succeededFuture(null)).when(protectionService).isOperationRestricted(any(), any(ProtectedOperationType.class), eq(requestContext)); | ||
doReturn(succeededFuture(null)).when(pieceStorageService).deletePiece(eq(piece.getId()), eq(true), eq(requestContext)); | ||
doReturn(succeededFuture(null)).when(circulationRequestsRetriever).getNumberOfRequestsByItemId(eq(piece.getItemId()), eq(requestContext)); | ||
doReturn(succeededFuture(holding)).when(inventoryHoldingManager).getHoldingById(holdingId, false, requestContext); | ||
doReturn(succeededFuture(null)).when(inventoryItemManager).getItemsByHoldingId(holdingId, requestContext); | ||
doReturn(succeededFuture(null)).when(inventoryHoldingManager).deleteHoldingById(piece.getHoldingId(), true, requestContext); | ||
doReturn(succeededFuture(null)).when(inventoryItemManager).getItemRecordById(itemId, true, requestContext); | ||
doReturn(succeededFuture(null)).when(inventoryItemManager).deleteItem(itemId, true, requestContext); | ||
doReturn(succeededFuture(holding)).when(inventoryHoldingManager).getHoldingById(holdingId, true, requestContext); | ||
doReturn(succeededFuture(null)).when(pieceUpdateInventoryService).deleteHoldingConnectedToPiece(piece, requestContext); | ||
doReturn(succeededFuture(new ArrayList<JsonObject>())).when(inventoryItemManager).getItemsByHoldingId(holdingId, requestContext); | ||
final ArgumentCaptor<PieceDeletionHolder> PieceDeletionHolderCapture = ArgumentCaptor.forClass(PieceDeletionHolder.class); | ||
doAnswer((Answer<Future<Void>>) invocation -> { | ||
PieceDeletionHolder answerHolder = invocation.getArgument(0); | ||
answerHolder.withOrderInformation(purchaseOrder, poLine); | ||
return succeededFuture(null); | ||
}).when(basePieceFlowHolderBuilder).updateHolderWithOrderInformation(PieceDeletionHolderCapture.capture(), eq(requestContext)); | ||
doAnswer((Answer<Future<Void>>) invocation -> { | ||
PieceDeletionHolder answerHolder = invocation.getArgument(0); | ||
answerHolder.withTitleInformation(title); | ||
return succeededFuture(null); | ||
}).when(basePieceFlowHolderBuilder).updateHolderWithTitleInformation(PieceDeletionHolderCapture.capture(), eq(requestContext)); | ||
|
||
final ArgumentCaptor<PieceDeletionHolder> pieceDeletionHolderCapture = ArgumentCaptor.forClass(PieceDeletionHolder.class); | ||
doReturn(succeededFuture(null)).when(pieceDeleteFlowPoLineService).updatePoLine(pieceDeletionHolderCapture.capture(), eq(requestContext)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also separate declaration doReturn and action and verification part to be easy to read |
||
//When | ||
pieceDeleteFlowManager.batchDeletePiece(pieceCollection, requestContext).result(); | ||
verify(pieceStorageService).deletePiece(eq(piece.getId()), eq(true), eq(requestContext)); | ||
verify(inventoryItemManager, times(0)).deleteItem(itemId, true, requestContext); | ||
verify(pieceStorageService, times(1)).deletePiece(eq(piece.getId()), eq(true), eq(requestContext)); | ||
} | ||
|
||
private static class ContextConfiguration { | ||
|
||
@Bean | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here is some formatting issue, also we need to accept list of ids to delete instead of full PieceCollection object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example how you can define the an endpoint:
DELETE /<endpoint_name>>
Content-Type: application/json
{
"ids": [1, 2, 3]
}