Skip to content
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

refactor: updates to inventory counts and statuses #4859

Merged
merged 94 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
b9e827d
update definition of isBackorder to be current backorder state
kieckhafer Dec 11, 2018
1ac2538
Merge branch 'release-2.0.0-rc.8' into feat-kieckhafer-inventoryUpdates
kieckhafer Dec 11, 2018
8e4a261
add canBackorder to GQL
kieckhafer Dec 11, 2018
63b1e73
update test with correct canBackorder data
kieckhafer Dec 11, 2018
e312fdf
add inventoryAvailableToSell and inventoryInStock to graphql cart end…
kieckhafer Dec 11, 2018
6ed8880
depreciat currentQuantity on Cart endpoint
kieckhafer Dec 11, 2018
9d55621
fix typo in jsdoc
kieckhafer Dec 11, 2018
c23662c
add inventoryAvailableToSell, inventoryInStock to correct schemas
kieckhafer Dec 12, 2018
ec74859
calculate inventory data for the catalog
kieckhafer Dec 12, 2018
653d13e
push for visibility
kieckhafer Dec 12, 2018
8fe1ec4
getDirectParent -> getTopLevelVariant
kieckhafer Dec 12, 2018
98dc142
pass context into appEvents
kieckhafer Dec 12, 2018
8a2efae
getVariantInventoryAvailableToSellQuantity for parent variant
kieckhafer Dec 12, 2018
9512d3a
add getVariantInventoryAvailableToSellQuantity tests
kieckhafer Dec 12, 2018
59188f8
move inventory code into orders startup file
kieckhafer Dec 13, 2018
9fd0f80
add new data to tests
kieckhafer Dec 13, 2018
4a6bcbb
move afterOrderCreate inventory calculations to new inventory package
kieckhafer Dec 13, 2018
f8d5aaf
add new inventory numbers to top level product
kieckhafer Dec 13, 2018
a84d354
added inventory to variant level
kieckhafer Dec 13, 2018
c40ef63
fix wrong data provided
kieckhafer Dec 13, 2018
eb2ef30
fix typo: varaint -> variant
kieckhafer Dec 13, 2018
5eca095
new getProductInventoryAvailableToSell function
kieckhafer Dec 13, 2018
4174886
add inventory data to catalog auto publish
kieckhafer Dec 13, 2018
2d4034a
update tests with new inventory numbers
kieckhafer Dec 13, 2018
d20d694
mock product return
kieckhafer Dec 13, 2018
5a1ad1f
move function into new location
kieckhafer Dec 13, 2018
d04d035
Merge branch 'release-2.0.0-rc.8' into feat-kieckhafer-inventoryUpdates
kieckhafer Dec 14, 2018
12bc348
Merge remote-tracking branch 'origin/develop' into feat-kieckhafer-in…
kieckhafer Dec 15, 2018
a2f6f98
add inventoryQuantity getters for variant and product
kieckhafer Dec 15, 2018
71e809f
add quantity numbers in approve and cancel
kieckhafer Dec 15, 2018
a1ca382
fix data that is used to get numbers
kieckhafer Dec 15, 2018
b42d7d4
add getVariantAvailableToSellQuantity to ReactionProduct
kieckhafer Dec 17, 2018
9ed8c68
add translations
kieckhafer Dec 17, 2018
cf0d7b8
add inventoryCalculation when product inventory is updated
kieckhafer Dec 17, 2018
64679ea
add inventory fields to operator UI
kieckhafer Dec 17, 2018
c7caeea
add util to get "reserved" inventory
kieckhafer Dec 17, 2018
937ce44
add meteor method to recalculate all variant and product inventory wh…
kieckhafer Dec 17, 2018
633ddbb
rename meteor call
kieckhafer Dec 17, 2018
84c92b9
add and use new functions for repeated code
kieckhafer Dec 17, 2018
7207811
add util functions to update inventory quantities for parents
kieckhafer Dec 17, 2018
c8224d6
move files from catalog to inventory collection
kieckhafer Dec 18, 2018
2cd45dd
add canBackorder to test product schema
kieckhafer Dec 18, 2018
ffcd368
remove old test
kieckhafer Dec 18, 2018
3edd78f
fix lint issues
kieckhafer Dec 18, 2018
ed45210
remove new function we ended up not refactoring in this PR
kieckhafer Dec 18, 2018
a38f793
remove unused getVariantQuantity function
kieckhafer Dec 18, 2018
f3f6394
lint fixes
kieckhafer Dec 18, 2018
85cd7e6
update inventory status checks to use number on variant, not getVaria…
kieckhafer Dec 18, 2018
83aadf3
remove no longer used functions
kieckhafer Dec 18, 2018
f86964b
remove unused updateQuantityIfChildVariants function
kieckhafer Dec 18, 2018
7377de4
remove {Catalog/ReactionProduct}.getVariantQuantity
kieckhafer Dec 18, 2018
d596eea
move inventory related functions into inventory plugin
kieckhafer Dec 18, 2018
4eca220
fix test: data was incorrect, was trying to loop over a non-existant …
kieckhafer Dec 18, 2018
f0ac73a
remove unused import
kieckhafer Dec 18, 2018
641655a
filter through all shipping groups instead of just shipping[0]
kieckhafer Dec 19, 2018
57699af
add tests for getVariantInventoryNotAvailableToSellQuantity
kieckhafer Dec 19, 2018
9df6a69
Merge remote-tracking branch 'origin/develop' into feat-kieckhafer-in…
kieckhafer Dec 19, 2018
762bc17
add migration to update inventory data
kieckhafer Dec 20, 2018
ae56662
add top level variant to migration
kieckhafer Dec 20, 2018
b9c1a99
add canBackorder to migration
kieckhafer Dec 20, 2018
59a46ce
move descriptions into jsdoc
kieckhafer Dec 20, 2018
b6ef01b
remove canBackorder from product level
kieckhafer Dec 20, 2018
35ad564
remove extra check for active shop
kieckhafer Dec 20, 2018
54b711d
remove extra line of mapping
kieckhafer Dec 20, 2018
6f09dab
add ancestors projection to findOne
kieckhafer Dec 20, 2018
b1b3ad0
un-meteorify the code
kieckhafer Dec 20, 2018
dca6086
remove uneeded extra step of mapping
kieckhafer Dec 20, 2018
fab842a
remove meteor mongo code
kieckhafer Dec 20, 2018
c95c5cf
fix jsdoc function name
kieckhafer Dec 20, 2018
381ad53
remove unecessary async
kieckhafer Dec 20, 2018
dee9a02
fix: return data for isSoldOut
kieckhafer Dec 20, 2018
3ebbeb2
change ne to gt
kieckhafer Dec 20, 2018
1d1e4e5
remove brackets around strings
kieckhafer Dec 20, 2018
562a4b2
udpate updates to use bypasscollection2
kieckhafer Dec 20, 2018
b2c087f
remove contest, startup already gets it
kieckhafer Dec 20, 2018
421fd99
remove uneeded returns
kieckhafer Dec 20, 2018
87f7680
change === to <=
kieckhafer Dec 20, 2018
00f0c4b
reverse check to match server
kieckhafer Dec 20, 2018
dcd058c
move cancelOrder inventory adjustments into appEvent
kieckhafer Dec 20, 2018
f1292c3
fix appEvent name
kieckhafer Dec 20, 2018
827c551
add uniqueProducts map
kieckhafer Dec 20, 2018
ab8b7ac
change set to array
kieckhafer Dec 20, 2018
af11aa3
fix projection level
kieckhafer Dec 20, 2018
dbdc9c4
move product update to outside order for loop
kieckhafer Dec 20, 2018
282768c
move inventoryQuantity updates into appEvents
kieckhafer Dec 21, 2018
666bf92
move post field change inventory calculation into appEvents
kieckhafer Dec 21, 2018
57b15c0
pass object instead of individual values
kieckhafer Dec 21, 2018
9c37bf6
add workflow status to check for item availability
kieckhafer Dec 21, 2018
6fbbfbf
lint fixes
kieckhafer Dec 21, 2018
6b117cb
update test with new workflow field
kieckhafer Dec 21, 2018
ba513f9
add server side check for inventory availability
kieckhafer Jan 2, 2019
09e2b4c
return new document instead of original
kieckhafer Jan 2, 2019
14bd438
Merge remote-tracking branch 'origin/develop' into feat-kieckhafer-in…
kieckhafer Jan 2, 2019
d77f104
fix options in query
kieckhafer Jan 2, 2019
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
60 changes: 42 additions & 18 deletions imports/collections/schemas/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,17 @@ export const SocialMetadata = new SimpleSchema({
* @type {SimpleSchema}
* @property {String} _id required
* @property {String} barcode optional
* @property {Boolean} canBackorder required, Indicates when the seller has allowed the sale of product which is not in stock
* @property {Date} createdAt required
* @property {Number} height optional, default value: `0`
* @property {Number} index required
* @property {Boolean} inventoryManagement required
* @property {Boolean} inventoryPolicy required
* @property {Boolean} isBackorder required
* @property {Boolean} isLowQuantity required
* @property {Boolean} isSoldOut required
* @property {Boolean} inventoryAvailableToSell required, The quantity of this item currently available to sell. This number does not include reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator). If this is a variant, this number is created by summing all child option inventory numbers. This is most likely the quantity to display in the storefront UI.
* @property {Boolean} inventoryInStock required, The quantity of this item currently in stock. This number is updated when an order is processed by the operator. This number includes all inventory, including reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator). If this is a variant, this number is created by summing all child option inventory numbers. This is most likely just used as a reference in the operator UI, and not displayed in the storefront UI. Called `inventoryQuantity` in the Product Schema, and `inventoryInStock` in the Catalog schema.
* @property {Boolean} inventoryManagement required, True if inventory management is enabled for this variant
* @property {Boolean} inventoryPolicy required, True if inventory policy is enabled for this variant
* @property {Boolean} isBackorder required, Indicates when a product is currently backordered
* @property {Boolean} isLowQuantity required, Indicates that the product quantity is too low
* @property {Boolean} isSoldOut required, Indicates when the product quantity is zero
* @property {Number} length optional, default value: `0`
* @property {Number} lowInventoryWarningThreshold optional, default value: `0`
* @property {ImageInfo[]} media optional
Expand Down Expand Up @@ -158,6 +161,10 @@ export const VariantBaseSchema = new SimpleSchema({
label: "Barcode",
optional: true
},
"canBackorder": {
type: Boolean,
label: "Can backorder"
},
"createdAt": {
type: Date,
label: "Date/time this variant was created at"
Expand All @@ -173,26 +180,34 @@ export const VariantBaseSchema = new SimpleSchema({
type: SimpleSchema.Integer,
label: "The position of this variant among other variants at the same level of the product-variant-option hierarchy"
},
"inventoryAvailableToSell": {
type: SimpleSchema.Integer,
label: "Inventory available to sell"
},
"inventoryInStock": {
type: SimpleSchema.Integer,
label: "Inventory in stock"
},
"inventoryManagement": {
type: Boolean,
label: "True if inventory management is enabled for this variant"
label: "Inventory management"
},
"inventoryPolicy": {
type: Boolean,
label: "True if inventory policy is enabled for this variant"
label: "Inventory policy"
},
"isBackorder": {
type: Boolean,
label: "Indicates when the seller has allowed the sale of product which is not in stock",
label: "Is backordered",
defaultValue: false
},
"isLowQuantity": {
type: Boolean,
label: "Indicates that the product quantity is too low"
label: "Is low quantity"
},
"isSoldOut": {
type: Boolean,
label: "Indicates when the product quantity is zero"
label: "Is sold out"
},
"length": {
type: Number,
Expand Down Expand Up @@ -320,10 +335,11 @@ export const CatalogVariantSchema = VariantBaseSchema.clone().extend({
* @property {Date} createdAt required
* @property {String} description optional
* @property {Number} height optional, default value: `0`
* @property {Boolean} isBackorder required
* @property {Boolean} isDeleted required, default value: `false`
* @property {Boolean} isLowQuantity required
* @property {Boolean} isSoldOut required
* @property {Boolean} inventoryAvailableToSell required, The quantity of this item currently available to sell. This number does not include reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator). If this is a variant, this number is created by summing all child option inventory numbers. This is most likely the quantity to display in the storefront UI.
* @property {Boolean} inventoryInStock required, The quantity of this item currently in stock. This number is updated when an order is processed by the operator. This number includes all inventory, including reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator). If this is a variant, this number is created by summing all child option inventory numbers. This is most likely just used as a reference in the operator UI, and not displayed in the storefront UI. Called `inventoryQuantity` in the Product Schema, and `inventoryInStock` in the Catalog schema.
* @property {Boolean} isBackorder required, Indicates when a product is currently backordered
* @property {Boolean} isLowQuantity required, Indicates that the product quantity is too low
* @property {Boolean} isSoldOut required, Indicates when the product quantity is zero
* @property {Boolean} isVisible required, default value: `false`
* @property {Number} length optional, default value: `0`
* @property {Number} lowInventoryWarningThreshold optional, default value: `0`
Expand Down Expand Up @@ -380,23 +396,31 @@ export const CatalogProduct = new SimpleSchema({
optional: true,
defaultValue: 0
},
"inventoryAvailableToSell": {
type: SimpleSchema.Integer,
label: "Inventory available to sell"
},
"inventoryInStock": {
type: SimpleSchema.Integer,
label: "Inventory in stock"
},
"isBackorder": {
type: Boolean,
label: "Indicates when the seller has allowed the sale of product which is not in stock"
label: "Is backorder"
},
"isDeleted": {
type: Boolean,
label: "Indicates when a product is archived",
label: "Is deleted",
index: 1,
defaultValue: false
},
"isLowQuantity": {
type: Boolean,
label: "Indicates that the product quantity is too low"
label: "Is low quantity"
},
"isSoldOut": {
type: Boolean,
label: "Indicates when the product quantity is zero"
label: "Is sold out"
},
"isVisible": {
type: Boolean,
Expand Down
48 changes: 43 additions & 5 deletions imports/collections/schemas/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ registerSchema("VariantMedia", VariantMedia);
* @property {Event[]} eventLog optional, Variant Event Log
* @property {Number} height optional, default value: `0`
* @property {Number} index optional, Variant position number in list. Keep array index for moving variants in a list.
* @property {Boolean} inventoryAvailableToSell required
* @property {Boolean} inventoryInStock required
* @property {Boolean} inventoryManagement, default value: `true`
* @property {Boolean} inventoryPolicy, default value: `false`, If disabled, item can be sold even if it not in stock.
* @property {Number} inventoryQuantity, default value: `0`
Expand Down Expand Up @@ -168,15 +170,29 @@ export const ProductVariant = new SimpleSchema({
}
}
},
"inventoryAvailableToSell": {
type: SimpleSchema.Integer,
label: "The quantity of this item currently available to sell." +
"This number is updated when an order is placed by the customer." +
"This number does not include reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator)." +
"If this is a variant, this number is created by summing all child option inventory numbers." +
"This is most likely the quantity to display in the storefront UI.",
optional: true,
defaultValue: 0
},
"inventoryQuantity": {
type: SimpleSchema.Integer,
label: "Quantity",
label: "The quantity of this item currently in stock." +
"This number is updated when an order is processed by the operator." +
"This number includes all inventory, including reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator)." +
"If this is a variant, this number is created by summing all child option inventory numbers." +
"This is most likely just used as a reference in the operator UI, and not displayed in the storefront UI." +
"Called `inventoryQuantity` in the Product Schema, and `inventoryInStock` in the Catalog schema.",
optional: true,
defaultValue: 0
},
"isBackorder": {
label: "Indicates when the seller has allowed the sale of product which" +
" is not in stock",
label: "Indicates when a product is currently backordered",
type: Boolean,
optional: true
},
Expand Down Expand Up @@ -339,6 +355,8 @@ registerSchema("PriceRange", PriceRange);
* @property {String} googleplusMsg optional
* @property {String} handle optional, slug
* @property {String[]} hashtags optional
* @property {Boolean} inventoryAvailableToSell required
* @property {Boolean} inventoryInStock required
* @property {Boolean} isBackorder denormalized, `true` if product not in stock, but customers anyway could order it
* @property {Boolean} isDeleted, default value: `false`
* @property {Boolean} isLowQuantity denormalized, true when at least 1 variant is below `lowInventoryWarningThreshold`
Expand Down Expand Up @@ -414,9 +432,29 @@ export const Product = new SimpleSchema({
"hashtags.$": {
type: String
},
"inventoryAvailableToSell": {
type: SimpleSchema.Integer,
label: "The quantity of this item currently available to sell." +
"This number is updated when an order is placed by the customer." +
"This number does not include reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator)." +
"If this is a variant, this number is created by summing all child option inventory numbers." +
"This is most likely the quantity to display in the storefront UI.",
optional: true,
defaultValue: 0
},
"inventoryQuantity": {
type: SimpleSchema.Integer,
label: "The quantity of this item currently in stock." +
"This number is updated when an order is processed by the operator." +
"This number includes all inventory, including reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator)." +
"If this is a variant, this number is created by summing all child option inventory numbers." +
"This is most likely just used as a reference in the operator UI, and not displayed in the storefront UI." +
"Called `inventoryQuantity` in the Product Schema, and `inventoryInStock` in the Catalog schema.",
optional: true,
defaultValue: 0
},
"isBackorder": {
label: "Indicates when the seller has allowed the sale of product which" +
" is not in stock",
label: "Indicates when a product is currently backordered",
type: Boolean,
optional: true
},
Expand Down
14 changes: 12 additions & 2 deletions imports/plugins/core/cart/server/no-meteor/schemas/cart.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ type CartItem implements Node {
"The current comparison (e.g., MSRP) price of the item"
compareAtPrice: Money

"The quantity of this item currently available to order"
currentQuantity: Int
"""
The quantity of this item currently available to order.
"""
currentQuantity: Int @deprecated(reason: "Use `inventoryAvailableToSell`.")

"""
The date and time at which the cart item was created. If an item is added, removed, and then added again,
Expand All @@ -104,6 +106,14 @@ type CartItem implements Node {
"The URLs for a picture of the item in various sizes"
imageURLs: ImageSizes

"""
The quantity of this item currently available to sell.
This number is updated when an order is placed by the customer.
This number does not include reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator).
This is most likely the quantity to display in the storefront UI.
"""
inventoryAvailableToSell: Int

"Is this item currently backordered?"
isBackorder: Boolean!

Expand Down
10 changes: 7 additions & 3 deletions imports/plugins/core/catalog/server/methods/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import Reaction from "/imports/plugins/core/core/server/Reaction";
import ReactionError from "@reactioncommerce/reaction-error";
import { MediaRecords, Products, Tags } from "/lib/collections";
import { Media } from "/imports/plugins/core/files/server";
import appEvents from "/imports/node-app/core/util/appEvents";
import rawCollections from "/imports/collections/rawCollections";
import getGraphQLContextInMeteorMethod from "/imports/plugins/core/graphql/server/getGraphQLContextInMeteorMethod";
import hashProduct from "../no-meteor/mutations/hashProduct";
import getCurrentCatalogPriceForProductConfiguration from "../no-meteor/queries/getCurrentCatalogPriceForProductConfiguration";
import getProductPriceRange from "../no-meteor/utils/getProductPriceRange";
import getVariants from "../no-meteor/utils/getVariants";
import hasChildVariant from "../no-meteor/utils/hasChildVariant";
import isSoldOut from "../no-meteor/utils/isSoldOut";
import isLowQuantity from "../no-meteor/utils/isLowQuantity";
import isBackorder from "../no-meteor/utils/isBackorder";
import isSoldOut from "/imports/plugins/core/inventory/server/no-meteor/utils/isSoldOut";
import isLowQuantity from "/imports/plugins/core/inventory/server/no-meteor/utils/isLowQuantity";
import isBackorder from "/imports/plugins/core/inventory/server/no-meteor/utils/isBackorder";

/* eslint new-cap: 0 */
/* eslint no-loop-func: 0 */
Expand Down Expand Up @@ -997,6 +998,9 @@ Meteor.methods({
denormalize(doc.ancestors[0], field);
}
}

appEvents.emit("afterVariantUpdate", { _id, field, value });

return update;
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,26 @@ type CatalogProduct implements CatalogProductOrVariant & Node {
"The height of the product, if it has physical dimensions"
height: Float

"True if isSoldOut is true AND none of the variants have an inventoryPolicy set"
"""
The quantity of this item currently available to sell.
This number is updated when an order is placed by the customer.
This number does not include reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator).
This number is calculated by summing all child variant inventory numbers.
This is most likely the quantity to display in the storefront UI.
"""
inventoryAvailableToSell: Int

"""
The quantity of this item currently in stock.
This number is updated when an order is processed by the operator.
This number includes all inventory, including reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator).
This number is calculated by summing all child variant inventory numbers.
This is most likely just used as a reference in the operator UI, and not displayed in the storefront UI.
Called `inventoryQuantity` in the Product Schema, and `inventoryInStock` in the Catalog schema.
"""
inventoryInStock: Int

"True if isSoldOut is true AND none of the variants have an inventoryPolicy set AND available inventory is 0"
isBackorder: Boolean!

"True if this product has been deleted. Typically, deleted products are not returned in queries."
Expand Down Expand Up @@ -247,6 +266,9 @@ type CatalogProductVariant implements CatalogProductOrVariant & Node {
"The product variant barcode value, if it has one"
barcode: String

"Indicates when the seller has allowed the sale of product which is not in stock. True if inventoryManagement is true AND none of the variants have an inventoryPolicy set."
canBackorder: Boolean!

"The date and time at which this CatalogProductVariant was created, which is when the related product was first published"
createdAt: DateTime

Expand All @@ -256,13 +278,32 @@ type CatalogProductVariant implements CatalogProductOrVariant & Node {
"The position of this variant among other variants at the same level of the product-variant-option hierarchy"
index: Int!

"""
The quantity of this item currently available to sell.
This number is updated when an order is placed by the customer.
This number does not include reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator).
If this is a variant, this number is created by summing all child option inventory numbers.
This is most likely the quantity to display in the storefront UI.
"""
inventoryAvailableToSell: Int

"""
The quantity of this item currently in stock.
This number is updated when an order is processed by the operator.
This number includes all inventory, including reserved inventory (i.e. inventory that has been ordered, but not yet processed by the operator).
If this is a variant, this number is created by summing all child option inventory numbers.
This is most likely just used as a reference in the operator UI, and not displayed in the storefront UI.
Called `inventoryQuantity` in the Product Schema, and `inventoryInStock` in the Catalog schema.
"""
inventoryInStock: Int

"True if inventory management is enabled for this variant"
inventoryManagement: Boolean!

"True if inventory policy is enabled for this variant"
inventoryPolicy: Boolean!

"True if isSoldOut is true AND none of the variants have an inventoryPolicy set"
"True if isSoldOut is true AND none of the variants have an inventoryPolicy set AND available inventory is 0"
isBackorder: Boolean!

"True if inventoryManagement is enabled and this variant has an available quantity less than its lowInventoryWarningThreshold"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @method canBackorder
* @summary If all the products variants have inventory policy disabled and inventory management enabled
* @memberof Catalog
* @param {Object[]} variants - Array with product variant objects
* @return {boolean} is backorder allowed or not for a product
*/
export default function canBackorder(variants) {
return variants.every((variant) => !variant.inventoryPolicy && variant.inventoryManagement);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import canBackorder from "./canBackorder";

// mock variant
const mockVariantWithBackorder = {
inventoryManagement: true,
inventoryPolicy: false
};

const mockVariantWithOutBackorder = {
inventoryManagement: true,
inventoryPolicy: true
};

const mockVariantWithOutInventory = {
inventoryManagement: false,
inventoryPolicy: false
};


test("expect true when a single product variant is sold out and has inventory policy disabled", () => {
const spec = canBackorder([mockVariantWithBackorder]);
expect(spec).toBe(true);
});

test("expect true when an array of product variants are sold out and have inventory policy disabled", () => {
const spec = canBackorder([mockVariantWithBackorder, mockVariantWithBackorder]);
expect(spec).toBe(true);
});

test("expect false when a single product variant is sold out and has inventory policy enabled", () => {
const spec = canBackorder([mockVariantWithOutBackorder]);
expect(spec).toBe(false);
});

test("expect false when a single product variant is sold out and has inventory controls disabled", () => {
const spec = canBackorder([mockVariantWithOutInventory]);
expect(spec).toBe(false);
});
Loading