From 3fe18144731c77404aaf8bd236ab0ada9c2aba48 Mon Sep 17 00:00:00 2001 From: Alex Haproff Date: Fri, 12 Jul 2019 12:54:18 +0000 Subject: [PATCH 1/3] fix(5313-dataloaders-not-found): use SimpleInventory collection to fetch inventory docs when the call is made internally, i.e. in the consumer Signed-off-by: Alex Haproff --- .../inventoryForProductConfigurations.js | 13 ++++++++- .../inventoryForProductConfigurations.test.js | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js index 23e5328327a..0c2cc8f32ca 100644 --- a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js +++ b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js @@ -16,10 +16,21 @@ import isEqual from "lodash/isEqual"; */ export default async function inventoryForProductConfigurations(context, input) { const { productConfigurations } = input; + const { isInternalCall } = context; const productVariantIds = productConfigurations.map(({ productVariantId }) => productVariantId); - const inventoryDocs = await context.dataLoaders.SimpleInventoryByProductVariantId.loadMany(productVariantIds); + let inventoryDocs; + if (isInternalCall) { + inventoryDocs = await context.collections.SimpleInventory + .find({ + "productConfiguration.productVariantId": { $in: productVariantIds } + }) + .limit(productConfigurations.length) // optimize query speed + .toArray(); + } else { + inventoryDocs = await context.dataLoaders.SimpleInventoryByProductVariantId.loadMany(productVariantIds); + } return productConfigurations.map((productConfiguration) => { const inventoryDoc = inventoryDocs.find((doc) => { diff --git a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js index b9816c344d0..6463438aa30 100644 --- a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js +++ b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js @@ -18,6 +18,18 @@ const context = { } ]) } + }, + collections: { + SimpleInventory: { + find: jest.fn(() => ({ + toArray() { + return []; + }, + limit() { + return this; + } + })) + } } }; @@ -35,3 +47,18 @@ test("calls SimpleInventoryByProductVariantId dataloader with correct product va await inventoryForProductConfigurations(context, input); expect(context.dataLoaders.SimpleInventoryByProductVariantId.loadMany).toHaveBeenCalledWith(["variant-2", "variant-1"]); }); + +test("calls SimpleInventory.find() on Mongo Collection when isInternalCall set to true", async () => { + const input = { + productConfigurations: [ + { + productVariantId: "variant-2" + }, + { + productVariantId: "variant-1" + } + ] + }; + await inventoryForProductConfigurations({ ...context, isInternalCall: true }, input); + expect(context.collections.SimpleInventory.find).toHaveBeenCalledWith({ "productConfiguration.productVariantId": { $in: ["variant-2", "variant-1"] } }); +}); From 880741eac7f3fa007aaf3e98c83a7f4e8a7316e6 Mon Sep 17 00:00:00 2001 From: Alex Haproff Date: Mon, 15 Jul 2019 10:12:22 +0000 Subject: [PATCH 2/3] fix(5313-dataloaders-not-found): only use dataloaders when registered Signed-off-by: Alex Haproff --- .../utils/inventoryForProductConfigurations.js | 11 ++++------- .../utils/inventoryForProductConfigurations.test.js | 8 ++++++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js index 0c2cc8f32ca..45fcd14695b 100644 --- a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js +++ b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js @@ -16,21 +16,18 @@ import isEqual from "lodash/isEqual"; */ export default async function inventoryForProductConfigurations(context, input) { const { productConfigurations } = input; - const { isInternalCall } = context; + const { collections, dataLoaders } = context; const productVariantIds = productConfigurations.map(({ productVariantId }) => productVariantId); - let inventoryDocs; - if (isInternalCall) { - inventoryDocs = await context.collections.SimpleInventory + const inventoryDocs = dataLoaders + ? await dataLoaders.SimpleInventoryByProductVariantId.loadMany(productVariantIds) + : await collections.SimpleInventory .find({ "productConfiguration.productVariantId": { $in: productVariantIds } }) .limit(productConfigurations.length) // optimize query speed .toArray(); - } else { - inventoryDocs = await context.dataLoaders.SimpleInventoryByProductVariantId.loadMany(productVariantIds); - } return productConfigurations.map((productConfiguration) => { const inventoryDoc = inventoryDocs.find((doc) => { diff --git a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js index 6463438aa30..782d7e5082c 100644 --- a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js +++ b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.test.js @@ -48,7 +48,7 @@ test("calls SimpleInventoryByProductVariantId dataloader with correct product va expect(context.dataLoaders.SimpleInventoryByProductVariantId.loadMany).toHaveBeenCalledWith(["variant-2", "variant-1"]); }); -test("calls SimpleInventory.find() on Mongo Collection when isInternalCall set to true", async () => { +test("calls SimpleInventory.find() on Mongo Collection when DataLoader is not registered", async () => { const input = { productConfigurations: [ { @@ -59,6 +59,10 @@ test("calls SimpleInventory.find() on Mongo Collection when isInternalCall set t } ] }; - await inventoryForProductConfigurations({ ...context, isInternalCall: true }, input); + + const newContext = { ...context }; + delete newContext.dataLoaders; + + await inventoryForProductConfigurations(newContext, input); expect(context.collections.SimpleInventory.find).toHaveBeenCalledWith({ "productConfiguration.productVariantId": { $in: ["variant-2", "variant-1"] } }); }); From 10b266d2660ac907be1e390d2c420bf99539ae4a Mon Sep 17 00:00:00 2001 From: Alex Haproff Date: Mon, 15 Jul 2019 10:13:41 +0000 Subject: [PATCH 3/3] fix(5313-dataloaders-not-found): eslint fix Signed-off-by: Alex Haproff --- .../server/no-meteor/utils/inventoryForProductConfigurations.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js index 45fcd14695b..ed2c1b2d518 100644 --- a/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js +++ b/imports/plugins/included/simple-inventory/server/no-meteor/utils/inventoryForProductConfigurations.js @@ -20,7 +20,7 @@ export default async function inventoryForProductConfigurations(context, input) const productVariantIds = productConfigurations.map(({ productVariantId }) => productVariantId); - const inventoryDocs = dataLoaders + const inventoryDocs = dataLoaders ? await dataLoaders.SimpleInventoryByProductVariantId.loadMany(productVariantIds) : await collections.SimpleInventory .find({