Skip to content

Commit

Permalink
fix(core): Correctly calculate refund amount when modifying order
Browse files Browse the repository at this point in the history
Fixes #890
  • Loading branch information
michaelbromley committed May 25, 2021
1 parent 70ddb87 commit 56d058d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2856,6 +2856,7 @@ export type TestOrderFragmentFragment = Pick<
| 'unitPriceWithTax'
| 'unitPriceChangeSinceAdded'
| 'unitPriceWithTaxChangeSinceAdded'
| 'proratedUnitPriceWithTax'
> & {
productVariant: Pick<ProductVariant, 'id'>;
discounts: Array<
Expand Down
1 change: 1 addition & 0 deletions packages/core/e2e/graphql/shop-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const TEST_ORDER_FRAGMENT = gql`
unitPriceWithTax
unitPriceChangeSinceAdded
unitPriceWithTaxChangeSinceAdded
proratedUnitPriceWithTax
productVariant {
id
}
Expand Down
70 changes: 67 additions & 3 deletions packages/core/e2e/order-modification.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { initialData } from '../../../e2e-common/e2e-initial-data';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
import { manualFulfillmentHandler } from '../src/config/fulfillment/manual-fulfillment-handler';
import { orderFixedDiscount } from '../src/config/promotion/actions/order-fixed-discount-action';
import { defaultPromotionActions } from '../src/config/promotion/index';

import {
failsToSettlePaymentMethod,
Expand Down Expand Up @@ -104,9 +105,6 @@ describe('Order modification', () => {
testFailingPaymentMethod,
],
},
promotionOptions: {
promotionConditions: [orderFixedDiscount],
},
shippingOptions: {
shippingCalculators: [defaultShippingCalculator, testCalculator],
},
Expand Down Expand Up @@ -1443,6 +1441,72 @@ describe('Order modification', () => {
});
});

// https://github.com/vendure-ecommerce/vendure/issues/890
describe('refund handling when promotions are active on order', () => {
it('refunds correct amount when order-level promotion applied', async () => {
await adminClient.query<CreatePromotion.Mutation, CreatePromotion.Variables>(CREATE_PROMOTION, {
input: {
name: '$5 off',
couponCode: '5OFF2',
enabled: true,
conditions: [],
actions: [
{
code: orderFixedDiscount.code,
arguments: [{ name: 'discount', value: '500' }],
},
],
},
});

await shopClient.asUserWithCredentials('[email protected]', 'test');
await shopClient.query(gql(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS), {
productVariantId: 'T_1',
quantity: 2,
} as any);
await shopClient.query<ApplyCouponCode.Mutation, ApplyCouponCode.Variables>(APPLY_COUPON_CODE, {
couponCode: '5OFF2',
});
await proceedToArrangingPayment(shopClient);
const order = await addPaymentToOrder(shopClient, testSuccessfulPaymentMethod);
orderGuard.assertSuccess(order);

const originalTotalWithTax = order.totalWithTax;

const { transitionOrderToState } = await adminClient.query<
AdminTransition.Mutation,
AdminTransition.Variables
>(ADMIN_TRANSITION_TO_STATE, {
id: order.id,
state: 'Modifying',
});
orderGuard.assertSuccess(transitionOrderToState);

expect(transitionOrderToState.state).toBe('Modifying');

const { modifyOrder } = await adminClient.query<ModifyOrder.Mutation, ModifyOrder.Variables>(
MODIFY_ORDER,
{
input: {
dryRun: false,
orderId: order.id,
adjustOrderLines: [{ orderLineId: order.lines[0].id, quantity: 1 }],
refund: {
paymentId: order.payments![0].id,
reason: 'requested',
},
},
},
);
orderGuard.assertSuccess(modifyOrder);

expect(modifyOrder.totalWithTax).toBe(
originalTotalWithTax - order.lines[0].proratedUnitPriceWithTax,
);
expect(modifyOrder.payments![0].refunds![0].total).toBe(order.lines[0].proratedUnitPriceWithTax);
});
});

async function assertOrderIsUnchanged(order: OrderWithLinesFragment) {
const { order: order2 } = await adminClient.query<GetOrder.Query, GetOrder.Variables>(GET_ORDER, {
id: order.id,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/entity/order-line/order-line.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class OrderLine extends VendureEntity implements HasCustomFields {
* is specified, then all adjustments are removed.
*/
clearAdjustments(type?: AdjustmentType) {
this.activeItems.forEach(item => item.clearAdjustments(type));
this.items.forEach(item => item.clearAdjustments(type));
}

private firstActiveItemPropOr<K extends keyof OrderItem>(
Expand Down

0 comments on commit 56d058d

Please sign in to comment.