Skip to content

Commit

Permalink
feat: support nested accessor for order attributes restriction
Browse files Browse the repository at this point in the history
Signed-off-by: tedraykov <[email protected]>
  • Loading branch information
tedraykov committed Feb 20, 2022
1 parent 6b05ae6 commit deadff5
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/util/orderMatchesOrderAttributesRestriction.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
import operators from "@reactioncommerce/api-utils/operators.js";
import propertyTypes from "@reactioncommerce/api-utils/propertyTypes.js";

/**
* @summary Access nested object property providing accessor with "." separator
* Example:
* const object = {
* property: {
* value: 10
* }
* }
* getNestedProperty(object, "property.value") // Outputs: 10
* @param {Object} object - the object
* @param {Object} propertyAccessorString - property accessor with "." separator
* @returns {Object | undefined} - the property of the object
*/
function getNestedProperty(object, propertyAccessorString) {
if (!propertyAccessorString) return object;

const propertyAccessors = propertyAccessorString.split(".");
let resultObjectProperty = object;

for (const propertyAccessor of propertyAccessors) {
resultObjectProperty = resultObjectProperty[propertyAccessor];

if (resultObjectProperty === undefined) {
break;
}
}

return resultObjectProperty;
}

/**
* @summary Check whether an attribute of an order matches with a provided value based on a provided comparison operator
* @param {Object} order - a common order
* @param {Object} orderAttribute - order attribute restriction definition
* @returns {boolean} true / false whether an order attribute matches a value
*/
function orderMatchesAttribute(order, { operator, property, propertyType, value }) {
const orderProperty = property.includes(".") ?
getNestedProperty(order, property) :
order[property];

return operators[operator](
order[property],
orderProperty,
propertyTypes[propertyType](value)
);
}
Expand Down

0 comments on commit deadff5

Please sign in to comment.