Skip to content

Commit

Permalink
fix(core): Allow unsetting PaymentMethod checkers
Browse files Browse the repository at this point in the history
Relates to #469
  • Loading branch information
michaelbromley committed Feb 1, 2021
1 parent e528c09 commit 48c0e96
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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']>;
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']>;
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']>;
Expand Down
25 changes: 22 additions & 3 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}>;
Expand Down Expand Up @@ -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]
>;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']>;
Expand Down
48 changes: 47 additions & 1 deletion packages/core/e2e/payment-method.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-conf

import {
CreatePaymentMethod,
GetPaymentMethod,
GetPaymentMethodCheckers,
GetPaymentMethodHandlers,
UpdatePaymentMethod,
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('PaymentMethod resolver', () => {
code: 'no-checks',
description: 'This is a test payment method',
enabled: true,
checker: null,
handler: {
args: [
{
Expand All @@ -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' }],
Expand All @@ -130,16 +136,40 @@ describe('PaymentMethod resolver', () => {
code: 'no-checks',
description: 'modified',
enabled: true,
checker: {
args: [{ name: 'minPrice', value: '0' }],
code: minPriceChecker.code,
},
handler: {
args: [
{
name: 'automaticSettle',
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 () => {
Expand Down Expand Up @@ -260,6 +290,13 @@ export const PAYMENT_METHOD_FRAGMENT = gql`
name
description
enabled
checker {
code
args {
name
value
}
}
handler {
code
args {
Expand Down Expand Up @@ -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}
`;
10 changes: 9 additions & 1 deletion packages/core/mock-data/data-sources/initial-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/api/middleware/id-codec-plugin.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/schema/common/common-types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ConfigArgDefinition {
type: String!
list: Boolean!
required: Boolean!
defaultValue: String
defaultValue: JSON
label: String
description: String
ui: JSON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions packages/core/src/service/services/payment-method.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']>;
Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 48c0e96

Please sign in to comment.