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

feat(core): Enable filtering customers by postalCode

Relates to #1389
Michael Bromley 3 лет назад
Родитель
Сommit
6692b95e47

+ 1 - 0
packages/admin-ui/src/lib/core/src/common/generated-types.ts

@@ -1204,6 +1204,7 @@ export type CustomerOrdersArgs = {
 };
 
 export type CustomerFilterParameter = {
+  postalCode?: Maybe<StringOperators>;
   id?: Maybe<IdOperators>;
   createdAt?: Maybe<DateOperators>;
   updatedAt?: Maybe<DateOperators>;

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

@@ -1184,6 +1184,7 @@ export type CustomerOrdersArgs = {
 };
 
 export type CustomerFilterParameter = {
+    postalCode?: Maybe<StringOperators>;
     id?: Maybe<IdOperators>;
     createdAt?: Maybe<DateOperators>;
     updatedAt?: Maybe<DateOperators>;

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

@@ -1196,6 +1196,7 @@ export type CustomerOrdersArgs = {
 };
 
 export type CustomerFilterParameter = {
+  postalCode?: Maybe<StringOperators>;
   id?: Maybe<IdOperators>;
   createdAt?: Maybe<DateOperators>;
   updatedAt?: Maybe<DateOperators>;

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

@@ -112,6 +112,24 @@ describe('Customer resolver', () => {
         thirdCustomer = result.customers.items[2];
     });
 
+    it('customers list filter by postalCode', async () => {
+        const result = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
+            GET_CUSTOMER_LIST,
+            {
+                options: {
+                    filter: {
+                        postalCode: {
+                            eq: 'NU9 0PW',
+                        },
+                    },
+                },
+            },
+        );
+
+        expect(result.customers.items.length).toBe(1);
+        expect(result.customers.items[0].emailAddress).toBe('eliezer56@yahoo.com');
+    });
+
     it('customer resolver resolves User', async () => {
         const { customer } = await adminClient.query<
             GetCustomerWithUser.Query,

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

@@ -1184,6 +1184,7 @@ export type CustomerOrdersArgs = {
 };
 
 export type CustomerFilterParameter = {
+    postalCode?: Maybe<StringOperators>;
     id?: Maybe<IdOperators>;
     createdAt?: Maybe<DateOperators>;
     updatedAt?: Maybe<DateOperators>;

+ 4 - 0
packages/core/src/api/schema/admin-api/customer.api.graphql

@@ -36,6 +36,10 @@ input UpdateCustomerInput {
     emailAddress: String
 }
 
+input CustomerFilterParameter {
+    postalCode: StringOperators
+}
+
 # generated by generateListOptions function
 input CustomerListOptions
 

+ 18 - 1
packages/core/src/service/helpers/list-query-builder/list-query-builder.ts

@@ -52,11 +52,28 @@ export type ExtendedListQueryOptions<T extends VendureEntity> = {
      * not a column in the Order table, it exists on the Customer entity, and Order has a relation to Customer via
      * `Order.customer`. Therefore we can define a customPropertyMap like this:
      *
+     * ```GraphQL
+     * """
+     * Manually extend the filter & sort inputs to include the new
+     * field that we want to be able to use in building list queries.
+     * """
+     * input OrderFilterParameter {
+     *     customerLastName: StringOperators
+     * }
+     *
+     * input OrderSortParameter {
+     *     customerLastName: SortOrder
+     * }
+     * ```
+     *
      * ```ts
      * const qb = this.listQueryBuilder.build(Order, options, {
      *   relations: ['customer'],
      *   customPropertyMap: {
-     *       customerLastName: 'customer.lastName',
+     *     // Tell TypeORM how to map that custom
+     *     // sort/filter field to the property on a
+     *     // related entity
+     *     customerLastName: 'customer.lastName',
      *   },
      * };
      * ```

+ 10 - 1
packages/core/src/service/services/customer.service.ts

@@ -10,6 +10,7 @@ import {
     CreateAddressInput,
     CreateCustomerInput,
     CreateCustomerResult,
+    CustomerListOptions,
     DeletionResponse,
     DeletionResult,
     HistoryEntryType,
@@ -88,12 +89,20 @@ export class CustomerService {
         ctx: RequestContext,
         options: ListQueryOptions<Customer> | undefined,
     ): Promise<PaginatedList<Customer>> {
+        const relations = ['channels'];
+        const customPropertyMap: { [name: string]: string } = {};
+        const hasPostalCodeFilter = !!(options as CustomerListOptions)?.filter?.postalCode;
+        if (hasPostalCodeFilter) {
+            relations.push('addresses');
+            customPropertyMap.postalCode = 'address.postalCode';
+        }
         return this.listQueryBuilder
             .build(Customer, options, {
-                relations: ['channels'],
+                relations,
                 channelId: ctx.channelId,
                 where: { deletedAt: null },
                 ctx,
+                customPropertyMap,
             })
             .getManyAndCount()
             .then(([items, totalItems]) => ({ items, totalItems }));

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

@@ -1184,6 +1184,7 @@ export type CustomerOrdersArgs = {
 };
 
 export type CustomerFilterParameter = {
+    postalCode?: Maybe<StringOperators>;
     id?: Maybe<IdOperators>;
     createdAt?: Maybe<DateOperators>;
     updatedAt?: Maybe<DateOperators>;

+ 1 - 0
packages/payments-plugin/e2e/graphql/generated-admin-types.ts

@@ -1184,6 +1184,7 @@ export type CustomerOrdersArgs = {
 };
 
 export type CustomerFilterParameter = {
+    postalCode?: Maybe<StringOperators>;
     id?: Maybe<IdOperators>;
     createdAt?: Maybe<DateOperators>;
     updatedAt?: Maybe<DateOperators>;

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
schema-admin.json


Некоторые файлы не были показаны из-за большого количества измененных файлов