-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Create OrderCodeStrategy for more control over order codes
Closes #452 BREAKING CHANGE: The `orderOptions.generateOrderCode` config option has been replaced with `orderOptions.orderCodeStrategy`. This change allows order code generation to take advantage of the `InjectableStrategy` interface, i.e. to be able to inject Vendure services and other providers (e.g. the database connection). See the `OrderCodeStrategy` documentation for guidance on how to use the new API.
- Loading branch information
1 parent
11caa23
commit 30dc639
Showing
6 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { RequestContext } from '../../api/common/request-context'; | ||
import { generatePublicId } from '../../common/generate-public-id'; | ||
import { InjectableStrategy } from '../../common/types/injectable-strategy'; | ||
|
||
/** | ||
* @description | ||
* The OrderCodeStrategy determines how Order codes are generated. | ||
* A custom strategy can be defined which e.g. integrates with an | ||
* existing system to generate a code: | ||
* | ||
* @example | ||
* ```TypeScript | ||
* class MyOrderCodeStrategy implements OrderCodeStrategy { | ||
* // Some imaginary service which calls out to an existing external | ||
* // order management system. | ||
* private mgmtService: ExternalOrderManagementService; | ||
* | ||
* init(injector: Injector) { | ||
* this.mgmtService = injector.get(ExternalOrderManagementService); | ||
* } | ||
* | ||
* async generate(ctx: RequestContext) { | ||
* const result = await this.mgmtService.getAvailableOrderParams(); | ||
* return result.code; | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @docsCategory orders | ||
* @docsPage OrderCodeStrategy | ||
*/ | ||
export interface OrderCodeStrategy extends InjectableStrategy { | ||
/** | ||
* @description | ||
* Generates the order code. | ||
*/ | ||
generate(ctx: RequestContext): string | Promise<string>; | ||
} | ||
|
||
/** | ||
* @description | ||
* The default OrderCodeStrategy generates a random string consisting | ||
* of 16 uppercase letters and numbers. | ||
* | ||
* @docsCategory orders | ||
* @docsPage OrderCodeStrategy | ||
*/ | ||
export class DefaultOrderCodeStrategy implements OrderCodeStrategy { | ||
generate(ctx: RequestContext): string { | ||
return generatePublicId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters