Skip to content

Commit

Permalink
feat(core): Expose order state machine config via serverConfig type
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Jul 13, 2020
1 parent 1d2ba31 commit 0a77438
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,12 @@ export type OrderListOptions = {
filter?: Maybe<OrderFilterParameter>;
};

export type OrderProcessState = {
__typename?: 'OrderProcessState';
name: Scalars['String'];
to: Array<Scalars['String']>;
};

export type OrderSortParameter = {
id?: Maybe<SortOrder>;
createdAt?: Maybe<SortOrder>;
Expand Down Expand Up @@ -3210,6 +3216,7 @@ export type SearchResultSortParameter = {

export type ServerConfig = {
__typename?: 'ServerConfig';
orderProcess: Array<OrderProcessState>;
customFieldConfig: CustomFields;
};

Expand Down
7 changes: 7 additions & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,12 @@ export type OrderListOptions = {
filter?: Maybe<OrderFilterParameter>;
};

export type OrderProcessState = {
__typename?: 'OrderProcessState';
name: Scalars['String'];
to: Array<Scalars['String']>;
};

export type OrderSortParameter = {
id?: Maybe<SortOrder>;
createdAt?: Maybe<SortOrder>;
Expand Down Expand Up @@ -2133,6 +2139,7 @@ export type SearchResultSortParameter = {

export type ServerConfig = {
__typename?: 'ServerConfig';
orderProcess: Array<OrderProcessState>;
customFieldConfig: CustomFields;
};

Expand Down
7 changes: 7 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,12 @@ export type OrderListOptions = {
filter?: Maybe<OrderFilterParameter>;
};

export type OrderProcessState = {
__typename?: 'OrderProcessState';
name: Scalars['String'];
to: Array<Scalars['String']>;
};

export type OrderSortParameter = {
id?: Maybe<SortOrder>;
createdAt?: Maybe<SortOrder>;
Expand Down Expand Up @@ -3335,6 +3341,7 @@ export type SearchResultSortParameter = {

export type ServerConfig = {
__typename?: 'ServerConfig';
orderProcess: Array<OrderProcessState>;
customFieldConfig: CustomFields;
};

Expand Down
7 changes: 7 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,12 @@ export type OrderListOptions = {
filter?: Maybe<OrderFilterParameter>;
};

export type OrderProcessState = {
__typename?: 'OrderProcessState';
name: Scalars['String'];
to: Array<Scalars['String']>;
};

export type OrderSortParameter = {
id?: Maybe<SortOrder>;
createdAt?: Maybe<SortOrder>;
Expand Down Expand Up @@ -3210,6 +3216,7 @@ export type SearchResultSortParameter = {

export type ServerConfig = {
__typename?: 'ServerConfig';
orderProcess: Array<OrderProcessState>;
customFieldConfig: CustomFields;
};

Expand Down
7 changes: 7 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,12 @@ export type OrderListOptions = {
filter?: Maybe<OrderFilterParameter>;
};

export type OrderProcessState = {
__typename?: 'OrderProcessState';
name: Scalars['String'];
to: Array<Scalars['String']>;
};

export type OrderSortParameter = {
id?: Maybe<SortOrder>;
createdAt?: Maybe<SortOrder>;
Expand Down Expand Up @@ -2133,6 +2139,7 @@ export type SearchResultSortParameter = {

export type ServerConfig = {
__typename?: 'ServerConfig';
orderProcess: Array<OrderProcessState>;
customFieldConfig: CustomFields;
};

Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/api/resolvers/admin/global-settings.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Args, Mutation, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { MutationUpdateGlobalSettingsArgs, Permission } from '@vendure/common/lib/generated-types';
import {
MutationUpdateGlobalSettingsArgs,
OrderProcessState,
Permission,
} from '@vendure/common/lib/generated-types';

import { UserInputError } from '../../../common/error/errors';
import { ConfigService } from '../../../config/config.service';
import { CustomFields } from '../../../config/custom-field/custom-field-types';
import { ChannelService } from '../../../service/services/channel.service';
import { GlobalSettingsService } from '../../../service/services/global-settings.service';
import { OrderService } from '../../../service/services/order.service';
import { Allow } from '../../decorators/allow.decorator';

@Resolver('GlobalSettings')
Expand All @@ -14,6 +19,7 @@ export class GlobalSettingsResolver {
private configService: ConfigService,
private globalSettingsService: GlobalSettingsService,
private channelService: ChannelService,
private orderService: OrderService,
) {}

@Query()
Expand All @@ -26,7 +32,10 @@ export class GlobalSettingsResolver {
* Exposes a subset of the VendureConfig which may be of use to clients.
*/
@ResolveField()
serverConfig() {
serverConfig(): {
customFieldConfig: CustomFields;
orderProcess: OrderProcessState[];
} {
// Do not expose custom fields marked as "internal".
const exposedCustomFieldConfig: CustomFields = {};
for (const [entityType, customFields] of Object.entries(this.configService.customFields)) {
Expand All @@ -36,6 +45,7 @@ export class GlobalSettingsResolver {
}
return {
customFieldConfig: exposedCustomFieldConfig,
orderProcess: this.orderService.getOrderProcessStates(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ type GlobalSettings {
serverConfig: ServerConfig!
}

type OrderProcessState {
name: String!
to: [String!]!
}

# Programatically extended by the addGraphQLCustomFields function
type ServerConfig
type ServerConfig {
orderProcess: [OrderProcessState!]!
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectConnection } from '@nestjs/typeorm';
import { HistoryEntryType } from '@vendure/common/lib/generated-types';
import { from } from 'rxjs';
import { Connection } from 'typeorm';

import { RequestContext } from '../../../api/common/request-context';
Expand All @@ -14,7 +13,6 @@ import {
import { mergeTransitionDefinitions } from '../../../common/finite-state-machine/merge-transition-definitions';
import { validateTransitionDefinition } from '../../../common/finite-state-machine/validate-transition-definition';
import { ConfigService } from '../../../config/config.service';
import { OrderItem } from '../../../entity/order-item/order-item.entity';
import { Order } from '../../../entity/order/order.entity';
import { HistoryService } from '../../services/history.service';
import { PromotionService } from '../../services/promotion.service';
Expand All @@ -31,7 +29,7 @@ import { OrderState, orderStateTransitions, OrderTransitionData } from './order-

@Injectable()
export class OrderStateMachine {
private readonly config: StateMachineConfig<OrderState, OrderTransitionData>;
readonly config: StateMachineConfig<OrderState, OrderTransitionData>;
private readonly initialState: OrderState = 'AddingItems';

constructor(
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
FulfillOrderInput,
HistoryEntryType,
OrderLineInput,
OrderProcessState,
RefundOrderInput,
SettleRefundInput,
ShippingMethodQuote,
Expand Down Expand Up @@ -90,6 +91,13 @@ export class OrderService {
private eventBus: EventBus,
) {}

getOrderProcessStates(): OrderProcessState[] {
return Object.entries(this.orderStateMachine.config.transitions).map(([name, { to }]) => ({
name,
to,
}));
}

findAll(ctx: RequestContext, options?: ListQueryOptions<Order>): Promise<PaginatedList<Order>> {
return this.listQueryBuilder
.build(Order, options, { relations: ['lines', 'customer', 'lines.productVariant'] })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,12 @@ export type OrderListOptions = {
filter?: Maybe<OrderFilterParameter>;
};

export type OrderProcessState = {
__typename?: 'OrderProcessState';
name: Scalars['String'];
to: Array<Scalars['String']>;
};

export type OrderSortParameter = {
id?: Maybe<SortOrder>;
createdAt?: Maybe<SortOrder>;
Expand Down Expand Up @@ -3210,6 +3216,7 @@ export type SearchResultSortParameter = {

export type ServerConfig = {
__typename?: 'ServerConfig';
orderProcess: Array<OrderProcessState>;
customFieldConfig: CustomFields;
};

Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 0a77438

Please sign in to comment.