Skip to content

Commit

Permalink
feat(core): Pass RequestContext to PriceCalculationStrategy
Browse files Browse the repository at this point in the history
Closes #487

BREAKING CHANGE: The `PriceCalculationStrategy.calculateUnitPrice()` function
signature has changed: the first argument is now the RequestContext of the current request.
  • Loading branch information
michaelbromley committed Oct 27, 2020
1 parent 9c2257d commit 8a58325
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { CalculatedPrice, PriceCalculationStrategy, ProductVariant } from '@vendure/core';
import { CalculatedPrice, PriceCalculationStrategy, ProductVariant, RequestContext } from '@vendure/core';

/**
* Adds $5 for items with gift wrapping.
*/
export class TestPriceCalculationStrategy implements PriceCalculationStrategy {
calculateUnitPrice(
ctx: RequestContext,
productVariant: ProductVariant,
orderLineCustomFields: { [p: string]: any },
): CalculatedPrice | Promise<CalculatedPrice> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '../../api/common/request-context';
import { ProductVariant } from '../../entity/product-variant/product-variant.entity';

import { CalculatedPrice, PriceCalculationStrategy } from './price-calculation-strategy';
Expand All @@ -10,7 +11,10 @@ import { CalculatedPrice, PriceCalculationStrategy } from './price-calculation-s
* @docsCategory orders
*/
export class DefaultPriceCalculationStrategy implements PriceCalculationStrategy {
calculateUnitPrice(productVariant: ProductVariant): CalculatedPrice | Promise<CalculatedPrice> {
calculateUnitPrice(
ctx: RequestContext,
productVariant: ProductVariant,
): CalculatedPrice | Promise<CalculatedPrice> {
return productVariant;
}
}
2 changes: 2 additions & 0 deletions packages/core/src/config/order/price-calculation-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RequestContext } from '../../api/common/request-context';
import { InjectableStrategy } from '../../common/types/injectable-strategy';
import { ProductVariant } from '../../entity/product-variant/product-variant.entity';

Expand Down Expand Up @@ -54,6 +55,7 @@ export interface PriceCalculationStrategy extends InjectableStrategy {
* the price for a single unit.
*/
calculateUnitPrice(
ctx: RequestContext,
productVariant: ProductVariant,
orderLineCustomFields: { [key: string]: any },
): CalculatedPrice | Promise<CalculatedPrice>;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ export class OrderService {
}
const productVariant = orderLine.productVariant;
const calculatedPrice = await priceCalculationStrategy.calculateUnitPrice(
ctx,
productVariant,
orderLine.customFields || {},
);
Expand Down

0 comments on commit 8a58325

Please sign in to comment.