Skip to content

Commit

Permalink
return new document instead of original
Browse files Browse the repository at this point in the history
  • Loading branch information
kieckhafer committed Jan 2, 2019
1 parent ba513f9 commit 09e2b4c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions imports/plugins/core/inventory/server/no-meteor/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function startup(context) {
// in some instances which causes the order not to cancel
const orderItems = order.shipping.reduce((list, group) => [...list, ...group.items], []);
orderItems.forEach(async (item) => {
const updatedItem = await collections.Products.findOneAndUpdate(
const { value: updatedItem } = await collections.Products.findOneAndUpdate(
{
_id: item.variantId
},
Expand All @@ -33,13 +33,15 @@ export default function startup(context) {
inventoryAvailableToSell: +item.quantity,
inventoryQuantity: +item.quantity
}
}, {
returnNewDocument: true
}
);

// Update parents of supplied item
await collections.Products.updateMany(
{
_id: { $in: updatedItem.value.ancestors }
_id: { $in: updatedItem.ancestors }
},
{
$inc: {
Expand All @@ -65,21 +67,23 @@ export default function startup(context) {
// in some instances which causes the order not to cancel
const orderItems = order.shipping.reduce((list, group) => [...list, ...group.items], []);
orderItems.forEach(async (item) => {
const updatedItem = await collections.Products.findOneAndUpdate(
const { value: updatedItem } = await collections.Products.findOneAndUpdate(
{
_id: item.variantId
},
{
$inc: {
inventoryAvailableToSell: +item.quantity
}
}, {
returnNewDocument: true
}
);

// Update parents of supplied item
await collections.Products.updateMany(
{
_id: { $in: updatedItem.value.ancestors }
_id: { $in: updatedItem.ancestors }
},
{
$inc: {
Expand Down Expand Up @@ -109,21 +113,23 @@ export default function startup(context) {
// we can map over the unique productIds at the end, and update each one once
orderItems.forEach(async (item) => {
// Update supplied item inventory
const updatedItem = await collections.Products.findOneAndUpdate(
const { value: updatedItem } = await collections.Products.findOneAndUpdate(
{
_id: item.variantId
},
{
$inc: {
inventoryAvailableToSell: -item.quantity
}
}, {
returnNewDocument: true
}
);

// Update supplied item inventory
await collections.Products.updateMany(
{
_id: { $in: updatedItem.value.ancestors }
_id: { $in: updatedItem.ancestors }
},
{
$inc: {
Expand Down Expand Up @@ -152,21 +158,23 @@ export default function startup(context) {
// we can map over the unique productIds at the end, and update each one once
orderItems.forEach(async (item) => {
// Update supplied item inventory
const updatedItem = await collections.Products.findOneAndUpdate(
const { value: updatedItem } = await collections.Products.findOneAndUpdate(
{
_id: item.variantId
},
{
$inc: {
inventoryQuantity: -item.quantity
}
}, {
returnNewDocument: true
}
);

// Update supplied item inventory
await collections.Products.updateMany(
{
_id: { $in: updatedItem.value.ancestors }
_id: { $in: updatedItem.ancestors }
},
{
$inc: {
Expand Down

0 comments on commit 09e2b4c

Please sign in to comment.