Skip to content

Commit

Permalink
fix(core): Fix incorrect quantity adjustment (#983)
Browse files Browse the repository at this point in the history
Fixes #931
  • Loading branch information
WilliamMilne authored Aug 18, 2021
1 parent 83d025d commit 2441ce7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/core/e2e/fixtures/e2e-products-full.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name ,slug ,description ,assets ,facets ,optionGroups ,optionValues ,sku ,price ,taxCategory,stockOnHand,trackInventory,variantAssets,variantFacets
Laptop ,laptop ,"Now equipped with seventh-generation Intel Core processors, Laptop is snappier than ever. From daily tasks like launching apps and opening files to more advanced computing, you can power through your day thanks to faster SSDs and Turbo Boost processing up to 3.6GHz." ,derick-david-409858-unsplash.jpg ,category:electronics|category:computers,screen size|RAM,13 inch|8GB ,L2201308 ,1299.00,standard ,100 ,false , ,
, , , , , ,15 inch|8GB ,L2201508 ,1399.00,standard ,100 ,false , ,
, , , , , ,13 inch|16GB ,L2201316 ,2199.00,standard ,100 ,false , ,
, , , , , ,15 inch|8GB ,L2201508 ,1399.00,standard ,100 ,false , ,
, , , , , ,13 inch|16GB ,L2201316 ,2199.00,standard ,100 ,true , ,
, , , , , ,15 inch|16GB ,L2201516 ,2299.00,standard ,100 ,false , ,
Curvy Monitor ,curvy-monitor ,"Discover a truly immersive viewing experience with this monitor curved more deeply than any other. Wrapping around your field of vision the 1,800 R screencreates a wider field of view, enhances depth perception, and minimises peripheral distractions to draw you deeper in to your content." ,alexandru-acea-686569-unsplash.jpg,category:electronics|category:computers,monitor size ,24 inch ,C24F390 ,143.74 ,standard ,100 ,false , ,
, , , , , ,27 inch ,C27F390 ,169.94 ,standard ,100 ,false , ,
Expand Down
53 changes: 48 additions & 5 deletions packages/core/e2e/shop-order.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('Shop orders', () => {
],
},
orderOptions: {
orderItemsLimit: 99,
orderItemsLimit: 199,
},
}),
);
Expand Down Expand Up @@ -473,12 +473,12 @@ describe('Shop orders', () => {
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 100,
quantity: 200,
});

orderResultGuard.assertErrorResult(addItemToOrder);
expect(addItemToOrder.message).toBe(
'Cannot add items. An order may consist of a maximum of 99 items',
'Cannot add items. An order may consist of a maximum of 199 items',
);
expect(addItemToOrder.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
});
Expand Down Expand Up @@ -520,17 +520,60 @@ describe('Shop orders', () => {
expect(adjustOrderLine!.lines.map(i => i.productVariant.id)).toEqual(['T_1']);
});

it('adjustOrderLine with quantity > stockOnHand only allows user to have stock on hand', async () => {
const { addItemToOrder } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_3',
quantity: 111,
});
orderResultGuard.assertErrorResult(addItemToOrder);
// Insufficient stock error should return because there are only 100 available
expect(addItemToOrder.errorCode).toBe('INSUFFICIENT_STOCK_ERROR');

// But it should still add the item to the order
expect(addItemToOrder!.order.lines[1].quantity).toBe(100);

const { adjustOrderLine } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: 'T_8',
quantity: 101,
});
orderResultGuard.assertErrorResult(adjustOrderLine);
expect(adjustOrderLine.errorCode).toBe('INSUFFICIENT_STOCK_ERROR');
expect(adjustOrderLine.message).toBe(
'Only 100 items were added to the order due to insufficient stock',
);

const order = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(order.activeOrder?.lines[1].quantity).toBe(100);

const { adjustOrderLine: adjustLine2 } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: 'T_8',
quantity: 0,
});
orderResultGuard.assertSuccess(adjustLine2);
expect(adjustLine2!.lines.length).toBe(1);
expect(adjustLine2!.lines.map(i => i.productVariant.id)).toEqual(['T_1']);
});

it('adjustOrderLine errors when going beyond orderItemsLimit', async () => {
const { adjustOrderLine } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: firstOrderLineId,
quantity: 100,
quantity: 200,
});
orderResultGuard.assertErrorResult(adjustOrderLine);
expect(adjustOrderLine.message).toBe(
'Cannot add items. An order may consist of a maximum of 99 items',
'Cannot add items. An order may consist of a maximum of 199 items',
);
expect(adjustOrderLine.errorCode).toBe(ErrorCode.ORDER_LIMIT_ERROR);
});
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,12 @@ export class OrderService {
productVariantId,
customFields,
);
await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, correctedQuantity, order);
if (correctedQuantity < quantity) {
const newQuantity = (existingOrderLine ? existingOrderLine?.quantity : 0) + correctedQuantity;
await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, newQuantity, order);
} else {
await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, correctedQuantity, order);
}
const quantityWasAdjustedDown = correctedQuantity < quantity;
const updatedOrder = await this.applyPriceAdjustments(ctx, order, orderLine);
if (quantityWasAdjustedDown) {
Expand Down Expand Up @@ -457,7 +462,7 @@ export class OrderService {
order.lines = order.lines.filter(l => !idsAreEqual(l.id, orderLine.id));
await this.connection.getRepository(ctx, OrderLine).remove(orderLine);
} else {
await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, quantity, order);
await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, correctedQuantity, order);
}
const quantityWasAdjustedDown = correctedQuantity < quantity;
const updatedOrder = await this.applyPriceAdjustments(ctx, order, orderLine);
Expand Down

0 comments on commit 2441ce7

Please sign in to comment.