Skip to content

Commit

Permalink
feat(core): Extract hard-coded order state & process
Browse files Browse the repository at this point in the history
This commit makes it possible to completely configure the order process by extracting all
transition validation and allowing the developer to replace with custom logic.
  • Loading branch information
michaelbromley committed Jan 17, 2023
1 parent 3d9f7e8 commit cff3b91
Show file tree
Hide file tree
Showing 14 changed files with 12,449 additions and 7,544 deletions.
13,211 changes: 8,425 additions & 4,786 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts

Large diffs are not rendered by default.

5,857 changes: 3,456 additions & 2,401 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts

Large diffs are not rendered by default.

129 changes: 66 additions & 63 deletions packages/core/e2e/order-process.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
/* tslint:disable:no-non-null-assertion */
import { CustomOrderProcess, mergeConfig, OrderState, TransactionalConnection } from '@vendure/core';
import {
CustomOrderProcess,
defaultOrderProcess,
mergeConfig,
OrderState,
TransactionalConnection,
} from '@vendure/core';
import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
import path from 'path';

import { initialData } from '../../../e2e-common/e2e-initial-data';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';

import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
import { AdminTransition, GetOrder, OrderFragment } from './graphql/generated-e2e-admin-types';
import * as Codegen from './graphql/generated-e2e-admin-types';
import { OrderFragment } from './graphql/generated-e2e-admin-types';
import {
AddItemToOrder,
AddPaymentToOrder,
AddPaymentToOrderMutation,
AddPaymentToOrderMutationVariables,
ErrorCode,
GetNextOrderStates,
SetCustomerForOrder,
SetShippingAddress,
SetShippingMethod,
TestOrderFragmentFragment,
TransitionToState,
TransitionToStateMutation,
TransitionToStateMutationVariables,
} from './graphql/generated-e2e-shop-types';
import * as CodegenShop from './graphql/generated-e2e-shop-types';
import { ADMIN_TRANSITION_TO_STATE, GET_ORDER } from './graphql/shared-definitions';
import {
ADD_ITEM_TO_ORDER,
Expand Down Expand Up @@ -96,7 +99,7 @@ describe('Order process', () => {

const { server, adminClient, shopClient } = createTestEnvironment(
mergeConfig(testConfig(), {
orderOptions: { process: [customOrderProcess as any, customOrderProcess2 as any] },
orderOptions: { process: [defaultOrderProcess, customOrderProcess, customOrderProcess2] as any },
paymentOptions: {
paymentMethodHandlers: [testSuccessfulPaymentMethod],
},
Expand Down Expand Up @@ -130,13 +133,13 @@ describe('Order process', () => {
transitionEndSpy.mockClear();
await shopClient.asAnonymousUser();

await shopClient.query<Codegen.AddItemToOrderMutation, Codegen.AddItemToOrderMutationVariables>(
ADD_ITEM_TO_ORDER,
{
productVariantId: 'T_1',
quantity: 1,
},
);
await shopClient.query<
CodegenShop.AddItemToOrderMutation,
CodegenShop.AddItemToOrderMutationVariables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 1,
});

expect(transitionStartSpy).toHaveBeenCalledTimes(1);
expect(transitionEndSpy).toHaveBeenCalledTimes(1);
Expand All @@ -152,15 +155,15 @@ describe('Order process', () => {
});

it('replaced transition target', async () => {
await shopClient.query<Codegen.AddItemToOrderMutation, Codegen.AddItemToOrderMutationVariables>(
ADD_ITEM_TO_ORDER,
{
productVariantId: 'T_1',
quantity: 1,
},
);
await shopClient.query<
CodegenShop.AddItemToOrderMutation,
CodegenShop.AddItemToOrderMutationVariables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 1,
});

const { nextOrderStates } = await shopClient.query<Codegen.GetNextOrderStatesQuery>(
const { nextOrderStates } = await shopClient.query<CodegenShop.GetNextOrderStatesQuery>(
GET_NEXT_STATES,
);

Expand All @@ -172,8 +175,8 @@ describe('Order process', () => {
transitionEndSpy.mockClear();

const { transitionOrderToState } = await shopClient.query<
Codegen.TransitionToStateMutation,
Codegen.TransitionToStateMutationVariables
CodegenShop.TransitionToStateMutation,
CodegenShop.TransitionToStateMutationVariables
>(TRANSITION_TO_STATE, {
state: 'ValidatingCustomer',
});
Expand All @@ -193,8 +196,8 @@ describe('Order process', () => {
transitionErrorSpy.mockClear();

await shopClient.query<
Codegen.SetCustomerForOrderMutation,
Codegen.SetCustomerForOrderMutationVariables
CodegenShop.SetCustomerForOrderMutation,
CodegenShop.SetCustomerForOrderMutationVariables
>(SET_CUSTOMER, {
input: {
firstName: 'Joe',
Expand All @@ -204,8 +207,8 @@ describe('Order process', () => {
});

const { transitionOrderToState } = await shopClient.query<
Codegen.TransitionToStateMutation,
Codegen.TransitionToStateMutationVariables
CodegenShop.TransitionToStateMutation,
CodegenShop.TransitionToStateMutationVariables
>(TRANSITION_TO_STATE, {
state: 'ValidatingCustomer',
});
Expand Down Expand Up @@ -233,8 +236,8 @@ describe('Order process', () => {
transitionEndSpy.mockClear();

await shopClient.query<
Codegen.SetCustomerForOrderMutation,
Codegen.SetCustomerForOrderMutationVariables
CodegenShop.SetCustomerForOrderMutation,
CodegenShop.SetCustomerForOrderMutationVariables
>(SET_CUSTOMER, {
input: {
firstName: 'Joe',
Expand All @@ -244,8 +247,8 @@ describe('Order process', () => {
});

const { transitionOrderToState } = await shopClient.query<
Codegen.TransitionToStateMutation,
Codegen.TransitionToStateMutationVariables
CodegenShop.TransitionToStateMutation,
CodegenShop.TransitionToStateMutationVariables
>(TRANSITION_TO_STATE, {
state: 'ValidatingCustomer',
});
Expand All @@ -260,15 +263,15 @@ describe('Order process', () => {
transitionEndSpy.mockClear();
transitionEndSpy2.mockClear();

const { nextOrderStates } = await shopClient.query<Codegen.GetNextOrderStatesQuery>(
const { nextOrderStates } = await shopClient.query<CodegenShop.GetNextOrderStatesQuery>(
GET_NEXT_STATES,
);

expect(nextOrderStates).toEqual(['ArrangingPayment', 'AddingItems', 'Cancelled']);

await shopClient.query<
Codegen.TransitionToStateMutation,
Codegen.TransitionToStateMutationVariables
CodegenShop.TransitionToStateMutation,
CodegenShop.TransitionToStateMutationVariables
>(TRANSITION_TO_STATE, {
state: 'AddingItems',
});
Expand All @@ -282,10 +285,10 @@ describe('Order process', () => {

// https://github.com/vendure-ecommerce/vendure/issues/963
it('allows addPaymentToOrder from a custom state', async () => {
await shopClient.query<SetShippingMethod.Mutation, SetShippingMethod.Variables>(
SET_SHIPPING_METHOD,
{ id: 'T_1' },
);
await shopClient.query<
CodegenShop.SetShippingMethodMutation,
CodegenShop.SetShippingMethodMutationVariables
>(SET_SHIPPING_METHOD, { id: 'T_1' });
const result0 = await shopClient.query<
TransitionToStateMutation,
TransitionToStateMutationVariables
Expand All @@ -309,8 +312,8 @@ describe('Order process', () => {
orderErrorGuard.assertSuccess(result2.transitionOrderToState);
expect(result2.transitionOrderToState.state).toBe('PaymentProcessing');
const { addPaymentToOrder } = await shopClient.query<
AddPaymentToOrder.Mutation,
AddPaymentToOrder.Variables
AddPaymentToOrderMutation,
AddPaymentToOrderMutationVariables
>(ADD_PAYMENT, {
input: {
method: testSuccessfulPaymentMethod.code,
Expand All @@ -327,16 +330,16 @@ describe('Order process', () => {

beforeAll(async () => {
await shopClient.asAnonymousUser();
await shopClient.query<Codegen.AddItemToOrderMutation, Codegen.AddItemToOrderMutationVariables>(
ADD_ITEM_TO_ORDER,
{
productVariantId: 'T_1',
quantity: 1,
},
);
await shopClient.query<
Codegen.SetCustomerForOrderMutation,
Codegen.SetCustomerForOrderMutationVariables
CodegenShop.AddItemToOrderMutation,
CodegenShop.AddItemToOrderMutationVariables
>(ADD_ITEM_TO_ORDER, {
productVariantId: 'T_1',
quantity: 1,
});
await shopClient.query<
CodegenShop.SetCustomerForOrderMutation,
CodegenShop.SetCustomerForOrderMutationVariables
>(SET_CUSTOMER, {
input: {
firstName: 'Su',
Expand All @@ -345,8 +348,8 @@ describe('Order process', () => {
},
});
await shopClient.query<
Codegen.SetShippingAddressMutation,
Codegen.SetShippingAddressMutationVariables
CodegenShop.SetShippingAddressMutation,
CodegenShop.SetShippingAddressMutationVariables
>(SET_SHIPPING_ADDRESS, {
input: {
fullName: 'name',
Expand All @@ -358,18 +361,18 @@ describe('Order process', () => {
},
});
await shopClient.query<
Codegen.SetShippingMethodMutation,
Codegen.SetShippingMethodMutationVariables
CodegenShop.SetShippingMethodMutation,
CodegenShop.SetShippingMethodMutationVariables
>(SET_SHIPPING_METHOD, { id: 'T_1' });
await shopClient.query<
Codegen.TransitionToStateMutation,
Codegen.TransitionToStateMutationVariables
CodegenShop.TransitionToStateMutation,
CodegenShop.TransitionToStateMutationVariables
>(TRANSITION_TO_STATE, {
state: 'ValidatingCustomer',
});
const { transitionOrderToState } = await shopClient.query<
Codegen.TransitionToStateMutation,
Codegen.TransitionToStateMutationVariables
CodegenShop.TransitionToStateMutation,
CodegenShop.TransitionToStateMutationVariables
>(TRANSITION_TO_STATE, {
state: 'ArrangingPayment',
});
Expand Down Expand Up @@ -434,8 +437,8 @@ describe('Order process', () => {

it('cannot manually transition to Cancelled', async () => {
const { addPaymentToOrder } = await shopClient.query<
Codegen.AddPaymentToOrderMutation,
Codegen.AddPaymentToOrderMutationVariables
CodegenShop.AddPaymentToOrderMutation,
CodegenShop.AddPaymentToOrderMutationVariables
>(ADD_PAYMENT, {
input: {
method: testSuccessfulPaymentMethod.code,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/e2e/payment-process.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CustomOrderProcess,
CustomPaymentProcess,
DefaultLogger,
defaultOrderProcess,
LanguageCode,
mergeConfig,
Order,
Expand Down Expand Up @@ -124,7 +125,7 @@ describe('Payment process', () => {
mergeConfig(testConfig(), {
// logger: new DefaultLogger(),
orderOptions: {
process: [customOrderProcess as any],
process: [defaultOrderProcess, customOrderProcess] as any,
orderPlacedStrategy: new TestOrderPlacedStrategy(),
},
paymentOptions: {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { DefaultActiveOrderStrategy } from './order/default-active-order-strateg
import { DefaultChangedPriceHandlingStrategy } from './order/default-changed-price-handling-strategy';
import { DefaultOrderItemPriceCalculationStrategy } from './order/default-order-item-price-calculation-strategy';
import { DefaultOrderPlacedStrategy } from './order/default-order-placed-strategy';
import { defaultOrderProcess } from './order/default-order-process';
import { DefaultOrderSellerStrategy } from './order/default-order-seller-strategy';
import { DefaultStockAllocationStrategy } from './order/default-stock-allocation-strategy';
import { MergeOrdersStrategy } from './order/merge-orders-strategy';
Expand Down Expand Up @@ -136,7 +137,7 @@ export const defaultConfig: RuntimeVendureConfig = {
orderItemPriceCalculationStrategy: new DefaultOrderItemPriceCalculationStrategy(),
mergeStrategy: new MergeOrdersStrategy(),
checkoutMergeStrategy: new UseGuestStrategy(),
process: [],
process: [defaultOrderProcess],
stockAllocationStrategy: new DefaultStockAllocationStrategy(),
orderCodeStrategy: new DefaultOrderCodeStrategy(),
orderByCodeAccessStrategy: new DefaultOrderByCodeAccessStrategy('2h'),
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ export * from './merge-config';
export * from './order/active-order-strategy';
export * from './order/default-active-order-strategy';
export * from './order/changed-price-handling-strategy';
export * from './order/custom-order-process';
export * from './order/order-process';
export * from './order/default-changed-price-handling-strategy';
export * from './order/default-order-placed-strategy';
export * from './order/default-order-process';
export * from './order/default-order-seller-strategy';
export * from './order/default-stock-allocation-strategy';
export * from './order/merge-orders-strategy';
Expand Down
Loading

0 comments on commit cff3b91

Please sign in to comment.