Skip to content

Commit

Permalink
feat(core): Pass RequestContext to ShippingEligibilityChecker functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The ShippingEligibilityChecker `check()`
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 6eee894 commit a5db022
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const defaultShippingEligibilityChecker = new ShippingEligibilityChecker(
],
},
},
check: (order, args) => {
check: (ctx, order, args) => {
return order.total >= args.orderMinimum;
},
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigArg } from '@vendure/common/lib/generated-types';

import { RequestContext } from '../../api/common/request-context';
import {
ConfigArgs,
ConfigArgValues,
Expand Down Expand Up @@ -57,8 +58,8 @@ export class ShippingEligibilityChecker<T extends ConfigArgs = ConfigArgs> exten
*
* @internal
*/
check(order: Order, args: ConfigArg[]): boolean | Promise<boolean> {
return this.checkFn(order, this.argsArrayToHash(args));
check(ctx: RequestContext, order: Order, args: ConfigArg[]): boolean | Promise<boolean> {
return this.checkFn(ctx, order, this.argsArrayToHash(args));
}
}

Expand All @@ -70,6 +71,7 @@ export class ShippingEligibilityChecker<T extends ConfigArgs = ConfigArgs> exten
* @docsCategory shipping
*/
export type CheckShippingEligibilityCheckerFn<T extends ConfigArgs> = (
ctx: RequestContext,
order: Order,
args: ConfigArgValues<T>,
) => boolean | Promise<boolean>;
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export class ShippingMethod extends VendureEntity implements ChannelAware, SoftD
}
}

async test(order: Order): Promise<boolean> {
async test(ctx: RequestContext, order: Order): Promise<boolean> {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ShippingCalculator {
order: Order,
method: ShippingMethod,
): Promise<EligibleShippingMethod | undefined> {
const eligible = await method.test(order);
const eligible = await method.test(ctx, order);
if (eligible) {
const result = await method.apply(ctx, order);
if (result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a5db022

Please sign in to comment.