Răsfoiți Sursa

fix(core): Allow removal of unused ProductOptionGroup

Relates to #1134
Michael Bromley 3 ani în urmă
părinte
comite
860cce683a

+ 29 - 0
packages/core/e2e/product.e2e-spec.ts

@@ -26,6 +26,8 @@ import {
     GetProductVariantList,
     GetProductWithVariantList,
     GetProductWithVariants,
+    GetProductWithVariantsQuery,
+    GetProductWithVariantsQueryVariables,
     LanguageCode,
     ProductVariantFragment,
     ProductVariantListOptions,
@@ -1304,6 +1306,33 @@ describe('Product resolver', () => {
             expect(removeOptionGroupFromProduct.productVariantCount).toBe(2);
         });
 
+        it('removeOptionGroupFromProduct succeeds if all related ProductVariants are also deleted', async () => {
+            const { product } = await adminClient.query<
+                GetProductWithVariantsQuery,
+                GetProductWithVariantsQueryVariables
+            >(GET_PRODUCT_WITH_VARIANTS, { id: 'T_2' });
+
+            // Delete all variants for that product
+            for (const variant of product!.variants) {
+                await adminClient.query<DeleteProductVariant.Mutation, DeleteProductVariant.Variables>(
+                    DELETE_PRODUCT_VARIANT,
+                    {
+                        id: variant.id,
+                    },
+                );
+            }
+
+            const { removeOptionGroupFromProduct } = await adminClient.query<
+                RemoveOptionGroupFromProduct.Mutation,
+                RemoveOptionGroupFromProduct.Variables
+            >(REMOVE_OPTION_GROUP_FROM_PRODUCT, {
+                optionGroupId: product!.optionGroups[0].id,
+                productId: product!.id,
+            });
+
+            removeOptionGuard.assertSuccess(removeOptionGroupFromProduct);
+        });
+
         it(
             'removeOptionGroupFromProduct errors with an invalid productId',
             assertThrowsWithMessage(

+ 6 - 1
packages/core/src/service/services/product.service.ts

@@ -370,7 +370,12 @@ export class ProductService {
         if (!optionGroup) {
             throw new EntityNotFoundError('ProductOptionGroup', optionGroupId);
         }
-        if (product.variants.length) {
+        const optionIsInUse = product.variants.some(
+            variant =>
+                variant.deletedAt == null &&
+                variant.options.some(option => idsAreEqual(option.groupId, optionGroupId)),
+        );
+        if (optionIsInUse) {
             return new ProductOptionInUseError(optionGroup.code, product.variants.length);
         }
         product.optionGroups = product.optionGroups.filter(g => g.id !== optionGroupId);