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

refactor(server): Extract common logic for marshalling data

Michael Bromley 6 лет назад
Родитель
Сommit
501e097bec

+ 1 - 9
server/src/api/resolvers/admin/collection.resolver.ts

@@ -30,15 +30,7 @@ export class CollectionResolver {
         @Ctx() ctx: RequestContext,
         @Args() args: CollectionsQueryArgs,
     ): Promise<ConfigurableOperation[]> {
-        // TODO: extract to common util bc it is used in at least 3 places.
-        const toAdjustmentOperation = (source: CollectionFilter<any>) => {
-            return {
-                code: source.code,
-                description: source.description,
-                args: Object.entries(source.args).map(([name, type]) => ({ name, type })),
-            };
-        };
-        return this.collectionService.getAvailableFilters().map(toAdjustmentOperation);
+        return this.collectionService.getAvailableFilters();
     }
 
     @Query()

+ 25 - 13
server/src/common/types/configurable-operation.ts → server/src/common/configurable-operation.ts

@@ -1,7 +1,7 @@
 // prettier-ignore
-import { ConfigArg } from '../../../../shared/generated-types';
-import { PromotionActionArgs, PromotionActionConfig } from '../../config/promotion/promotion-action';
-import { InternalServerError } from '../error/errors';
+import { ConfigArg, ConfigurableOperation } from '../../../shared/generated-types';
+
+import { InternalServerError } from './error/errors';
 
 /**
  * Certain entities allow arbitrary configuration arguments to be specified which can then
@@ -39,6 +39,28 @@ export type ConfigArgValues<T extends ConfigArgs<any>> = {
         : string
 };
 
+/**
+ * Defines a ConfigurableOperation, which is a method which can be configured
+ * by the Administrator via the Admin API.
+ */
+export interface ConfigurableOperationDef {
+    code: string;
+    args: ConfigArgs<any>;
+    description: string;
+}
+
+/**
+ * Convert a ConfigurableOperationDef into a ConfigurableOperation object, typically
+ * so that it can be sent via the API.
+ */
+export function configurableDefToOperation(def: ConfigurableOperationDef): ConfigurableOperation {
+    return {
+        code: def.code,
+        description: def.description,
+        args: Object.entries(def.args).map(([name, type]) => ({ name, type })),
+    };
+}
+
 /**
  * Coverts an array of ConfigArgs into a hash object:
  *
@@ -77,13 +99,3 @@ function coerceValueToType<T>(arg: ConfigArg): ConfigArgValues<T>[keyof T] {
             return (arg.value as string) as any;
     }
 }
-
-/**
- * Defines a ConfigurableOperation, which is a method which can be configured
- * by the Administrator via the Admin API.
- */
-export interface ConfigurableOperationDef {
-    code: string;
-    args: ConfigArgs<any>;
-    description: string;
-}

+ 1 - 1
server/src/config/collection/collection-filter.ts

@@ -6,7 +6,7 @@ import {
     ConfigArgs,
     ConfigArgValues,
     ConfigurableOperationDef,
-} from '../../common/types/configurable-operation';
+} from '../../common/configurable-operation';
 import { ProductVariant } from '../../entity/product-variant/product-variant.entity';
 
 export type CollectionFilterArgType = 'facetValueIds';

+ 1 - 1
server/src/config/payment-method/payment-method-handler.ts

@@ -1,7 +1,7 @@
 import { ConfigArg } from '../../../../shared/generated-types';
 
+import { argsArrayToHash, ConfigArgs, ConfigArgValues } from '../../common/configurable-operation';
 import { StateMachineConfig } from '../../common/finite-state-machine';
-import { argsArrayToHash, ConfigArgs, ConfigArgValues } from '../../common/types/configurable-operation';
 import { Order } from '../../entity/order/order.entity';
 import { PaymentMetadata } from '../../entity/payment/payment.entity';
 import {

+ 1 - 1
server/src/config/promotion/promotion-action.ts

@@ -4,7 +4,7 @@ import {
     ConfigArgs,
     ConfigArgValues,
     ConfigurableOperationDef,
-} from '../../common/types/configurable-operation';
+} from '../../common/configurable-operation';
 import { OrderItem } from '../../entity/order-item/order-item.entity';
 import { OrderLine } from '../../entity/order-line/order-line.entity';
 import { Order } from '../../entity/order/order.entity';

+ 1 - 1
server/src/config/promotion/promotion-condition.ts

@@ -6,7 +6,7 @@ import {
     ConfigArgs,
     ConfigArgValues,
     ConfigurableOperationDef,
-} from '../../common/types/configurable-operation';
+} from '../../common/configurable-operation';
 import { OrderLine } from '../../entity';
 import { Order } from '../../entity/order/order.entity';
 

+ 2 - 2
server/src/config/shipping-method/shipping-calculator.ts

@@ -1,7 +1,7 @@
 import { ConfigArg } from '../../../../shared/generated-types';
 
-import { ConfigArgs, ConfigurableOperationDef } from '../../common/types/configurable-operation';
-import { argsArrayToHash, ConfigArgValues } from '../../common/types/configurable-operation';
+import { ConfigArgs, ConfigurableOperationDef } from '../../common/configurable-operation';
+import { argsArrayToHash, ConfigArgValues } from '../../common/configurable-operation';
 import { Order } from '../../entity/order/order.entity';
 
 export type ShippingCalculatorArgType = 'int' | 'money' | 'string' | 'boolean';

+ 2 - 2
server/src/config/shipping-method/shipping-eligibility-checker.ts

@@ -1,7 +1,7 @@
 import { ConfigArg } from '../../../../shared/generated-types';
 
-import { ConfigArgs, ConfigurableOperationDef } from '../../common/types/configurable-operation';
-import { argsArrayToHash, ConfigArgValues } from '../../common/types/configurable-operation';
+import { ConfigArgs, ConfigurableOperationDef } from '../../common/configurable-operation';
+import { argsArrayToHash, ConfigArgValues } from '../../common/configurable-operation';
 import { Order } from '../../entity/order/order.entity';
 
 export type ShippingEligibilityCheckerArgType = 'int' | 'money' | 'string' | 'boolean';

+ 3 - 2
server/src/service/services/collection.service.ts

@@ -10,6 +10,7 @@ import {
 import { ROOT_CATEGORY_NAME } from '../../../../shared/shared-constants';
 import { ID, PaginatedList } from '../../../../shared/shared-types';
 import { RequestContext } from '../../api/common/request-context';
+import { configurableDefToOperation } from '../../common/configurable-operation';
 import { DEFAULT_LANGUAGE_CODE } from '../../common/constants';
 import { IllegalOperationError, UserInputError } from '../../common/error/errors';
 import { ListQueryOptions } from '../../common/types/common-types';
@@ -78,8 +79,8 @@ export class CollectionService {
         return translateDeep(collection, ctx.languageCode, ['parent']);
     }
 
-    getAvailableFilters(): Array<CollectionFilter<any>> {
-        return this.availableFilters;
+    getAvailableFilters(): ConfigurableOperation[] {
+        return this.availableFilters.map(configurableDefToOperation);
     }
 
     /**

+ 3 - 9
server/src/service/services/promotion.service.ts

@@ -13,6 +13,7 @@ import {
 import { omit } from '../../../../shared/omit';
 import { ID, PaginatedList } from '../../../../shared/shared-types';
 import { RequestContext } from '../../api/common/request-context';
+import { configurableDefToOperation } from '../../common/configurable-operation';
 import { UserInputError } from '../../common/error/errors';
 import { ListQueryOptions } from '../../common/types/common-types';
 import { assertFound } from '../../common/utils';
@@ -68,16 +69,9 @@ export class PromotionService {
         conditions: ConfigurableOperation[];
         actions: ConfigurableOperation[];
     } {
-        const toAdjustmentOperation = (source: PromotionCondition | PromotionAction) => {
-            return {
-                code: source.code,
-                description: source.description,
-                args: Object.entries(source.args).map(([name, type]) => ({ name, type })),
-            };
-        };
         return {
-            conditions: this.availableConditions.map(toAdjustmentOperation),
-            actions: this.availableActions.map(toAdjustmentOperation),
+            conditions: this.availableConditions.map(configurableDefToOperation),
+            actions: this.availableActions.map(configurableDefToOperation),
         };
     }
 

+ 3 - 10
server/src/service/services/shipping-method.service.ts

@@ -10,6 +10,7 @@ import {
 } from '../../../../shared/generated-types';
 import { omit } from '../../../../shared/omit';
 import { ID, PaginatedList } from '../../../../shared/shared-types';
+import { configurableDefToOperation } from '../../common/configurable-operation';
 import { EntityNotFoundError, UserInputError } from '../../common/error/errors';
 import { ListQueryOptions } from '../../common/types/common-types';
 import { assertFound } from '../../common/utils';
@@ -97,25 +98,17 @@ export class ShippingMethodService {
     }
 
     getShippingEligibilityCheckers(): ConfigurableOperation[] {
-        return this.shippingEligibilityCheckers.map(this.toAdjustmentOperation);
+        return this.shippingEligibilityCheckers.map(configurableDefToOperation);
     }
 
     getShippingCalculators(): ConfigurableOperation[] {
-        return this.shippingCalculators.map(this.toAdjustmentOperation);
+        return this.shippingCalculators.map(configurableDefToOperation);
     }
 
     getActiveShippingMethods(channel: Channel): ShippingMethod[] {
         return this.activeShippingMethods.filter(sm => sm.channels.find(c => c.id === channel.id));
     }
 
-    private toAdjustmentOperation(source: ShippingCalculator | ShippingEligibilityChecker) {
-        return {
-            code: source.code,
-            description: source.description,
-            args: Object.entries(source.args).map(([name, type]) => ({ name, type })),
-        };
-    }
-
     /**
      * Converts the input values of the "create" and "update" mutations into the format expected by the ShippingMethod entity.
      */