diff --git a/packages/core/src/config/default-config.ts b/packages/core/src/config/default-config.ts index 074d4e9814..e5f950ec6c 100644 --- a/packages/core/src/config/default-config.ts +++ b/packages/core/src/config/default-config.ts @@ -2,6 +2,8 @@ import { Transport } from '@nestjs/microservices'; import { LanguageCode } from '@vendure/common/lib/generated-types'; import { DEFAULT_AUTH_TOKEN_HEADER_KEY } from '@vendure/common/lib/shared-constants'; +import { generatePublicId } from '../common/generate-public-id'; + import { DefaultAssetNamingStrategy } from './asset-naming-strategy/default-asset-naming-strategy'; import { NoAssetPreviewStrategy } from './asset-preview-strategy/no-asset-preview-strategy'; import { NoAssetStorageStrategy } from './asset-storage-strategy/no-asset-storage-strategy'; @@ -71,6 +73,7 @@ export const defaultConfig: RuntimeVendureConfig = { mergeStrategy: new MergeOrdersStrategy(), checkoutMergeStrategy: new UseGuestStrategy(), process: {}, + generateOrderCode: () => generatePublicId(), }, paymentOptions: { paymentMethodHandlers: [], diff --git a/packages/core/src/config/vendure-config.ts b/packages/core/src/config/vendure-config.ts index 0d6181648e..bda135b862 100644 --- a/packages/core/src/config/vendure-config.ts +++ b/packages/core/src/config/vendure-config.ts @@ -7,6 +7,7 @@ import { RequestHandler } from 'express'; import { Observable } from 'rxjs'; import { ConnectionOptions } from 'typeorm'; +import { RequestContext } from '../api/common/request-context'; import { Transitions } from '../common/finite-state-machine'; import { Order } from '../entity/order/order.entity'; import { OrderState } from '../service/helpers/order-state-machine/order-state'; @@ -152,6 +153,17 @@ export interface OrderOptions { * @default UseGuestStrategy */ checkoutMergeStrategy?: OrderMergeStrategy; + /** + * @description + * Allows a user-defined function to create Order codes. This can be useful when + * integrating with existing systems. By default, Vendure will generate a 16-character + * alphanumeric string. + * + * Note: when using a custom function for Order codes, bear in mind the database limit + * for string types (e.g. 255 chars for a varchar field in MySQL), and also the need + * for codes to be unique. + */ + generateOrderCode?: (ctx: RequestContext) => string | Promise; } /** diff --git a/packages/core/src/service/services/order.service.ts b/packages/core/src/service/services/order.service.ts index 225573a04f..1f38523132 100644 --- a/packages/core/src/service/services/order.service.ts +++ b/packages/core/src/service/services/order.service.ts @@ -24,7 +24,6 @@ import { OrderItemsLimitError, UserInputError, } from '../../common/error/errors'; -import { generatePublicId } from '../../common/generate-public-id'; import { ListQueryOptions } from '../../common/types/common-types'; import { assertFound, idsAreEqual } from '../../common/utils'; import { ConfigService } from '../../config/config.service'; @@ -200,7 +199,7 @@ export class OrderService { async create(ctx: RequestContext, userId?: ID): Promise { const newOrder = new Order({ - code: generatePublicId(), + code: await this.configService.orderOptions.generateOrderCode(ctx), state: this.orderStateMachine.getInitialState(), lines: [], couponCodes: [],