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

Add ENV to disable inventory auto-publish #5149

Merged
merged 4 commits into from Apr 25, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -36,15 +36,9 @@ const variantFieldsThatNeedPublishing = [
"compareAtPrice",
"height",
"index",
"inventoryManagement",
"inventoryPolicy",
"inventoryInStock",
"isDeleted",
"isLowQuantity",
"isSoldOut",
"isVisible",
"length",
"lowInventoryWarningThreshold",
"metafields",
"minOrderQuantity",
"optionTitle",
Expand Down
34 changes: 34 additions & 0 deletions imports/plugins/core/inventory/register.js
@@ -1,8 +1,38 @@
import Reaction from "/imports/plugins/core/core/server/Reaction";
import config from "./server/config";
import startup from "./server/no-meteor/startup";
import schemas from "./server/no-meteor/schemas";
import xformCatalogBooleanFilters from "./server/no-meteor/utils/xformCatalogBooleanFilters";

const publishedProductFields = [];

// These require manual publication always
const publishedProductVariantFields = [
"inventoryManagement",
"inventoryPolicy"
];

// Additional fields require manual publication only if they are
// not auto-published on every variant update.
if (!config.AUTO_PUBLISH_INVENTORY_FIELDS) {
publishedProductFields.push(
"inventoryAvailableToSell",
"inventoryInStock",
"isBackorder",
"isLowQuantity",
"isSoldOut"
);

publishedProductVariantFields.push(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not clear an why the variant field are outside of the if statement above? From my understanding, it should be an all or nothing update. Meaning, all inventory fields across top level product variants and options

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@willopez If I am reading the auto-publishing code correctly, then I don't see where it updates those two fields. It auto-publishes the others but not those. So I left them as always needing to be manually published.

I agree it may be best for it to work as you say, all fields, but I would treat that as a separate PR because it's a bigger change and I'm not sure what the ramifications would be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that sounds good. Lets create another issue to follow up on the other fields in the future.

"inventoryAvailableToSell",
"inventoryInStock",
"isBackorder",
"isLowQuantity",
"isSoldOut",
"lowInventoryWarningThreshold"
);
}

Reaction.registerPackage({
label: "Inventory",
name: "reaction-inventory",
Expand All @@ -13,5 +43,9 @@ Reaction.registerPackage({
},
graphQL: {
schemas
},
catalog: {
publishedProductFields,
publishedProductVariantFields
}
});
5 changes: 5 additions & 0 deletions imports/plugins/core/inventory/server/config.js
@@ -0,0 +1,5 @@
import envalid, { bool } from "envalid";

export default envalid.cleanEnv(process.env, {
AUTO_PUBLISH_INVENTORY_FIELDS: bool({ default: true })
});
9 changes: 6 additions & 3 deletions imports/plugins/core/inventory/server/no-meteor/startup.js
@@ -1,5 +1,6 @@
import updateCatalogProductInventoryStatus from "/imports/plugins/core/catalog/server/no-meteor/utils/updateCatalogProductInventoryStatus";
import config from "../config";
import getVariantInventoryNotAvailableToSellQuantity from "./utils/getVariantInventoryNotAvailableToSellQuantity";
import updateCatalogProductInventoryStatus from "./utils/updateCatalogProductInventoryStatus";
import updateParentInventoryFields from "./utils/updateParentInventoryFields";

/**
Expand Down Expand Up @@ -197,7 +198,7 @@ export default function startup(context) {

appEvents.on("afterVariantUpdate", async ({ _id, field }) => {
// If the updated field was `inventoryInStock`, adjust `inventoryAvailableToSell` quantities
if (field === "inventoryInStock") {
if (field === "inventoryInStock" || field === "lowInventoryWarningThreshold") {
const doc = await collections.Products.findOne({ _id });

// Get reserved inventory - the inventory currently in an unprocessed order
Expand All @@ -221,7 +222,9 @@ export default function startup(context) {
await updateParentInventoryFields(doc, collections);

// Publish inventory to catalog
await updateCatalogProductInventoryStatus(doc.ancestors[0], collections);
if (config.AUTO_PUBLISH_INVENTORY_FIELDS) {
await updateCatalogProductInventoryStatus(doc.ancestors[0], collections);
}
}
});
}
Expand Up @@ -3,9 +3,9 @@ import getVariants from "/imports/plugins/core/catalog/server/no-meteor/utils/ge
/**
*
* @method getProductInventoryAvailableToSellQuantity
* @summary Get the number of product variants still avalible to sell. This calculates based off of `inventoryAvailableToSell`.
* This function can take only a top product ID and a mongo collection as params to return the product
* `inventoryAvailableToSell` quantity, which is a calculation of the sum of all variant `inventoryAvailableToSell` quantities.
* @summary This function can take only a top product ID and a mongo collection as params to return the product
* `inventoryAvailableToSell` quantity, which is a calculation of the sum of all variant
* `inventoryAvailableToSell` quantities.
* @param {Object} productId - A top level product variant object.
* @param {Object} collections - Raw mongo collections.
* @param {Object[]} variants - Array of product variant option objects.
Expand Down
Expand Up @@ -3,9 +3,8 @@ import getVariants from "/imports/plugins/core/catalog/server/no-meteor/utils/ge
/**
*
* @method getProductInventoryInStockQuantity
* @summary Get the number of product variants still avalible to sell. This calculates based off of `inventoryInStock`.
* This function can take only a top product ID and a mongo collection as params to return the product
* `inventoryInStock` quantity, which is a calculation of the sum of all variant `inventoryInStock` quantities.
* @summary This function can take only a top product ID and a mongo collection as params to return the product
* `inventoryInStock` quantity, which is a calculation of the sum of all variant `inventoryInStock` quantities.
* @param {Object} productId - A top level product variant object.
* @param {Object} collections - Raw mongo collections.
* @param {Object[]} variants - Array of product variant option objects.
Expand Down
Expand Up @@ -3,7 +3,7 @@ import getVariants from "/imports/plugins/core/catalog/server/no-meteor/utils/ge
/**
*
* @method getVariantInventoryAvailableToSellQuantity
* @summary Get the number of product variants still avalible to sell. This calculates based off of `inventoryAvailableToSell`.
* @summary Get the number of product variants still av to sell. This calculates based off of `inventoryAvailableToSell`.
* This function can take only a top level variant object and a mongo collection as params to return the product
* variant quantity. This method can also take a top level variant, mongo collection and an array of
* product variant options as params to skip the db lookup and return the variant quantity
Expand Down
@@ -1,8 +1,8 @@
import Logger from "@reactioncommerce/logger";
import _ from "lodash";
import isBackorder from "/imports/plugins/core/inventory/server/no-meteor/utils/isBackorder";
import isLowQuantity from "/imports/plugins/core/inventory/server/no-meteor/utils/isLowQuantity";
import isSoldOut from "/imports/plugins/core/inventory/server/no-meteor/utils/isSoldOut";
import isBackorder from "./isBackorder";
import isLowQuantity from "./isLowQuantity";
import isSoldOut from "./isSoldOut";

/**
*
Expand Down
@@ -1,7 +1,7 @@
import mockContext from "/imports/test-utils/helpers/mockContext";
import { rewire as rewire$isBackorder, restore as restore$isBackorder } from "/imports/plugins/core/inventory/server/no-meteor/utils/isBackorder";
import { rewire as rewire$isLowQuantity, restore as restore$isLowQuantity } from "/imports/plugins/core/inventory/server/no-meteor/utils/isLowQuantity";
import { rewire as rewire$isSoldOut, restore as restore$isSoldOut } from "/imports/plugins/core/inventory/server/no-meteor/utils/isSoldOut";
import { rewire as rewire$isBackorder, restore as restore$isBackorder } from "./isBackorder";
import { rewire as rewire$isLowQuantity, restore as restore$isLowQuantity } from "./isLowQuantity";
import { rewire as rewire$isSoldOut, restore as restore$isSoldOut } from "./isSoldOut";
import updateCatalogProductInventoryStatus from "./updateCatalogProductInventoryStatus";

const mockCollections = { ...mockContext.collections };
Expand Down
@@ -1,6 +1,6 @@
import getTopLevelVariant from "/imports/plugins/core/catalog/server/no-meteor/utils/getTopLevelVariant";
import getProductInventoryAvailableToSellQuantity from "./getProductInventoryAvailableToSellQuantity.js";
import getProductInventoryAvailableToSellQuantity from "./getProductInventoryAvailableToSellQuantity";
import getProductInventoryInStockQuantity from "./getProductInventoryInStockQuantity";
import getTopLevelVariant from "./getTopLevelVariant";
import getVariantInventoryAvailableToSellQuantity from "./getVariantInventoryAvailableToSellQuantity";
import getVariantInventoryInStockQuantity from "./getVariantInventoryInStockQuantity";

Expand Down