Преглед на файлове

fix(core): Correctly resolve Customer.User property

Michael Bromley преди 6 години
родител
ревизия
c11c8a0afe

+ 29 - 0
packages/core/e2e/customer.e2e-spec.ts

@@ -24,6 +24,7 @@ import {
     GetCustomer,
     GetCustomerList,
     GetCustomerOrders,
+    GetCustomerWithUser,
     UpdateAddress,
     UpdateCustomer,
 } from './graphql/generated-e2e-admin-types';
@@ -86,6 +87,21 @@ describe('Customer resolver', () => {
         thirdCustomer = result.customers.items[2];
     });
 
+    it('customer resolver resolves User', async () => {
+        const { customer } = await adminClient.query<
+            GetCustomerWithUser.Query,
+            GetCustomerWithUser.Variables
+        >(GET_CUSTOMER_WITH_USER, {
+            id: firstCustomer.id,
+        });
+
+        expect(customer!.user).toEqual({
+            id: 'T_2',
+            identifier: firstCustomer.emailAddress,
+            verified: true,
+        });
+    });
+
     describe('addresses', () => {
         let firstCustomerAddressIds: string[] = [];
         let firstCustomerThirdAddressId: string;
@@ -429,6 +445,19 @@ describe('Customer resolver', () => {
     });
 });
 
+const GET_CUSTOMER_WITH_USER = gql`
+    query GetCustomerWithUser($id: ID!) {
+        customer(id: $id) {
+            id
+            user {
+                id
+                identifier
+                verified
+            }
+        }
+    }
+`;
+
 const CREATE_ADDRESS = gql`
     mutation CreateAddress($id: ID!, $input: CreateAddressInput!) {
         createCustomerAddress(customerId: $id, input: $input) {

+ 19 - 0
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -3660,6 +3660,18 @@ export type DeleteCustomerAddressMutation = { __typename?: 'Mutation' } & Pick<
     'deleteCustomerAddress'
 >;
 
+export type GetCustomerWithUserQueryVariables = {
+    id: Scalars['ID'];
+};
+
+export type GetCustomerWithUserQuery = { __typename?: 'Query' } & {
+    customer: Maybe<
+        { __typename?: 'Customer' } & Pick<Customer, 'id'> & {
+                user: Maybe<{ __typename?: 'User' } & Pick<User, 'id' | 'identifier' | 'verified'>>;
+            }
+    >;
+};
+
 export type CreateAddressMutationVariables = {
     id: Scalars['ID'];
     input: CreateAddressInput;
@@ -5325,6 +5337,13 @@ export namespace DeleteCustomerAddress {
     export type Mutation = DeleteCustomerAddressMutation;
 }
 
+export namespace GetCustomerWithUser {
+    export type Variables = GetCustomerWithUserQueryVariables;
+    export type Query = GetCustomerWithUserQuery;
+    export type Customer = NonNullable<GetCustomerWithUserQuery['customer']>;
+    export type User = NonNullable<(NonNullable<GetCustomerWithUserQuery['customer']>)['user']>;
+}
+
 export namespace CreateAddress {
     export type Variables = CreateAddressMutationVariables;
     export type Mutation = CreateAddressMutation;

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

@@ -2251,7 +2251,11 @@ export type TestOrderFragmentFragment = { __typename?: 'Order' } & Pick<
         shippingMethod: Maybe<
             { __typename?: 'ShippingMethod' } & Pick<ShippingMethod, 'id' | 'code' | 'description'>
         >;
-        customer: Maybe<{ __typename?: 'Customer' } & Pick<Customer, 'id'>>;
+        customer: Maybe<
+            { __typename?: 'Customer' } & Pick<Customer, 'id'> & {
+                    user: Maybe<{ __typename?: 'User' } & Pick<User, 'id' | 'identifier'>>;
+                }
+        >;
         history: { __typename?: 'HistoryEntryList' } & {
             items: Array<{ __typename?: 'HistoryEntry' } & Pick<HistoryEntry, 'id' | 'type' | 'data'>>;
         };
@@ -2617,6 +2621,7 @@ export namespace TestOrderFragment {
     export type ProductVariant = (NonNullable<TestOrderFragmentFragment['lines'][0]>)['productVariant'];
     export type ShippingMethod = NonNullable<TestOrderFragmentFragment['shippingMethod']>;
     export type Customer = NonNullable<TestOrderFragmentFragment['customer']>;
+    export type User = NonNullable<(NonNullable<TestOrderFragmentFragment['customer']>)['user']>;
     export type History = TestOrderFragmentFragment['history'];
     export type Items = NonNullable<TestOrderFragmentFragment['history']['items'][0]>;
 }

+ 4 - 0
packages/core/e2e/graphql/shop-definitions.ts

@@ -29,6 +29,10 @@ export const TEST_ORDER_FRAGMENT = gql`
         }
         customer {
             id
+            user {
+                id
+                identifier
+            }
         }
         history {
             items {

+ 8 - 0
packages/core/e2e/shop-order.e2e-spec.ts

@@ -484,6 +484,14 @@ describe('Shop orders', () => {
             expect(result.activeOrder!.state).toBe('AddingItems');
         });
 
+        it('activeOrder resolves customer user', async () => {
+            const result = await shopClient.query<GetActiveOrder.Query>(GET_ACTIVE_ORDER);
+            expect(result.activeOrder!.customer!.user).toEqual({
+                id: 'T_2',
+                identifier: 'hayden.zieme12@hotmail.com',
+            });
+        });
+
         it('addItemToOrder with an existing productVariantId adds quantity to the existing OrderLine', async () => {
             const { addItemToOrder } = await shopClient.query<
                 AddItemToOrder.Mutation,

+ 15 - 1
packages/core/src/api/resolvers/entity/customer-entity.resolver.ts

@@ -7,12 +7,17 @@ import { Customer } from '../../../entity/customer/customer.entity';
 import { Order } from '../../../entity/order/order.entity';
 import { CustomerService } from '../../../service/services/customer.service';
 import { OrderService } from '../../../service/services/order.service';
+import { UserService } from '../../../service/services/user.service';
 import { RequestContext } from '../../common/request-context';
 import { Ctx } from '../../decorators/request-context.decorator';
 
 @Resolver('Customer')
 export class CustomerEntityResolver {
-    constructor(private customerService: CustomerService, private orderService: OrderService) {}
+    constructor(
+        private customerService: CustomerService,
+        private orderService: OrderService,
+        private userService: UserService,
+    ) {}
     @ResolveProperty()
     async addresses(@Ctx() ctx: RequestContext, @Parent() customer: Customer): Promise<Address[]> {
         return this.customerService.findAddressesByCustomerId(ctx, customer.id);
@@ -26,4 +31,13 @@ export class CustomerEntityResolver {
     ): Promise<PaginatedList<Order>> {
         return this.orderService.findByCustomerId(ctx, customer.id, args.options || undefined);
     }
+
+    @ResolveProperty()
+    user(@Ctx() ctx: RequestContext, @Parent() customer: Customer) {
+        if (customer.user) {
+            return customer.user;
+        }
+
+        return this.userService.getUserByEmailAddress(customer.emailAddress);
+    }
 }

+ 2 - 2
packages/dev-server/dev-config.ts

@@ -37,10 +37,10 @@ export const devConfig: VendureConfig = {
         paymentMethodHandlers: [examplePaymentHandler],
     },
     customFields: {
-        Product: [
+        /*Product: [
             { name: 'rating', type: 'float', readonly: true },
             { name: 'markup', type: 'float', internal: true },
-        ],
+        ],*/
     },
     logger: new DefaultLogger({ level: LogLevel.Info }),
     importExportOptions: {