Skip to content

Commit

Permalink
feat(core): Allow a custom function for generating order codes
Browse files Browse the repository at this point in the history
Closes #252
  • Loading branch information
michaelbromley committed Feb 3, 2020
1 parent 2b3fc72 commit 7d36de9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -71,6 +73,7 @@ export const defaultConfig: RuntimeVendureConfig = {
mergeStrategy: new MergeOrdersStrategy(),
checkoutMergeStrategy: new UseGuestStrategy(),
process: {},
generateOrderCode: () => generatePublicId(),
},
paymentOptions: {
paymentMethodHandlers: [],
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/config/vendure-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string>;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -200,7 +199,7 @@ export class OrderService {

async create(ctx: RequestContext, userId?: ID): Promise<Order> {
const newOrder = new Order({
code: generatePublicId(),
code: await this.configService.orderOptions.generateOrderCode(ctx),
state: this.orderStateMachine.getInitialState(),
lines: [],
couponCodes: [],
Expand Down

0 comments on commit 7d36de9

Please sign in to comment.