use of org.folio.orders.utils.ProtectedOperationType.UPDATE in project mod-orders by folio-org.
the class PieceUpdateFlowManager method updatePiece.
// Flow to update piece
// 1. Before update, get piece by id from storage and store receiving status
// 2. Update piece with new content and complete future
// 3. Create a message and check if receivingStatus is not consistent with storage; if yes - send a message to event bus
public CompletableFuture<Void> updatePiece(Piece pieceToUpdate, boolean createItem, boolean deleteHolding, RequestContext requestContext) {
CompletableFuture<Void> future = new FolioVertxCompletableFuture<>(requestContext.getContext());
PieceUpdateHolder holder = new PieceUpdateHolder().withPieceToUpdate(pieceToUpdate).withCreateItem(createItem).withDeleteHolding(deleteHolding);
pieceStorageService.getPieceById(pieceToUpdate.getId(), requestContext).thenAccept(holder::withPieceFromStorage).thenCompose(aHolder -> basePieceFlowHolderBuilder.updateHolderWithOrderInformation(holder, requestContext)).thenAccept(v -> defaultPieceFlowsValidator.isPieceRequestValid(pieceToUpdate, holder.getOriginPoLine(), createItem)).thenCompose(purchaseOrder -> protectionService.isOperationRestricted(holder.getOriginPurchaseOrder().getAcqUnitIds(), UPDATE, requestContext)).thenCompose(v -> pieceUpdateFlowInventoryManager.processInventory(holder, requestContext)).thenCompose(vVoid -> updatePoLine(holder, requestContext)).thenAccept(afterUpdate -> {
JsonObject messageToEventBus = new JsonObject();
messageToEventBus.put("poLineIdUpdate", holder.getPieceToUpdate().getPoLineId());
Piece.ReceivingStatus receivingStatusStorage = holder.getPieceFromStorage().getReceivingStatus();
Piece.ReceivingStatus receivingStatusUpdate = holder.getPieceToUpdate().getReceivingStatus();
logger.debug("receivingStatusStorage -- {}", receivingStatusStorage);
logger.debug("receivingStatusUpdate -- {}", receivingStatusUpdate);
if (receivingStatusStorage.compareTo(receivingStatusUpdate) != 0) {
pieceService.receiptConsistencyPiecePoLine(messageToEventBus, requestContext);
}
}).thenCompose(aVoid -> pieceStorageService.updatePiece(holder.getPieceToUpdate(), requestContext)).thenAccept(future::complete).exceptionally(t -> {
logger.error("User to update piece with id={}", holder.getPieceToUpdate().getId(), t.getCause());
future.completeExceptionally(t);
return null;
});
return future;
}
Aggregations