Просмотр исходного кода

feat(core): Pass shipping method to calculator and eligibility checker (#1509)

Alexander Shitikov 3 лет назад
Родитель
Сommit
826aa4af01

+ 4 - 3
packages/core/src/config/shipping-method/shipping-calculator.ts

@@ -7,7 +7,7 @@ import {
     ConfigurableOperationDef,
     ConfigurableOperationDefOptions,
 } from '../../common/configurable-operation';
-import { Order } from '../../entity/order/order.entity';
+import { ShippingMethod, Order } from '../../entity';
 
 export interface ShippingCalculatorConfig<T extends ConfigArgs> extends ConfigurableOperationDefOptions<T> {
     calculate: CalculateShippingFn<T>;
@@ -59,8 +59,8 @@ export class ShippingCalculator<T extends ConfigArgs = ConfigArgs> extends Confi
      *
      * @internal
      */
-    calculate(ctx: RequestContext, order: Order, args: ConfigArg[]): CalculateShippingFnResult {
-        return this.calculateFn(ctx, order, this.argsArrayToHash(args));
+    calculate(ctx: RequestContext, order: Order, args: ConfigArg[], method: ShippingMethod): CalculateShippingFnResult {
+        return this.calculateFn(ctx, order, this.argsArrayToHash(args), method);
     }
 }
 
@@ -115,4 +115,5 @@ export type CalculateShippingFn<T extends ConfigArgs> = (
     ctx: RequestContext,
     order: Order,
     args: ConfigArgValues<T>,
+    method: ShippingMethod
 ) => CalculateShippingFnResult;

+ 8 - 6
packages/core/src/config/shipping-method/shipping-eligibility-checker.ts

@@ -10,7 +10,7 @@ import {
     ConfigurableOperationDefOptions,
 } from '../../common/configurable-operation';
 import { TtlCache } from '../../common/ttl-cache';
-import { Order } from '../../entity/order/order.entity';
+import { ShippingMethod, Order } from '../../entity';
 
 /**
  * @description
@@ -65,20 +65,20 @@ export class ShippingEligibilityChecker<T extends ConfigArgs = ConfigArgs> exten
      *
      * @internal
      */
-    async check(ctx: RequestContext, order: Order, args: ConfigArg[]): Promise<boolean> {
-        const shouldRunCheck = await this.shouldRunCheck(ctx, order, args);
-        return shouldRunCheck ? this.checkFn(ctx, order, this.argsArrayToHash(args)) : true;
+    async check(ctx: RequestContext, order: Order, args: ConfigArg[], method: ShippingMethod): Promise<boolean> {
+        const shouldRunCheck = await this.shouldRunCheck(ctx, order, args, method);
+        return shouldRunCheck ? this.checkFn(ctx, order, this.argsArrayToHash(args), method) : true;
     }
 
     /**
      * Determines whether the check function needs to be run, based on the presence and
      * result of any defined `shouldRunCheckFn`.
      */
-    private async shouldRunCheck(ctx: RequestContext, order: Order, args: ConfigArg[]): Promise<boolean> {
+    private async shouldRunCheck(ctx: RequestContext, order: Order, args: ConfigArg[], method: ShippingMethod): Promise<boolean> {
         if (typeof this.shouldRunCheckFn === 'function') {
             const cacheKey = ctx.session?.id;
             if (cacheKey) {
-                const checkResult = await this.shouldRunCheckFn(ctx, order, this.argsArrayToHash(args));
+                const checkResult = await this.shouldRunCheckFn(ctx, order, this.argsArrayToHash(args), method);
                 const checkResultHash = createHash('sha1')
                     .update(JSON.stringify(checkResult))
                     .digest('base64');
@@ -109,6 +109,7 @@ export type CheckShippingEligibilityCheckerFn<T extends ConfigArgs> = (
     ctx: RequestContext,
     order: Order,
     args: ConfigArgValues<T>,
+    method: ShippingMethod
 ) => boolean | Promise<boolean>;
 
 /**
@@ -143,4 +144,5 @@ export type ShouldRunCheckFn<T extends ConfigArgs> = (
     ctx: RequestContext,
     order: Order,
     args: ConfigArgValues<T>,
+    method: ShippingMethod
 ) => Json | Promise<Json>;

+ 2 - 2
packages/core/src/entity/shipping-method/shipping-method.entity.ts

@@ -73,7 +73,7 @@ export class ShippingMethod
     async apply(ctx: RequestContext, order: Order): Promise<ShippingCalculationResult | undefined> {
         const calculator = this.allCalculators[this.calculator.code];
         if (calculator) {
-            const response = await calculator.calculate(ctx, order, this.calculator.args);
+            const response = await calculator.calculate(ctx, order, this.calculator.args, this);
             if (response) {
                 const { price, priceIncludesTax, taxRate, metadata } = response;
                 return {
@@ -89,7 +89,7 @@ export class ShippingMethod
     async test(ctx: RequestContext, order: Order): Promise<boolean> {
         const checker = this.allCheckers[this.checker.code];
         if (checker) {
-            return checker.check(ctx, order, this.checker.args);
+            return checker.check(ctx, order, this.checker.args, this);
         } else {
             return false;
         }