Browse Source

fix(core): Fix list query builder error when using entityPrefix

Closes #1569
Michael Bromley 3 years ago
parent
commit
0353a3f349

+ 57 - 0
packages/core/e2e/entity-prefix.e2e-spec.ts

@@ -0,0 +1,57 @@
+import { mergeConfig } from '@vendure/core';
+import { createTestEnvironment } from '@vendure/testing';
+import path from 'path';
+
+import { initialData } from '../../../e2e-common/e2e-initial-data';
+import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
+
+import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
+import { GetCustomerList } from './graphql/generated-e2e-admin-types';
+import { GET_CUSTOMER_LIST } from './graphql/shared-definitions';
+
+/**
+ * Tests edge-cases related to configurations with an `entityPrefix` defined in the
+ * dbConnectionOptions.
+ */
+describe('Entity prefix edge-cases', () => {
+    const { server, adminClient, shopClient } = createTestEnvironment(
+        mergeConfig(testConfig(), {
+            dbConnectionOptions: {
+                entityPrefix: 'prefix_',
+            },
+            plugins: [ListQueryPlugin],
+        }),
+    );
+
+    beforeAll(async () => {
+        await server.init({
+            initialData,
+            productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
+            customerCount: 5,
+        });
+        await adminClient.asSuperAdmin();
+    }, TEST_SETUP_TIMEOUT_MS);
+
+    afterAll(async () => {
+        await server.destroy();
+    });
+
+    // https://github.com/vendure-ecommerce/vendure/issues/1569
+    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');
+    });
+});

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

@@ -310,7 +310,7 @@ export class ListQueryBuilder implements OnApplicationBootstrap {
                 }
                 const tableNameLower = path.split('.')[0];
                 const entityMetadata = repository.manager.connection.entityMetadatas.find(
-                    em => em.tableName === tableNameLower,
+                    em => em.tableNameWithoutPrefix === tableNameLower,
                 );
                 if (entityMetadata) {
                     const relationMetadata = metadata.relations.find(r => r.type === entityMetadata.target);