Skip to content

Commit

Permalink
Merge pull request #547 from newsiberian/inventory_register_refactoring
Browse files Browse the repository at this point in the history
inventory/register refactoring
  • Loading branch information
Aaron Judd committed Dec 5, 2015
2 parents c8fe12d + 0352d23 commit d0e33af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/reaction-inventory/server/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,24 @@ Meteor.methods({
`inserting ${newQty - inventoryVariantCount} new inventory items for ${variant._id}`
);

let items = [];
while (i <= newQty) {
let inventoryItem = ReactionCore.Collections.Inventory.insert({
items.push({
shopId: product.shopId,
variantId: variant._id,
productId: product._id
});

if (!inventoryItem) {
// throw new Meteor.Error("Inventory Anomaly Detected. Abort! Abort!");
return totalNewInventory;
}
ReactionInventory.Log.debug(`registered ${inventoryItem}`);
totalNewInventory++;
i++;
}

let inventoryItem = ReactionCore.Collections.Inventory.batchInsert(items);

if (!inventoryItem) { // or maybe `inventory.length === 0`?
// throw new Meteor.Error("Inventory Anomaly Detected. Abort! Abort!");
return totalNewInventory;
}
ReactionInventory.Log.debug(`registered ${inventoryItem}`);
totalNewInventory += inventoryItem.length;
}
}
// returns the total amount of new inventory created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ describe("inventory method", function () {
expect(_.size(inventory)).toEqual(quantity);
done();
});

it("should add inventory items for a new child variant", done => {
spyOn(Roles, "userIsInRole").and.returnValue(true);
let product = Factory.create("product");
const productId = product._id;
const quantity = product.variants[0].inventoryQuantity;
Meteor.call("products/cloneVariant", productId, product.variants[
0]._id, product.variants[0]._id);
product = Products.findOne(product._id);
const newChildVariant = product.variants[product.variants.length - 1];
expect(undefined).toEqual(newChildVariant.inventoryQuantity);
newChildVariant["inventoryQuantity"] = 10;
Meteor.call("products/updateVariant", newChildVariant);
spyOn(ReactionCore, "hasPermission").and.returnValue(true);
const totalQuantity = quantity + newChildVariant.inventoryQuantity;
Meteor.call("inventory/register", product);
// uncommenting that leads to test failure. looks like a bug in tests;
// expect(Meteor.call("inventory/register", product)).toEqual(totalQuantity);
const inventory = ReactionCore.Collections.Inventory.find({
productId: productId
}).fetch();
expect(inventory.length).toEqual(totalQuantity);
done();
});

it("should remove deleted variants from inventory", function (done) {
let product = Factory.create("product");
let productId = product._id;
Expand Down
1 change: 1 addition & 0 deletions packages/reaction-schemas/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Package.onUse(function (api) {
api.use("aldeed:[email protected]");
api.use("aldeed:[email protected]");
api.use("matb33:[email protected]");
api.use("mikowals:[email protected]");


// ReactionCore declaration
Expand Down

0 comments on commit d0e33af

Please sign in to comment.