Browse Source

fix(core): Do not list deleted productVariants in a Collection

Fixes #100
Michael Bromley 6 years ago
parent
commit
e1fecbbe50

+ 36 - 3
packages/core/e2e/collection.e2e-spec.ts

@@ -4,7 +4,10 @@ import gql from 'graphql-tag';
 import path from 'path';
 
 import { StringOperator } from '../src/common/configurable-operation';
-import { facetValueCollectionFilter, variantNameCollectionFilter } from '../src/config/collection/default-collection-filters';
+import {
+    facetValueCollectionFilter,
+    variantNameCollectionFilter,
+} from '../src/config/collection/default-collection-filters';
 
 import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
 import { COLLECTION_FRAGMENT, FACET_VALUE_FRAGMENT } from './graphql/fragments';
@@ -14,6 +17,7 @@ import {
     CreateCollection,
     CreateCollectionInput,
     CreateCollectionSelectVariants,
+    DeleteProduct,
     FacetValueFragment,
     GetAssetList,
     GetCollection,
@@ -30,7 +34,14 @@ import {
     UpdateProduct,
     UpdateProductVariants,
 } from './graphql/generated-e2e-admin-types';
-import { CREATE_COLLECTION, GET_ASSET_LIST, UPDATE_COLLECTION, UPDATE_PRODUCT, UPDATE_PRODUCT_VARIANTS } from './graphql/shared-definitions';
+import {
+    CREATE_COLLECTION,
+    DELETE_PRODUCT,
+    GET_ASSET_LIST,
+    UPDATE_COLLECTION,
+    UPDATE_PRODUCT,
+    UPDATE_PRODUCT_VARIANTS,
+} from './graphql/shared-definitions';
 import { TestAdminClient } from './test-client';
 import { TestServer } from './test-server';
 import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
@@ -73,7 +84,9 @@ describe('Collection resolver', () => {
      * Test case for https://github.com/vendure-ecommerce/vendure/issues/97
      */
     it('collection breadcrumbs works after bootstrap', async () => {
-        const result = await client.query<GetCollectionBreadcrumbs.Query>(GET_COLLECTION_BREADCRUMBS, { id: 'T_1' });
+        const result = await client.query<GetCollectionBreadcrumbs.Query>(GET_COLLECTION_BREADCRUMBS, {
+            id: 'T_1',
+        });
         expect(result.collection!.breadcrumbs[0].name).toBe(ROOT_COLLECTION_NAME);
     });
 
@@ -705,6 +718,26 @@ describe('Collection resolver', () => {
         });
     });
 
+    it('collection does not list deleted products', async () => {
+        await client.query<DeleteProduct.Mutation, DeleteProduct.Variables>(DELETE_PRODUCT, {
+            id: 'T_2', // curvy monitor
+        });
+        const { collection } = await client.query<
+            GetCollectionProducts.Query,
+            GetCollectionProducts.Variables
+        >(GET_COLLECTION_PRODUCT_VARIANTS, {
+            id: pearCollection.id,
+        });
+        expect(collection!.productVariants.items.map(i => i.name)).toEqual([
+            'Laptop 13 inch 8GB',
+            'Laptop 15 inch 8GB',
+            'Laptop 13 inch 16GB',
+            'Laptop 15 inch 16GB',
+            'Gaming PC i7-8700 240GB SSD',
+            'Instant Camera',
+        ]);
+    });
+
     function getFacetValueId(code: string): string {
         const match = facetValues.find(fv => fv.code === code);
         if (!match) {

+ 8 - 0
packages/core/e2e/graphql/shared-definitions.ts

@@ -228,3 +228,11 @@ export const GET_FACET_LIST = gql`
     }
     ${FACET_WITH_VALUES_FRAGMENT}
 `;
+
+export const DELETE_PRODUCT = gql`
+    mutation DeleteProduct($id: ID!) {
+        deleteProduct(id: $id) {
+            result
+        }
+    }
+`;

+ 1 - 8
packages/core/e2e/product.e2e-spec.ts

@@ -22,6 +22,7 @@ import {
 } from './graphql/generated-e2e-admin-types';
 import {
     CREATE_PRODUCT,
+    DELETE_PRODUCT,
     GET_ASSET_LIST,
     GET_PRODUCT_LIST,
     GET_PRODUCT_WITH_VARIANTS,
@@ -678,14 +679,6 @@ describe('Product resolver', () => {
     });
 });
 
-const DELETE_PRODUCT = gql`
-    mutation DeleteProduct($id: ID!) {
-        deleteProduct(id: $id) {
-            result
-        }
-    }
-`;
-
 export const ADD_OPTION_GROUP_TO_PRODUCT = gql`
     mutation AddOptionGroupToProduct($productId: ID!, $optionGroupId: ID!) {
         addOptionGroupToProduct(productId: $productId, optionGroupId: $optionGroupId) {

+ 3 - 2
packages/core/src/service/services/product-variant.service.ts

@@ -103,11 +103,12 @@ export class ProductVariantService {
                 channelId: ctx.channelId,
             })
             .leftJoin('productvariant.collections', 'collection')
+            .leftJoin('productvariant.product', 'product')
+            .andWhere('product.deletedAt IS NULL', { deletedAt: null })
             .andWhere('collection.id = :collectionId', { collectionId });
 
         if (options && options.filter && options.filter.enabled && options.filter.enabled.eq === true) {
-            qb.leftJoin('productvariant.product', 'product')
-                .andWhere('product.enabled = :enabled', { enabled: true });
+            qb.andWhere('product.enabled = :enabled', { enabled: true });
         }
 
         return qb.getManyAndCount()