Browse Source

fix(core): Correctly update search index on ProductVariant deletion

Relates to #266
Michael Bromley 5 years ago
parent
commit
401c236276

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

@@ -609,6 +609,29 @@ describe('Default search plugin', () => {
                 expect(search2.items[0].productAsset!.focalPoint).toEqual({ x: 0.42, y: 0.42 });
             });
 
+            it('does not include deleted ProductVariants in index', async () => {
+                const { search: s1 } = await doAdminSearchQuery({
+                    term: 'hard drive',
+                    groupByProduct: false,
+                });
+
+                const { deleteProductVariant } = await adminClient.query<
+                    DeleteProductVariant.Mutation,
+                    DeleteProductVariant.Variables
+                >(DELETE_PRODUCT_VARIANT, { id: s1.items[0].productVariantId });
+
+                await awaitRunningJobs(adminClient);
+
+                const { search } = await adminClient.query<SearchGetPrices.Query, SearchGetPrices.Variables>(
+                    SEARCH_GET_PRICES,
+                    { input: { term: 'hard drive', groupByProduct: true } },
+                );
+                expect(search.items[0].price).toEqual({
+                    min: 7896,
+                    max: 13435,
+                });
+            });
+
             it('returns enabled field when not grouped', async () => {
                 const result = await doAdminSearchQuery({ groupByProduct: false, take: 3 });
                 expect(result.search.items.map(pick(['productVariantId', 'enabled']))).toEqual([

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

@@ -115,6 +115,7 @@ export class IndexerController {
                     const batchIds = ids.slice(begin, end);
                     const batch = await this.connection.getRepository(ProductVariant).findByIds(batchIds, {
                         relations: variantRelations,
+                        where: { deletedAt: null },
                     });
                     const variants = this.hydrateVariants(ctx, batch);
                     await this.saveVariants(ctx.languageCode, ctx.channelId, variants);
@@ -221,6 +222,7 @@ export class IndexerController {
                 .getRepository(ProductVariant)
                 .findByIds(product.variants.map(v => v.id), {
                     relations: variantRelations,
+                    where: { deletedAt: null },
                 });
             if (product.enabled === false) {
                 updatedVariants.forEach(v => (v.enabled = false));
@@ -241,6 +243,7 @@ export class IndexerController {
     ): Promise<boolean> {
         const variants = await this.connection.getRepository(ProductVariant).findByIds(variantIds, {
             relations: variantRelations,
+            where: { deletedAt: null },
         });
         if (variants) {
             const updatedVariants = this.hydrateVariants(ctx, variants);
@@ -276,7 +279,8 @@ export class IndexerController {
         qb.leftJoin('variants.product', 'product')
             .leftJoin('product.channels', 'channel')
             .where('channel.id = :channelId', { channelId })
-            .andWhere('variants__product.deletedAt IS NULL');
+            .andWhere('variants__product.deletedAt IS NULL')
+            .andWhere('variants.deletedAt IS NULL');
         return qb;
     }