Search in sources :

Example 6 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method getFromPriceMoneyNode.

public static JsonNode getFromPriceMoneyNode(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
        case GIFT_CARD:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            JsonNode first = variants.get(0);
            JsonNode moneyNode = isTruthy(first.path("onSale")) ? first.path("salePriceMoney") : first.path("priceMoney");
            BigDecimal price = getAmountFromMoneyNode(moneyNode);
            for (int i = 1; i < variants.size(); i++) {
                JsonNode var = variants.get(i);
                JsonNode currentMoneyNode = isTruthy(var.path("onSale")) ? var.path("salePriceMoney") : var.path("priceMoney");
                BigDecimal current = getAmountFromMoneyNode(currentMoneyNode);
                if (current.compareTo(price) < 0) {
                    price = current;
                    moneyNode = currentMoneyNode;
                }
            }
            return moneyNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode) BigDecimal(java.math.BigDecimal)

Example 7 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method getNormalPriceMoneyNode.

public static JsonNode getNormalPriceMoneyNode(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
        case GIFT_CARD:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            JsonNode moneyNode = variants.get(0).path("priceMoney");
            BigDecimal price = getAmountFromMoneyNode(moneyNode);
            for (int i = 1; i < variants.size(); i++) {
                JsonNode currMoneyNode = variants.get(i).path("priceMoney");
                BigDecimal curr = getAmountFromMoneyNode(currMoneyNode);
                if (curr.compareTo(price) > 0) {
                    price = curr;
                    moneyNode = currMoneyNode;
                }
            }
            return moneyNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode) BigDecimal(java.math.BigDecimal)

Example 8 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method hasVariants.

public static boolean hasVariants(JsonNode item) {
    JsonNode structuredContent = item.path("structuredContent");
    JsonNode variants = structuredContent.path("variants");
    ProductType type = getProductType(item);
    return type.equals(ProductType.DIGITAL) ? false : variants.size() > 1;
}
Also used : ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 9 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method getHighestPriceAmongVariants.

public static JsonNode getHighestPriceAmongVariants(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
        case GIFT_CARD:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            JsonNode moneyNode = variants.get(0).path("priceMoney");
            Decimal price = getAmountFromMoneyNode(moneyNode);
            for (int i = 1; i < variants.size(); i++) {
                JsonNode currMoneyNode = variants.get(i).path("priceMoney");
                Decimal curr = getAmountFromMoneyNode(currMoneyNode);
                if (curr.compare(price) > 0) {
                    price = curr;
                    moneyNode = currMoneyNode;
                }
            }
            return moneyNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 10 with ProductType

use of com.squarespace.template.plugins.platform.enums.ProductType in project template-compiler by Squarespace.

the class CommerceUtils method hasVariedPrices.

public static boolean hasVariedPrices(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
        case GIFT_CARD:
            JsonNode variants = structuredContent.path("variants");
            JsonNode first = variants.get(0);
            for (int i = 1; i < variants.size(); i++) {
                JsonNode var = variants.get(i);
                boolean flag1 = !var.path("onSale").equals(first.path("onSale"));
                boolean flag2 = isTruthy(first.path("onSale")) && !var.path("salePrice").equals(first.path("salePrice"));
                boolean flag3 = !var.path("price").equals(first.path("price"));
                if (flag1 || flag2 || flag3) {
                    return true;
                }
            }
            return false;
        case DIGITAL:
        default:
            return false;
    }
}
Also used : ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)10 ProductType (com.squarespace.template.plugins.platform.enums.ProductType)10 Decimal (com.squarespace.cldrengine.api.Decimal)3 BigDecimal (java.math.BigDecimal)2