Browse Source

fix(core): Allow unsetting PaymentMethod checkers

Relates to #469
Michael Bromley 5 years ago
parent
commit
48c0e9641d

+ 1 - 1
packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts

@@ -2548,7 +2548,7 @@ export type ConfigArgDefinition = {
     type: Scalars['String'];
     list: Scalars['Boolean'];
     required: Scalars['Boolean'];
-    defaultValue?: Maybe<Scalars['String']>;
+    defaultValue?: Maybe<Scalars['JSON']>;
     label?: Maybe<Scalars['String']>;
     description?: Maybe<Scalars['String']>;
     ui?: Maybe<Scalars['JSON']>;

+ 1 - 1
packages/common/src/generated-shop-types.ts

@@ -652,7 +652,7 @@ export type ConfigArgDefinition = {
     type: Scalars['String'];
     list: Scalars['Boolean'];
     required: Scalars['Boolean'];
-    defaultValue?: Maybe<Scalars['String']>;
+    defaultValue?: Maybe<Scalars['JSON']>;
     label?: Maybe<Scalars['String']>;
     description?: Maybe<Scalars['String']>;
     ui?: Maybe<Scalars['JSON']>;

+ 1 - 1
packages/common/src/generated-types.ts

@@ -2740,7 +2740,7 @@ export type ConfigArgDefinition = {
   type: Scalars['String'];
   list: Scalars['Boolean'];
   required: Scalars['Boolean'];
-  defaultValue?: Maybe<Scalars['String']>;
+  defaultValue?: Maybe<Scalars['JSON']>;
   label?: Maybe<Scalars['String']>;
   description?: Maybe<Scalars['String']>;
   ui?: Maybe<Scalars['JSON']>;

+ 22 - 3
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -2548,7 +2548,7 @@ export type ConfigArgDefinition = {
     type: Scalars['String'];
     list: Scalars['Boolean'];
     required: Scalars['Boolean'];
-    defaultValue?: Maybe<Scalars['String']>;
+    defaultValue?: Maybe<Scalars['JSON']>;
     label?: Maybe<Scalars['String']>;
     description?: Maybe<Scalars['String']>;
     ui?: Maybe<Scalars['JSON']>;
@@ -6037,7 +6037,10 @@ export type GetOrderListWithQtyQuery = {
 export type PaymentMethodFragment = Pick<
     PaymentMethod,
     'id' | 'code' | 'name' | 'description' | 'enabled'
-> & { handler: Pick<ConfigurableOperation, 'code'> & { args: Array<Pick<ConfigArg, 'name' | 'value'>> } };
+> & {
+    checker?: Maybe<Pick<ConfigurableOperation, 'code'> & { args: Array<Pick<ConfigArg, 'name' | 'value'>> }>;
+    handler: Pick<ConfigurableOperation, 'code'> & { args: Array<Pick<ConfigArg, 'name' | 'value'>> };
+};
 
 export type CreatePaymentMethodMutationVariables = Exact<{
     input: CreatePaymentMethodInput;
@@ -6071,6 +6074,12 @@ export type GetPaymentMethodCheckersQuery = {
     >;
 };
 
+export type GetPaymentMethodQueryVariables = Exact<{
+    id: Scalars['ID'];
+}>;
+
+export type GetPaymentMethodQuery = { paymentMethod?: Maybe<PaymentMethodFragment> };
+
 export type UpdateProductOptionGroupMutationVariables = Exact<{
     input: UpdateProductOptionGroupInput;
 }>;
@@ -8156,8 +8165,12 @@ export namespace GetOrderListWithQty {
 
 export namespace PaymentMethod {
     export type Fragment = PaymentMethodFragment;
-    export type Handler = NonNullable<PaymentMethodFragment['handler']>;
+    export type Checker = NonNullable<PaymentMethodFragment['checker']>;
     export type Args = NonNullable<
+        NonNullable<NonNullable<PaymentMethodFragment['checker']>['args']>[number]
+    >;
+    export type Handler = NonNullable<PaymentMethodFragment['handler']>;
+    export type _Args = NonNullable<
         NonNullable<NonNullable<PaymentMethodFragment['handler']>['args']>[number]
     >;
 }
@@ -8202,6 +8215,12 @@ export namespace GetPaymentMethodCheckers {
     >;
 }
 
+export namespace GetPaymentMethod {
+    export type Variables = GetPaymentMethodQueryVariables;
+    export type Query = GetPaymentMethodQuery;
+    export type PaymentMethod = NonNullable<GetPaymentMethodQuery['paymentMethod']>;
+}
+
 export namespace UpdateProductOptionGroup {
     export type Variables = UpdateProductOptionGroupMutationVariables;
     export type Mutation = UpdateProductOptionGroupMutation;

+ 1 - 1
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -627,7 +627,7 @@ export type ConfigArgDefinition = {
     type: Scalars['String'];
     list: Scalars['Boolean'];
     required: Scalars['Boolean'];
-    defaultValue?: Maybe<Scalars['String']>;
+    defaultValue?: Maybe<Scalars['JSON']>;
     label?: Maybe<Scalars['String']>;
     description?: Maybe<Scalars['String']>;
     ui?: Maybe<Scalars['JSON']>;

+ 47 - 1
packages/core/e2e/payment-method.e2e-spec.ts

@@ -13,6 +13,7 @@ import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-conf
 
 import {
     CreatePaymentMethod,
+    GetPaymentMethod,
     GetPaymentMethodCheckers,
     GetPaymentMethodHandlers,
     UpdatePaymentMethod,
@@ -97,6 +98,7 @@ describe('PaymentMethod resolver', () => {
             code: 'no-checks',
             description: 'This is a test payment method',
             enabled: true,
+            checker: null,
             handler: {
                 args: [
                     {
@@ -117,6 +119,10 @@ describe('PaymentMethod resolver', () => {
             input: {
                 id: 'T_1',
                 description: 'modified',
+                checker: {
+                    code: minPriceChecker.code,
+                    arguments: [{ name: 'minPrice', value: '0' }],
+                },
                 handler: {
                     code: dummyPaymentHandler.code,
                     arguments: [{ name: 'automaticSettle', value: 'false' }],
@@ -130,6 +136,10 @@ describe('PaymentMethod resolver', () => {
             code: 'no-checks',
             description: 'modified',
             enabled: true,
+            checker: {
+                args: [{ name: 'minPrice', value: '0' }],
+                code: minPriceChecker.code,
+            },
             handler: {
                 args: [
                     {
@@ -137,9 +147,29 @@ describe('PaymentMethod resolver', () => {
                         value: 'false',
                     },
                 ],
-                code: 'dummy-payment-handler',
+                code: dummyPaymentHandler.code,
+            },
+        });
+    });
+
+    it('unset checker', async () => {
+        const { updatePaymentMethod } = await adminClient.query<
+            UpdatePaymentMethod.Mutation,
+            UpdatePaymentMethod.Variables
+        >(UPDATE_PAYMENT_METHOD, {
+            input: {
+                id: 'T_1',
+                checker: null,
             },
         });
+
+        expect(updatePaymentMethod.checker).toEqual(null);
+
+        const { paymentMethod } = await adminClient.query<GetPaymentMethod.Query, GetPaymentMethod.Variables>(
+            GET_PAYMENT_METHOD,
+            { id: 'T_1' },
+        );
+        expect(paymentMethod.checker).toEqual(null);
     });
 
     it('paymentMethodEligibilityCheckers', async () => {
@@ -260,6 +290,13 @@ export const PAYMENT_METHOD_FRAGMENT = gql`
         name
         description
         enabled
+        checker {
+            code
+            args {
+                name
+                value
+            }
+        }
         handler {
             code
             args {
@@ -311,3 +348,12 @@ export const GET_PAYMENT_METHOD_CHECKERS = gql`
         }
     }
 `;
+
+export const GET_PAYMENT_METHOD = gql`
+    query GetPaymentMethod($id: ID!) {
+        paymentMethod(id: $id) {
+            ...PaymentMethod
+        }
+    }
+    ${PAYMENT_METHOD_FRAGMENT}
+`;

+ 9 - 1
packages/core/mock-data/data-sources/initial-data.ts

@@ -14,7 +14,15 @@ export const initialData: InitialData = {
         { name: 'Standard Shipping', price: 500 },
         { name: 'Express Shipping', price: 1000 },
     ],
-    paymentMethods: [{ name: 'Standard Payment', handler: { code: 'dummy-payment-handler', arguments: [] } }],
+    paymentMethods: [
+        {
+            name: 'Standard Payment',
+            handler: {
+                code: 'dummy-payment-handler',
+                arguments: [{ name: 'automaticSettle', value: 'false' }],
+            },
+        },
+    ],
     collections: [
         {
             name: 'Electronics',

+ 2 - 1
packages/core/src/api/middleware/id-codec-plugin.ts

@@ -1,3 +1,4 @@
+import { isObject } from '@vendure/common/lib/shared-utils';
 import { ApolloServerPlugin, GraphQLRequestListener, GraphQLServiceContext } from 'apollo-server-plugin-base';
 import { DocumentNode, OperationDefinitionNode } from 'graphql';
 
@@ -36,7 +37,7 @@ export class IdCodecPlugin implements ApolloServerPlugin {
         const typeTree = this.graphqlValueTransformer.getOutputTypeTree(document);
         this.graphqlValueTransformer.transformValues(typeTree, data, (value, type) => {
             const isIdType = type && type.name === 'ID';
-            if (type && type.name === 'JSON') {
+            if (type && type.name === 'JSON' && isObject(value)) {
                 return this.idCodecService.encode(value, [
                     'paymentId',
                     'fulfillmentId',

+ 1 - 1
packages/core/src/api/schema/common/common-types.graphql

@@ -39,7 +39,7 @@ type ConfigArgDefinition {
     type: String!
     list: Boolean!
     required: Boolean!
-    defaultValue: String
+    defaultValue: JSON
     label: String
     description: String
     ui: JSON

+ 1 - 0
packages/core/src/data-import/providers/populator/populator.ts

@@ -228,6 +228,7 @@ export class Populator {
             await this.paymentMethodService.create(ctx, {
                 name: method.name,
                 code: normalizeString(method.name, '-'),
+                description: '',
                 enabled: true,
                 handler: method.handler,
             });

+ 1 - 1
packages/core/src/entity/payment-method/payment-method.entity.ts

@@ -25,7 +25,7 @@ export class PaymentMethod extends VendureEntity {
 
     @Column() enabled: boolean;
 
-    @Column('simple-json', { nullable: true }) checker?: ConfigurableOperation;
+    @Column('simple-json', { nullable: true }) checker: ConfigurableOperation | null;
 
     @Column('simple-json') handler: ConfigurableOperation;
 }

+ 5 - 2
packages/core/src/service/services/payment-method.service.ts

@@ -79,11 +79,14 @@ export class PaymentMethodService {
         const paymentMethod = await this.connection.getEntityOrThrow(ctx, PaymentMethod, input.id);
         const updatedPaymentMethod = patchEntity(paymentMethod, omit(input, ['handler', 'checker']));
         if (input.checker) {
-            paymentMethod.handler = this.configArgService.parseInput(
+            paymentMethod.checker = this.configArgService.parseInput(
                 'PaymentMethodEligibilityChecker',
                 input.checker,
             );
         }
+        if (input.checker === null) {
+            paymentMethod.checker = null;
+        }
         if (input.handler) {
             paymentMethod.handler = this.configArgService.parseInput('PaymentMethodHandler', input.handler);
         }
@@ -245,7 +248,7 @@ export class PaymentMethodService {
     ): Promise<{
         paymentMethod: PaymentMethod;
         handler: PaymentMethodHandler;
-        checker: PaymentMethodEligibilityChecker | undefined;
+        checker: PaymentMethodEligibilityChecker | null;
     }> {
         const paymentMethod = await this.connection.getRepository(ctx, PaymentMethod).findOne({
             where: {

+ 1 - 1
packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts

@@ -2548,7 +2548,7 @@ export type ConfigArgDefinition = {
     type: Scalars['String'];
     list: Scalars['Boolean'];
     required: Scalars['Boolean'];
-    defaultValue?: Maybe<Scalars['String']>;
+    defaultValue?: Maybe<Scalars['JSON']>;
     label?: Maybe<Scalars['String']>;
     description?: Maybe<Scalars['String']>;
     ui?: Maybe<Scalars['JSON']>;

File diff suppressed because it is too large
+ 0 - 0
schema-admin.json


File diff suppressed because it is too large
+ 0 - 0
schema-shop.json


Some files were not shown because too many files changed in this diff