From a5db02285a2a867a7541738b2779b2302beaeb93 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 27 Oct 2020 15:17:13 +0100 Subject: [PATCH] feat(core): Pass RequestContext to ShippingEligibilityChecker functions BREAKING CHANGE: The ShippingEligibilityChecker `check()` function signature has changed: the first argument is now the RequestContext of the current request. --- .../shipping-method/default-shipping-eligibility-checker.ts | 2 +- .../config/shipping-method/shipping-eligibility-checker.ts | 6 ++++-- .../src/entity/shipping-method/shipping-method.entity.ts | 4 ++-- .../helpers/shipping-calculator/shipping-calculator.ts | 2 +- packages/core/src/service/services/order-testing.service.ts | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/core/src/config/shipping-method/default-shipping-eligibility-checker.ts b/packages/core/src/config/shipping-method/default-shipping-eligibility-checker.ts index 32811f6d96..9e307ca0ef 100644 --- a/packages/core/src/config/shipping-method/default-shipping-eligibility-checker.ts +++ b/packages/core/src/config/shipping-method/default-shipping-eligibility-checker.ts @@ -18,7 +18,7 @@ export const defaultShippingEligibilityChecker = new ShippingEligibilityChecker( ], }, }, - check: (order, args) => { + check: (ctx, order, args) => { return order.total >= args.orderMinimum; }, }); diff --git a/packages/core/src/config/shipping-method/shipping-eligibility-checker.ts b/packages/core/src/config/shipping-method/shipping-eligibility-checker.ts index db837c6350..0a29abf07c 100644 --- a/packages/core/src/config/shipping-method/shipping-eligibility-checker.ts +++ b/packages/core/src/config/shipping-method/shipping-eligibility-checker.ts @@ -1,5 +1,6 @@ import { ConfigArg } from '@vendure/common/lib/generated-types'; +import { RequestContext } from '../../api/common/request-context'; import { ConfigArgs, ConfigArgValues, @@ -57,8 +58,8 @@ export class ShippingEligibilityChecker exten * * @internal */ - check(order: Order, args: ConfigArg[]): boolean | Promise { - return this.checkFn(order, this.argsArrayToHash(args)); + check(ctx: RequestContext, order: Order, args: ConfigArg[]): boolean | Promise { + return this.checkFn(ctx, order, this.argsArrayToHash(args)); } } @@ -70,6 +71,7 @@ export class ShippingEligibilityChecker exten * @docsCategory shipping */ export type CheckShippingEligibilityCheckerFn = ( + ctx: RequestContext, order: Order, args: ConfigArgValues, ) => boolean | Promise; diff --git a/packages/core/src/entity/shipping-method/shipping-method.entity.ts b/packages/core/src/entity/shipping-method/shipping-method.entity.ts index 27ff9065b1..2f86134bcf 100644 --- a/packages/core/src/entity/shipping-method/shipping-method.entity.ts +++ b/packages/core/src/entity/shipping-method/shipping-method.entity.ts @@ -72,10 +72,10 @@ export class ShippingMethod extends VendureEntity implements ChannelAware, SoftD } } - async test(order: Order): Promise { + async test(ctx: RequestContext, order: Order): Promise { const checker = this.allCheckers[this.checker.code]; if (checker) { - return checker.check(order, this.checker.args); + return checker.check(ctx, order, this.checker.args); } else { return false; } diff --git a/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts b/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts index 057d12afd5..db395634ef 100644 --- a/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts +++ b/packages/core/src/service/helpers/shipping-calculator/shipping-calculator.ts @@ -36,7 +36,7 @@ export class ShippingCalculator { order: Order, method: ShippingMethod, ): Promise { - const eligible = await method.test(order); + const eligible = await method.test(ctx, order); if (eligible) { const result = await method.apply(ctx, order); if (result) { diff --git a/packages/core/src/service/services/order-testing.service.ts b/packages/core/src/service/services/order-testing.service.ts index dc13f207b0..82b930278c 100644 --- a/packages/core/src/service/services/order-testing.service.ts +++ b/packages/core/src/service/services/order-testing.service.ts @@ -45,7 +45,7 @@ export class OrderTestingService { calculator: this.shippingConfiguration.parseCalculatorInput(input.calculator), }); const mockOrder = await this.buildMockOrder(ctx, input.shippingAddress, input.lines); - const eligible = await shippingMethod.test(mockOrder); + const eligible = await shippingMethod.test(ctx, mockOrder); const result = eligible ? await shippingMethod.apply(ctx, mockOrder) : undefined; return { eligible,