Skip to content

Commit

Permalink
[MODORDERS-1174] Simplify status change logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Sep 20, 2024
1 parent 15cbe2a commit bfa70fb
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 93 deletions.
2 changes: 1 addition & 1 deletion ramls/raml-util
Submodule raml-util updated 54 files
+0 −89 .github/workflows/api-doc.yml
+0 −59 .github/workflows/api-lint.yml
+0 −40 .github/workflows/api-schema-lint.yml
+62 −0 Jenkinsfile
+3 −2 README.md
+16 −25 examples/codex/packageCollection.sample
+169 −0 examples/mod-users/userdata.json.sample
+53 −0 examples/mod-users/users.raml.sample
+7 −0 examples/moduleInfo.sample
+23 −0 examples/tagged-record-example/user.sample
+0 −7 examples/tenantAttributes.sample
+0 −17 examples/tenantJob.sample
+0 −14 library/ramltojaxrs.raml
+3 −0 ramls/codex/codex-instances-sources.raml
+1 −0 ramls/codex/codex-packages.raml
+1 −0 ramls/codex/codex.raml
+2 −1 ramls/jsonSchemas.raml
+2 −1 ramls/ramls.raml
+53 −0 ramls/tagged-record-example/users.raml
+46 −62 ramls/tenant.raml
+1 −0 rtypes/attachments.raml
+0 −55 rtypes/collection-get-with-json-response.raml
+1 −0 rtypes/collection-get.raml
+2 −1 rtypes/collection-stream.raml
+0 −93 rtypes/collection-with-json-response.raml
+1 −0 rtypes/collection.raml
+0 −80 rtypes/get-delete-with-json-response.raml
+1 −0 rtypes/get-delete.raml
+0 −31 rtypes/get-only-with-json-response.raml
+1 −0 rtypes/get-only.raml
+0 −43 rtypes/item-collection-get-with-json-response.raml
+1 −0 rtypes/item-collection-get.raml
+0 −118 rtypes/item-collection-with-json-response.raml
+2 −5 rtypes/item-collection.raml
+1 −0 rtypes/post-empty-body.raml
+0 −1 schemas/codex/codex_instance_cqlschema-ext.json
+0 −1 schemas/codex/codex_instance_cqlschema.json
+0 −1 schemas/codex/codex_package_cqlschema-ext.json
+0 −1 schemas/codex/codex_package_cqlschema.json
+1 −3 schemas/codex/packageType.json
+2 −2 schemas/codex/source.json
+1 −1 schemas/error.schema
+0 −1 schemas/errors.schema
+0 −1 schemas/metadata.schema
+24 −0 schemas/moduleInfo.schema
+0 −1 schemas/parameters.schema
+1 −10 schemas/resultInfo.schema
+0 −1 schemas/tags.schema
+0 −24 schemas/tenantAttributes.schema
+0 −36 schemas/tenantJob.schema
+0 −7 schemas/uuid.schema
+0 −8 traits/internal-error.raml
+9 −0 traits/language.raml
+0 −8 traits/pageable.raml
2 changes: 1 addition & 1 deletion src/main/java/org/folio/helper/PurchaseOrderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.folio.orders.utils.HelperUtils.ORDER_CONFIG_MODULE_NAME;
import static org.folio.orders.utils.HelperUtils.REASON_CANCELLED;
import static org.folio.orders.utils.HelperUtils.WORKFLOW_STATUS;
import static org.folio.orders.utils.HelperUtils.changeOrderStatus;
import static org.folio.orders.utils.HelperUtils.collectResultsOnSuccess;
import static org.folio.orders.utils.HelperUtils.convertToCompositePurchaseOrder;
import static org.folio.orders.utils.OrderStatusTransitionUtil.isOrderClosing;
Expand All @@ -24,6 +23,7 @@
import static org.folio.rest.jaxrs.model.CompositePurchaseOrder.WorkflowStatus.OPEN;
import static org.folio.rest.jaxrs.model.CompositePurchaseOrder.WorkflowStatus.PENDING;
import static org.folio.service.UserService.getCurrentUserId;
import static org.folio.service.orders.utils.StatusUtils.changeOrderStatus;

import java.util.Arrays;
import java.util.List;
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/org/folio/helper/PurchaseOrderLineHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.apache.commons.collections4.CollectionUtils.isEqualCollection;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
import static org.folio.helper.BaseHelper.EVENT_PAYLOAD;
import static org.folio.helper.BaseHelper.ORDER_ID;
import static org.folio.orders.utils.HelperUtils.calculateEstimatedPrice;
import static org.folio.orders.utils.HelperUtils.convertToCompositePoLine;
import static org.folio.orders.utils.HelperUtils.convertToPoLine;
Expand All @@ -20,9 +22,8 @@
import static org.folio.rest.jaxrs.model.CompositePurchaseOrder.WorkflowStatus.OPEN;
import static org.folio.rest.jaxrs.model.CompositePurchaseOrder.WorkflowStatus.PENDING;
import static org.folio.service.orders.utils.StatusUtils.areAllPoLinesCanceled;
import static org.folio.service.orders.utils.StatusUtils.isCompositePoLineStatusCanceled;
import static org.folio.service.orders.utils.StatusUtils.isStatusCanceledCompositePoLine;
import static org.folio.service.orders.utils.StatusUtils.isStatusChanged;
import static org.folio.service.orders.utils.StatusUtils.updateOrderStatusIfNeeded;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -37,13 +38,15 @@

import javax.ws.rs.core.Response;

import io.vertx.core.json.JsonArray;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.models.ItemStatus;
import org.folio.models.PoLineInvoiceLineHolder;
import org.folio.okapi.common.GenericCompositeFuture;
import org.folio.orders.events.handlers.MessageAddress;
import org.folio.orders.utils.HelperUtils;
import org.folio.orders.utils.POLineProtectedFieldsUtil;
import org.folio.orders.utils.PoLineCommonUtil;
Expand Down Expand Up @@ -672,7 +675,7 @@ private Future<Void> processPoLineEncumbrances(CompositePurchaseOrder compOrder,
}

private Future<Void> updateInventoryItemStatus(CompositePoLine compOrderLine, PoLine lineFromStorage, RequestContext requestContext) {
if (!isStatusChanged(compOrderLine, lineFromStorage) || !isCompositePoLineStatusCanceled(compOrderLine)) {
if (!isStatusChanged(compOrderLine, lineFromStorage) || !isStatusCanceledCompositePoLine(compOrderLine)) {
return Future.succeededFuture();
}
return purchaseOrderLineService.getPoLinesByOrderId(compOrderLine.getPurchaseOrderId(), requestContext)
Expand All @@ -686,6 +689,16 @@ private Future<Void> updateInventoryItemStatus(CompositePoLine compOrderLine, Po
});
}

public Future<Void> updateOrderStatusIfNeeded(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine,
PoLine poLineFromStorage, RequestContext requestContext) {
// See MODORDERS-218
if (isStatusChanged(compOrderLine, poLineFromStorage)) {
var updateOrderMessage = JsonObject.of(EVENT_PAYLOAD, JsonArray.of(JsonObject.of(ORDER_ID, compOrderLine.getPurchaseOrderId())));
HelperUtils.sendEvent(MessageAddress.RECEIVE_ORDER_STATUS_UPDATE, updateOrderMessage, requestContext);
}
return Future.succeededFuture();
}

private boolean isEncumbranceUpdateNeeded(CompositePurchaseOrder compOrder, CompositePoLine compositePoLine, PoLine storagePoLine) {
List<FundDistribution> requestFundDistros = compositePoLine.getFundDistribution();
List<FundDistribution> storageFundDistros = storagePoLine.getFundDistribution();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.folio.orders.events.handlers;

import static org.folio.orders.utils.HelperUtils.changeOrderStatus;
import static org.folio.orders.utils.HelperUtils.getOkapiHeaders;
import static org.folio.service.orders.utils.StatusUtils.changeOrderStatus;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,8 +10,8 @@
import org.folio.completablefuture.AsyncUtil;
import org.folio.helper.BaseHelper;
import org.folio.helper.PurchaseOrderHelper;
import org.folio.orders.utils.HelperUtils;
import org.folio.rest.core.models.RequestContext;
import org.folio.rest.jaxrs.model.CompositePoLine;
import org.folio.rest.jaxrs.model.CompositePurchaseOrder;
import org.folio.rest.jaxrs.model.PoLine;
import org.folio.rest.jaxrs.model.PurchaseOrder;
Expand Down Expand Up @@ -108,7 +108,7 @@ protected JsonArray messageAsJsonArray(String rootElement, Message<JsonObject> m
}

protected CompositePurchaseOrder convert(PurchaseOrder po, List<PoLine> poLines) {
var lines = poLines.stream().map(line -> JsonObject.mapFrom(line).mapTo(CompositePoLine.class)).toList();
var lines = poLines.stream().map(HelperUtils::convertToCompositePoLine).toList();
return JsonObject.mapFrom(po).mapTo(CompositePurchaseOrder.class).withCompositePoLines(lines);
}

Expand Down
50 changes: 0 additions & 50 deletions src/main/java/org/folio/orders/utils/HelperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,61 +393,11 @@ public static CompositePurchaseOrder convertToCompositePurchaseOrder(JsonObject
return poJson.mapTo(CompositePurchaseOrder.class);
}

public static boolean changeOrderStatus(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
if (toBeCancelled(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED);
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_CANCELLED));
return true;
}

if (toBeClosed(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED);
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_COMPLETE));
return true;
}

if (toBeReopened(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.OPEN);
return true;
}

return false;
}

public static String convertTagListToCqlQuery(Collection<String> values, String fieldName, boolean strictMatch) {

String prefix = fieldName + (strictMatch ? "==(\"" : "=(\"");
return StreamEx.of(values).joining("\" or \"", prefix, "\")");
}

private static boolean toBeClosed(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
return purchaseOrder.getWorkflowStatus() == PurchaseOrder.WorkflowStatus.OPEN
&& poLines.stream().allMatch(HelperUtils::isCompletedPoLine);
}

private static boolean toBeCancelled(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
return purchaseOrder.getWorkflowStatus() == PurchaseOrder.WorkflowStatus.OPEN
&& poLines.stream().allMatch(HelperUtils::isCancelledPoLine);
}

private static boolean isCancelledPoLine(PoLine line) {
PoLine.PaymentStatus paymentStatus = line.getPaymentStatus();
PoLine.ReceiptStatus receiptStatus = line.getReceiptStatus();
return paymentStatus == PaymentStatus.CANCELLED && receiptStatus == ReceiptStatus.CANCELLED;
}

private static boolean toBeReopened(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
return purchaseOrder.getWorkflowStatus() == PurchaseOrder.WorkflowStatus.CLOSED
&& poLines.stream().anyMatch(line -> !isCompletedPoLine(line));
}

private static boolean isCompletedPoLine(PoLine line) {
PoLine.PaymentStatus paymentStatus = line.getPaymentStatus();
PoLine.ReceiptStatus receiptStatus = line.getReceiptStatus();
return (paymentStatus == PAYMENT_NOT_REQUIRED || paymentStatus == FULLY_PAID || paymentStatus == PaymentStatus.CANCELLED)
&& (receiptStatus == FULLY_RECEIVED || receiptStatus == RECEIPT_NOT_REQUIRED || receiptStatus == ReceiptStatus.CANCELLED);
}

public static CompositePoLine convertToCompositePoLine(PoLine poLine) {
JsonObject pol = JsonObject.mapFrom(poLine);
pol.remove(ALERTS);
Expand Down
115 changes: 81 additions & 34 deletions src/main/java/org/folio/service/orders/utils/StatusUtils.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,109 @@
package org.folio.service.orders.utils;

import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.folio.orders.events.handlers.MessageAddress;
import org.folio.orders.utils.HelperUtils;
import org.folio.rest.core.models.RequestContext;
import org.folio.rest.jaxrs.model.CloseReason;
import org.folio.rest.jaxrs.model.CompositePoLine;
import org.folio.rest.jaxrs.model.CompositePoLine.PaymentStatus;
import org.folio.rest.jaxrs.model.CompositePoLine.ReceiptStatus;
import org.folio.rest.jaxrs.model.CompositePurchaseOrder;
import org.folio.rest.jaxrs.model.PoLine;
import org.folio.rest.jaxrs.model.PoLine.PaymentStatus;
import org.folio.rest.jaxrs.model.PoLine.ReceiptStatus;
import org.folio.rest.jaxrs.model.PurchaseOrder;

import java.util.List;
import java.util.Set;

import static org.folio.helper.BaseHelper.EVENT_PAYLOAD;
import static org.folio.helper.BaseHelper.ORDER_ID;
import static org.folio.orders.utils.HelperUtils.REASON_CANCELLED;
import static org.folio.orders.utils.HelperUtils.REASON_COMPLETE;

public class StatusUtils {

private static final Set<PaymentStatus> resolutionPaymentStatus = Set.of(PaymentStatus.CANCELLED, PaymentStatus.FULLY_PAID, PaymentStatus.PAYMENT_NOT_REQUIRED);
private static final Set<ReceiptStatus> resolutionReceiptStatus = Set.of(ReceiptStatus.CANCELLED, ReceiptStatus.FULLY_RECEIVED, ReceiptStatus.RECEIPT_NOT_REQUIRED);
private static final Set<String> resolutionPaymentStatus = Set.of(PaymentStatus.CANCELLED.value(), PaymentStatus.PAYMENT_NOT_REQUIRED.value(), PaymentStatus.FULLY_PAID.value());
private static final Set<String> resolutionReceiptStatus = Set.of(ReceiptStatus.CANCELLED.value(), ReceiptStatus.RECEIPT_NOT_REQUIRED.value(), ReceiptStatus.FULLY_RECEIVED.value());

public static Future<Void> updateOrderStatusIfNeeded(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine,
PoLine poLineFromStorage, RequestContext requestContext) {
// See MODORDERS-218
if (isStatusChanged(compOrderLine, poLineFromStorage) && shouldUpdateOrderStatus(compositePurchaseOrder, compOrderLine)) {
var updateOrderMessage = JsonObject.of(EVENT_PAYLOAD, JsonArray.of(JsonObject.of(ORDER_ID, compOrderLine.getPurchaseOrderId())));
HelperUtils.sendEvent(MessageAddress.RECEIVE_ORDER_STATUS_UPDATE, updateOrderMessage, requestContext);
}
return Future.succeededFuture();
}

// private static boolean isCompositeOrderClosed(CompositePurchaseOrder order) {
// return order.getWorkflowStatus() == CompositePurchaseOrder.WorkflowStatus.CLOSED;
// }

public static boolean isStatusChanged(CompositePoLine compOrderLine, PoLine lineFromStorage) {
return !StringUtils.equals(lineFromStorage.getReceiptStatus().value(), compOrderLine.getReceiptStatus().value()) ||
!StringUtils.equals(lineFromStorage.getPaymentStatus().value(), compOrderLine.getPaymentStatus().value());
}

private static boolean shouldUpdateOrderStatus(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine) {
return compositePurchaseOrder.getWorkflowStatus() != CompositePurchaseOrder.WorkflowStatus.CLOSED
&& !resolutionPaymentStatus.contains(compOrderLine.getPaymentStatus())
&& !resolutionReceiptStatus.contains(compOrderLine.getReceiptStatus());
}
// public static boolean shouldUpdateOrderStatus(CompositePurchaseOrder compositePurchaseOrder, CompositePoLine compOrderLine, PoLine lineFromStorage) {
// return !isCompositeOrderClosed(compositePurchaseOrder) && isStatusChanged(compOrderLine, lineFromStorage)
// || isCompositeOrderClosed(compositePurchaseOrder) && isNonResolutionPoLine(compOrderLine);
// }

public static boolean areAllPoLinesCanceled(List<PoLine> poLines) {
return poLines.stream().allMatch(StatusUtils::isPoLineStatusCanceled);
return poLines.stream().allMatch(StatusUtils::isStatusCanceledPoLine);
}

public static boolean changeOrderStatus(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
if (toBeCancelled(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED);
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_CANCELLED));
return true;
}
if (toBeClosed(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.CLOSED);
purchaseOrder.setCloseReason(new CloseReason().withReason(REASON_COMPLETE));
return true;
}
if (toBeReopened(purchaseOrder, poLines)) {
purchaseOrder.setWorkflowStatus(PurchaseOrder.WorkflowStatus.OPEN);
return true;
}
return false;
}

private static boolean toBeClosed(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
return isOrderOpen(purchaseOrder)
&& poLines.stream().allMatch(StatusUtils::isCompletedPoLine);
}

private static boolean toBeCancelled(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
return isOrderOpen(purchaseOrder)
&& poLines.stream().allMatch(StatusUtils::isCancelledPoLine);
}

private static boolean isPoLineStatusCanceled(PoLine poLine) {
return PoLine.PaymentStatus.CANCELLED.equals(poLine.getPaymentStatus()) ||
PoLine.ReceiptStatus.CANCELLED.equals(poLine.getReceiptStatus());
private static boolean toBeReopened(PurchaseOrder purchaseOrder, List<PoLine> poLines) {
return isOrderClosed(purchaseOrder)
&& poLines.stream().anyMatch(StatusUtils::isNonResolutionPoLine);
}

public static boolean isCompositePoLineStatusCanceled(CompositePoLine compOrderLine) {
return CompositePoLine.ReceiptStatus.CANCELLED.equals(compOrderLine.getReceiptStatus()) ||
CompositePoLine.PaymentStatus.CANCELLED.equals(compOrderLine.getPaymentStatus());
private static boolean isCompletedPoLine(PoLine line) {
return resolutionPaymentStatus.contains(line.getPaymentStatus().value())
&& resolutionReceiptStatus.contains(line.getReceiptStatus().value());
}

private static boolean isNonResolutionPoLine(PoLine line) {
return !resolutionPaymentStatus.contains(line.getPaymentStatus().value())
&& !resolutionReceiptStatus.contains(line.getReceiptStatus().value());
}

private static boolean isCancelledPoLine(PoLine poLine) {
return poLine.getPaymentStatus() == PoLine.PaymentStatus.CANCELLED
&& poLine.getReceiptStatus() == PoLine.ReceiptStatus.CANCELLED;
}

private static boolean isStatusCanceledPoLine(PoLine poLine) {
return poLine.getPaymentStatus() == PoLine.PaymentStatus.CANCELLED
|| poLine.getReceiptStatus() == PoLine.ReceiptStatus.CANCELLED;
}

public static boolean isStatusCanceledCompositePoLine(CompositePoLine compOrderLine) {
return compOrderLine.getReceiptStatus() == CompositePoLine.ReceiptStatus.CANCELLED
|| compOrderLine.getPaymentStatus() == CompositePoLine.PaymentStatus.CANCELLED;
}

private static boolean isOrderOpen(PurchaseOrder order) {
return order.getWorkflowStatus() == PurchaseOrder.WorkflowStatus.OPEN;
}

private static boolean isOrderClosed(PurchaseOrder order) {
return order.getWorkflowStatus() == PurchaseOrder.WorkflowStatus.CLOSED;
}

private StatusUtils() {}

}
3 changes: 2 additions & 1 deletion src/test/java/org/folio/orders/utils/HelperUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.folio.rest.jaxrs.model.CloseReason;
import org.folio.rest.jaxrs.model.PoLine;
import org.folio.rest.jaxrs.model.PurchaseOrder;
import org.folio.service.orders.utils.StatusUtils;
import org.junit.jupiter.api.Test;

import javax.money.convert.ConversionQuery;
Expand Down Expand Up @@ -92,7 +93,7 @@ void testOrderStatusToBeCancelled() {

List<PoLine> poLines = List.of(firstPoLine, secondPoLine);

assertTrue(HelperUtils.changeOrderStatus(purchaseOrder, poLines));
assertTrue(StatusUtils.changeOrderStatus(purchaseOrder, poLines));
assertEquals(purchaseOrder.getWorkflowStatus(), PurchaseOrder.WorkflowStatus.CLOSED);
assertEquals(purchaseOrder.getCloseReason(), new CloseReason().withReason(REASON_CANCELLED));
}
Expand Down

0 comments on commit bfa70fb

Please sign in to comment.