Skip to content

Instantly share code, notes, and snippets.

@gdenning
Created February 4, 2022 00:13
Show Gist options
  • Save gdenning/04d05de52d6197fe189771648cb84a51 to your computer and use it in GitHub Desktop.
Save gdenning/04d05de52d6197fe189771648cb84a51 to your computer and use it in GitHub Desktop.
package com.elasticpath.plugins.pickuprequiredaddtocartvalidator.extensions;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.pf4j.Extension;
import com.elasticpath.plugins.pickuprequiredaddtocartvalidator.PickupRequiredAddToCartValidatorPlugin;
import com.elasticpath.xpf.XPFExtensionPointEnum;
import com.elasticpath.xpf.connectivity.annontation.XPFAssignment;
import com.elasticpath.xpf.connectivity.context.XPFShoppingItemValidationContext;
import com.elasticpath.xpf.connectivity.dto.XPFStructuredErrorMessage;
import com.elasticpath.xpf.connectivity.extension.XPFExtensionPointImpl;
import com.elasticpath.xpf.connectivity.extensionpoint.ShoppingItemValidator;
/**
* Ensure that the "pickupDate" cart item modifier is set to at least 7 days in the future if the product in the cart has the "pickupRequired"
* attribute set to true.
*/
@SuppressWarnings("checkstyle:magicnumber")
@Extension
@XPFAssignment(extensionPoint = XPFExtensionPointEnum.VALIDATE_SHOPPING_ITEM_AT_ADD_TO_CART, priority = 100)
@XPFAssignment(extensionPoint = XPFExtensionPointEnum.VALIDATE_SHOPPING_ITEM_AT_CHECKOUT, priority = 100)
public class PickupRequiredAddToCartValidator extends XPFExtensionPointImpl implements ShoppingItemValidator {
/**
* Message id for this validation.
*/
public static final String MESSAGE_ID = "invalid.pickup.date";
@Override
public Collection<XPFStructuredErrorMessage> validate(final XPFShoppingItemValidationContext xpfShoppingItemValidationContext) {
if (xpfShoppingItemValidationContext.getShoppingItem().getProductSku().getProduct().getAttributeValueByKey("pickupRequired", null)
.map(value -> value.getValue().equals(Boolean.TRUE)).orElse(false)) {
PickupRequiredAddToCartValidatorPlugin.getInstance().getContext().getLogger().info("pickupRequired attribute found");
String pickupDateString = xpfShoppingItemValidationContext.getShoppingItem().getModifierFields().get("pickupDate");
if (StringUtils.isEmpty(pickupDateString)) {
return Collections.singletonList(XPFStructuredErrorMessage.builder()
.withMessageId(MESSAGE_ID)
.withDebugMessage("pickupDate cart item modifier must be set.")
.build());
}
LocalDate pickupDate = LocalDate.parse(pickupDateString);
if (pickupDate.isBefore(LocalDate.now().plus(7, ChronoUnit.DAYS))) {
return Collections.singletonList(XPFStructuredErrorMessage.builder()
.withMessageId(MESSAGE_ID)
.withDebugMessage("pickupDate cart item modifier must be set to at least one week in the future.")
.build());
}
}
return Collections.EMPTY_LIST;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment