-
-
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): Implement configurable PriceCalculationStrategy
Relates to #237
- Loading branch information
1 parent
3583089
commit 3e2cc2b
Showing
9 changed files
with
172 additions
and
34 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
14 changes: 14 additions & 0 deletions
14
packages/core/src/config/order/default-price-calculation-strategy.ts
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,14 @@ | ||
import { ProductVariant } from '../../entity/product-variant/product-variant.entity'; | ||
|
||
import { CalculatedPrice, PriceCalculationStrategy } from './price-calculation-strategy'; | ||
|
||
/** | ||
* @description | ||
* The default {@link PriceCalculationStrategy}, which simply passes through the price of | ||
* the ProductVariant without performing any calculations | ||
*/ | ||
export class DefaultPriceCalculationStrategy implements PriceCalculationStrategy { | ||
calculateUnitPrice(productVariant: ProductVariant): CalculatedPrice | Promise<CalculatedPrice> { | ||
return productVariant; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/core/src/config/order/price-calculation-strategy.ts
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,60 @@ | ||
import { InjectableStrategy } from '../../common/types/injectable-strategy'; | ||
import { ProductVariant } from '../../entity/product-variant/product-variant.entity'; | ||
|
||
/** | ||
* @description | ||
* The result of the price calculation from the {@link PriceCalculationStrategy}. | ||
* | ||
* @docsCateogory Orders | ||
*/ | ||
export type CalculatedPrice = { | ||
price: number; | ||
priceIncludesTax: boolean; | ||
}; | ||
|
||
/** | ||
* @description | ||
* The PriceCalculationStrategy defines the price of an OrderItem when a ProductVariant gets added | ||
* to an order via the `addItemToOrder` mutation. By default the {@link DefaultPriceCalculationStrategy} | ||
* is used. | ||
* | ||
* ### PriceCalculationStrategy vs Promotions | ||
* Both the PriceCalculationStrategy and Promotions can be used to alter the price paid for a product. | ||
* | ||
* Use PriceCalculationStrategy if: | ||
* | ||
* * The price is not dependent on quantity or on the other contents of the Order. | ||
* * The price calculation is based on the properties of the ProductVariant and any CustomFields | ||
* specified on the OrderLine, for example via a product configurator. | ||
* * The logic is a permanent part of your business requirements. | ||
* | ||
* Use Promotions if: | ||
* | ||
* * You want to implement "discounts" and "special offers" | ||
* * The calculation is not a permanent part of your business requirements. | ||
* * The price depends on dynamic aspects such as quantities and which other | ||
* ProductVariants are in the Order. | ||
* * The configuration of the logic needs to be manipulated via the Admin UI. | ||
* | ||
* ### Example use-cases | ||
* | ||
* A custom PriceCalculationStrategy can be used to implement things like: | ||
* | ||
* * A gift-wrapping service, where a boolean custom field is defined on the OrderLine. If `true`, | ||
* a gift-wrapping surcharge would be added to the price. | ||
* * A product-configurator where e.g. various finishes, colors, and materials can be selected and stored | ||
* as OrderLine custom fields. | ||
* | ||
* @docsCateogory Orders | ||
*/ | ||
export interface PriceCalculationStrategy extends InjectableStrategy { | ||
/** | ||
* @description | ||
* Receives the ProductVariant to be added to the Order as well as any OrderLine custom fields and returns | ||
* the price for a single unit. | ||
*/ | ||
calculateUnitPrice( | ||
productVariant: ProductVariant, | ||
orderLineCustomFields: { [key: string]: any }, | ||
): CalculatedPrice | Promise<CalculatedPrice>; | ||
} |
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
Oops, something went wrong.