From 8a5832592dc2491accbd572f22629912e081371c Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 27 Oct 2020 13:26:38 +0100 Subject: [PATCH] feat(core): Pass RequestContext to PriceCalculationStrategy Closes #487 BREAKING CHANGE: The `PriceCalculationStrategy.calculateUnitPrice()` function signature has changed: the first argument is now the RequestContext of the current request. --- .../core/e2e/fixtures/test-price-calculation-strategy.ts | 3 ++- .../src/config/order/default-price-calculation-strategy.ts | 6 +++++- .../core/src/config/order/price-calculation-strategy.ts | 2 ++ packages/core/src/service/services/order.service.ts | 1 + 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/core/e2e/fixtures/test-price-calculation-strategy.ts b/packages/core/e2e/fixtures/test-price-calculation-strategy.ts index 9b7b20a7e4..cd29c1a616 100644 --- a/packages/core/e2e/fixtures/test-price-calculation-strategy.ts +++ b/packages/core/e2e/fixtures/test-price-calculation-strategy.ts @@ -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 { diff --git a/packages/core/src/config/order/default-price-calculation-strategy.ts b/packages/core/src/config/order/default-price-calculation-strategy.ts index 90cde6a791..9aa104b515 100644 --- a/packages/core/src/config/order/default-price-calculation-strategy.ts +++ b/packages/core/src/config/order/default-price-calculation-strategy.ts @@ -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'; @@ -10,7 +11,10 @@ import { CalculatedPrice, PriceCalculationStrategy } from './price-calculation-s * @docsCategory orders */ export class DefaultPriceCalculationStrategy implements PriceCalculationStrategy { - calculateUnitPrice(productVariant: ProductVariant): CalculatedPrice | Promise { + calculateUnitPrice( + ctx: RequestContext, + productVariant: ProductVariant, + ): CalculatedPrice | Promise { return productVariant; } } diff --git a/packages/core/src/config/order/price-calculation-strategy.ts b/packages/core/src/config/order/price-calculation-strategy.ts index cc36a2f6a1..0ba2a52cec 100644 --- a/packages/core/src/config/order/price-calculation-strategy.ts +++ b/packages/core/src/config/order/price-calculation-strategy.ts @@ -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'; @@ -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; diff --git a/packages/core/src/service/services/order.service.ts b/packages/core/src/service/services/order.service.ts index f562a165e9..5a3c0ee3c7 100644 --- a/packages/core/src/service/services/order.service.ts +++ b/packages/core/src/service/services/order.service.ts @@ -378,6 +378,7 @@ export class OrderService { } const productVariant = orderLine.productVariant; const calculatedPrice = await priceCalculationStrategy.calculateUnitPrice( + ctx, productVariant, orderLine.customFields || {}, );