Skip to content

Commit

Permalink
fix(core): Fix FK error with adjustOrderLine when zero saleable stock
Browse files Browse the repository at this point in the history
Fixes #1273
  • Loading branch information
michaelbromley committed Dec 14, 2021
1 parent d810450 commit 28aeddb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
64 changes: 64 additions & 0 deletions packages/core/e2e/stock-control.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import {
import {
AddItemToOrder,
AddPaymentToOrder,
AdjustItemQuantity,
ErrorCode,
GetActiveOrder,
GetProductStockLevel,
GetShippingMethods,
PaymentInput,
Expand All @@ -54,6 +56,8 @@ import {
import {
ADD_ITEM_TO_ORDER,
ADD_PAYMENT,
ADJUST_ITEM_QUANTITY,
GET_ACTIVE_ORDER,
GET_ELIGIBLE_SHIPPING_METHODS,
GET_PRODUCT_WITH_STOCK_LEVEL,
SET_SHIPPING_ADDRESS,
Expand Down Expand Up @@ -941,6 +945,7 @@ describe('Stock control', () => {
describe('edge cases', () => {
const variant5Id = 'T_5';
const variant6Id = 'T_6';
const variant7Id = 'T_7';

beforeAll(async () => {
// First place an order which creates a backorder (excess of allocated units)
Expand All @@ -962,6 +967,13 @@ describe('Stock control', () => {
trackInventory: GlobalFlag.TRUE,
useGlobalOutOfStockThreshold: false,
},
{
id: variant7Id,
stockOnHand: 3,
outOfStockThreshold: 0,
trackInventory: GlobalFlag.TRUE,
useGlobalOutOfStockThreshold: false,
},
],
},
);
Expand Down Expand Up @@ -1055,6 +1067,58 @@ describe('Stock control', () => {
expect((add2 as any).order.lines[0].productVariant.id).toBe(variant6Id);
expect((add2 as any).order.lines[0].quantity).toBe(3);
});

// https://github.com/vendure-ecommerce/vendure/issues/1273
it('adjustOrderLine when saleable stock changes to zero', async () => {
await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
UPDATE_PRODUCT_VARIANTS,
{
input: [
{
id: variant7Id,
stockOnHand: 10,
},
],
},
);

await shopClient.asAnonymousUser();
const { addItemToOrder: add1 } = await shopClient.query<
AddItemToOrder.Mutation,
AddItemToOrder.Variables
>(ADD_ITEM_TO_ORDER, {
productVariantId: variant7Id,
quantity: 1,
});
orderGuard.assertSuccess(add1);
expect(add1.lines.length).toBe(1);

await adminClient.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
UPDATE_PRODUCT_VARIANTS,
{
input: [
{
id: variant7Id,
stockOnHand: 0,
},
],
},
);

const { adjustOrderLine: add2 } = await shopClient.query<
AdjustItemQuantity.Mutation,
AdjustItemQuantity.Variables
>(ADJUST_ITEM_QUANTITY, {
orderLineId: add1.lines[0].id,
quantity: 2,
});
orderGuard.assertErrorResult(add2);

expect(add2.errorCode).toBe(ErrorCode.INSUFFICIENT_STOCK_ERROR);

const { activeOrder } = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
expect(activeOrder!.lines.length).toBe(0);
});
});
});

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,16 @@ export class OrderService {
orderLine.productVariant,
quantity,
);
let updatedOrderLines = [orderLine];
if (correctedQuantity === 0) {
order.lines = order.lines.filter(l => !idsAreEqual(l.id, orderLine.id));
await this.connection.getRepository(ctx, OrderLine).remove(orderLine);
updatedOrderLines = [];
} else {
await this.orderModifier.updateOrderLineQuantity(ctx, orderLine, correctedQuantity, order);
}
const quantityWasAdjustedDown = correctedQuantity < quantity;
const updatedOrder = await this.applyPriceAdjustments(ctx, order, [orderLine]);
const updatedOrder = await this.applyPriceAdjustments(ctx, order, updatedOrderLines);
if (quantityWasAdjustedDown) {
return new InsufficientStockError(correctedQuantity, updatedOrder);
} else {
Expand Down

0 comments on commit 28aeddb

Please sign in to comment.