Browse Source

fix(core): Correctly reindex enabled state

Closes #295
Michael Bromley 5 years ago
parent
commit
2231505d32

+ 22 - 0
packages/core/e2e/default-search-plugin.e2e-spec.ts

@@ -22,6 +22,7 @@ import {
     DeleteProduct,
     DeleteProductVariant,
     LanguageCode,
+    Reindex,
     RemoveProductsFromChannel,
     SearchFacetValues,
     SearchGetAssets,
@@ -701,6 +702,19 @@ describe('Default search plugin', () => {
                     { productId: 'T_3', enabled: false },
                 ]);
             });
+
+            // https://github.com/vendure-ecommerce/vendure/issues/295
+            it('enabled status survives reindex', async () => {
+                await adminClient.query<Reindex.Mutation>(REINDEX);
+
+                await awaitRunningJobs(adminClient);
+                const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
+                expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
+                    { productId: 'T_1', enabled: false },
+                    { productId: 'T_2', enabled: true },
+                    { productId: 'T_3', enabled: false },
+                ]);
+            });
         });
 
         describe('channel handling', () => {
@@ -761,6 +775,14 @@ describe('Default search plugin', () => {
     });
 });
 
+export const REINDEX = gql`
+    mutation Reindex {
+        reindex {
+            id
+        }
+    }
+`;
+
 export const SEARCH_PRODUCTS = gql`
     query SearchProductsAdmin($input: SearchInput!) {
         search(input: $input) {

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

@@ -3855,6 +3855,12 @@ export type DeleteCustomerMutation = { __typename?: 'Mutation' } & {
     deleteCustomer: { __typename?: 'DeletionResponse' } & Pick<DeletionResponse, 'result'>;
 };
 
+export type ReindexMutationVariables = {};
+
+export type ReindexMutation = { __typename?: 'Mutation' } & {
+    reindex: { __typename?: 'Job' } & Pick<Job, 'id'>;
+};
+
 export type SearchProductsAdminQueryVariables = {
     input: SearchInput;
 };
@@ -5635,6 +5641,12 @@ export namespace DeleteCustomer {
     export type DeleteCustomer = DeleteCustomerMutation['deleteCustomer'];
 }
 
+export namespace Reindex {
+    export type Variables = ReindexMutationVariables;
+    export type Mutation = ReindexMutation;
+    export type Reindex = ReindexMutation['reindex'];
+}
+
 export namespace SearchProductsAdmin {
     export type Variables = SearchProductsAdminQueryVariables;
     export type Query = SearchProductsAdminQuery;

+ 1 - 1
packages/core/src/plugin/default-search-plugin/indexer/indexer.controller.ts

@@ -306,7 +306,7 @@ export class IndexerController {
                     channelId,
                     languageCode,
                     sku: v.sku,
-                    enabled: v.enabled,
+                    enabled: v.product.enabled === false ? false : v.enabled,
                     slug: v.product.slug,
                     price: v.price,
                     priceWithTax: v.priceWithTax,

+ 1 - 0
packages/dev-server/dev-config.ts

@@ -10,6 +10,7 @@ import {
     LogLevel,
     VendureConfig,
 } from '@vendure/core';
+import { ElasticsearchPlugin } from '@vendure/elasticsearch-plugin';
 import { defaultEmailHandlers, EmailPlugin } from '@vendure/email-plugin';
 import path from 'path';
 import { ConnectionOptions } from 'typeorm';

+ 13 - 0
packages/elasticsearch-plugin/e2e/elasticsearch-plugin.e2e-spec.ts

@@ -728,6 +728,19 @@ describe('Elasticsearch plugin', () => {
                     { productId: 'T_3', enabled: false },
                 ]);
             });
+
+            // https://github.com/vendure-ecommerce/vendure/issues/295
+            it('enabled status survives reindex', async () => {
+                await adminClient.query<Reindex.Mutation>(REINDEX);
+
+                await awaitRunningJobs(adminClient);
+                const result = await doAdminSearchQuery({ groupByProduct: true, take: 3 });
+                expect(result.search.items.map(pick(['productId', 'enabled']))).toEqual([
+                    { productId: 'T_1', enabled: false },
+                    { productId: 'T_2', enabled: true },
+                    { productId: 'T_3', enabled: false },
+                ]);
+            });
         });
 
         describe('channel handling', () => {

+ 1 - 1
packages/elasticsearch-plugin/src/indexer.controller.ts

@@ -657,7 +657,7 @@ export class ElasticsearchIndexerController implements OnModuleInit, OnModuleDes
                 [] as ID[],
             ),
             channelIds: first.product.channels.map((c) => c.id as string),
-            enabled: variants.some((v) => v.enabled),
+            enabled: variants.some((v) => v.enabled) && first.product.enabled,
         };
 
         const customMappings = Object.entries(this.options.customProductMappings);