+
Skip to content

fix: allow restrictions on nested properties #6578

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

Merged
merged 2 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/modern-poems-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactioncommerce/api-plugin-shipments-flat-rate": patch
---

allow nested properties to be accessible for shipment restrictions.
3 changes: 2 additions & 1 deletion packages/api-plugin-shipments-flat-rate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"@reactioncommerce/logger": "^1.1.3",
"@reactioncommerce/random": "^1.0.2",
"@reactioncommerce/reaction-error": "^1.0.1",
"simpl-schema": "^1.12.0"
"simpl-schema": "^1.12.0",
"lodash": "^4.17.15"
},
"devDependencies": {
"@babel/core": "^7.7.7",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from "lodash";
import operators from "@reactioncommerce/api-utils/operators.js";
import propertyTypes from "@reactioncommerce/api-utils/propertyTypes.js";
import isDestinationRestricted from "./isDestinationRestricted.js";
Expand Down Expand Up @@ -25,12 +26,13 @@ export async function attributeDenyCheck(methodRestrictions, method, hydratedOrd

// Item must meet all attributes to be restricted
return attributes.every((attribute) => {
let attributeFound = operators[attribute.operator](item[attribute.property], propertyTypes[attribute.propertyType](attribute.value));
const attributePropertyValue = _.get(item, attribute.property);
let attributeFound = operators[attribute.operator](attributePropertyValue, propertyTypes[attribute.propertyType](attribute.value));

// If attribute is an array on the item, use `includes` instead of checking for ===
// This works for tags, tagIds, and any future attribute that might be an array
if (Array.isArray(item[attribute.property])) {
attributeFound = item[attribute.property].includes(attribute.value);
if (Array.isArray(attributePropertyValue)) {
attributeFound = attributePropertyValue.includes(attribute.value);
}

if (attributeFound) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,17 @@ const mockHydratedOrderItems = {
variantId: "tMkp5QwZog5ihYTfG",
weight: 50,
width: 10,
tags: [Array]
tags: [Array],
subtotal: {
amount: 12.99,
currencyCode: "USD"
},
parcel: {
height: 10,
weight: 50,
width: 10,
length: 10
}
};

const mockHydratedOrder = {
Expand Down Expand Up @@ -814,3 +824,82 @@ test("deny method - multiple attributes - item value is less than $100 AND item

expect(allowedMethods).toEqual([]);
});

/*
* Tests with nested item/attribute restrictions
*/
test("deny method - nested attribute - subtotal.amount is less than $100, item restricted", async () => {
const mockMethodRestrictions = [
{
_id: "allow001",
methodIds: [
"stviZaLdqRvTKW6J5"
],
type: "allow",
destination: {
country: [
"US"
]
}
},
{
_id: "deny001",
methodIds: [
"stviZaLdqRvTKW6J5"
],
type: "deny",
attributes: [
{
property: "subtotal.amount",
value: 100,
propertyType: "int",
operator: "lt"
}
]
}
];

mockContext.collections.FlatRateFulfillmentRestrictions.toArray.mockReturnValue(Promise.resolve(mockMethodRestrictions));

const allowedMethods = await filterShippingMethods(mockContext, mockShippingMethod, mockHydratedOrder);

expect(allowedMethods).toEqual([]);
});

test("deny method - nested attributes - parcel.weight is greater than 50, no item restrictions", async () => {
const mockMethodRestrictions = [
{
_id: "allow001",
methodIds: [
"stviZaLdqRvTKW6J5"
],
type: "allow",
destination: {
country: [
"US"
]
}
},
{
_id: "deny001",
methodIds: [
"stviZaLdqRvTKW6J5"
],
type: "deny",
attributes: [
{
property: "parcel.weight",
value: 50,
propertyType: "int",
operator: "gt"
}
]
}
];

mockContext.collections.FlatRateFulfillmentRestrictions.toArray.mockReturnValue(Promise.resolve(mockMethodRestrictions));

const allowedMethods = await filterShippingMethods(mockContext, mockShippingMethod, mockHydratedOrder);

expect(allowedMethods).toEqual(mockShippingMethod);
});
8 changes: 5 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载