From 2f0521cff7485aa8340556af559d7abaf79f151d Mon Sep 17 00:00:00 2001 From: Marc Schipperheyn Date: Tue, 16 Jul 2024 04:08:10 -0300 Subject: [PATCH] fix(core): Copy tax category on product duplication (#2947) --- .../core/e2e/duplicate-entity.e2e-spec.ts | 47 ++++++++++++++++++- .../entity-duplicators/product-duplicator.ts | 3 +- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/core/e2e/duplicate-entity.e2e-spec.ts b/packages/core/e2e/duplicate-entity.e2e-spec.ts index 084649f6b7..bb7c00bf30 100644 --- a/packages/core/e2e/duplicate-entity.e2e-spec.ts +++ b/packages/core/e2e/duplicate-entity.e2e-spec.ts @@ -556,7 +556,6 @@ describe('Duplicating entities', () => { duplicateEntityGuard.assertSuccess(duplicateEntity); const variant = product?.variants.find(v => v.sku.startsWith(originalFirstVariant.sku)); - // console.log('pv1', duplicateEntity.newEntityId, product!.variants, variant) expect(variant).not.toBeUndefined(); expect(originalFirstVariant.price).toBeGreaterThan(0); expect(variant!.price).toBe(originalFirstVariant.price); @@ -676,6 +675,52 @@ describe('Duplicating entities', () => { expect(productVariantChannel2!.price).toEqual(productVariant.price + 150); }); + + it('tax categories are duplicated', async () => { + // update existing variant with a non 1 first tax category + // bc tax category defaults to the first available + const { product } = await adminClient.query< + Codegen.GetProductWithVariantsQuery, + Codegen.GetProductWithVariantsQueryVariables + >(GET_PRODUCT_WITH_VARIANTS, { + id: 'T_1', + }); + const { updateProductVariants } = await adminClient.query< + Codegen.UpdateProductVariantsMutation, + Codegen.UpdateProductVariantsMutationVariables + >(UPDATE_PRODUCT_VARIANTS, { + input: [{ id: product!.variants[0].id, taxCategoryId: 'T_2' }], + }); + const { duplicateEntity } = await adminClient.query< + Codegen.DuplicateEntityMutation, + Codegen.DuplicateEntityMutationVariables + >(DUPLICATE_ENTITY, { + input: { + entityName: 'Product', + entityId: 'T_1', + duplicatorInput: { + code: 'product-duplicator', + arguments: [ + { + name: 'includeVariants', + value: 'true', + }, + ], + }, + }, + }); + const { product: productReloaded } = await adminClient.query< + Codegen.GetProductWithVariantsQuery, + Codegen.GetProductWithVariantsQueryVariables + >(GET_PRODUCT_WITH_VARIANTS, { + id: duplicateEntity.newEntityId, + }); + const variant = productReloaded?.variants.find(v => + v.sku.startsWith(product!.variants[0].sku), + ); + expect(variant).not.toBeUndefined(); + expect(variant!.taxCategory.id).toEqual('T_2'); + }); }); describe('Collection duplicator', () => { diff --git a/packages/core/src/config/entity/entity-duplicators/product-duplicator.ts b/packages/core/src/config/entity/entity-duplicators/product-duplicator.ts index 22d753bf3c..05dd197b47 100644 --- a/packages/core/src/config/entity/entity-duplicators/product-duplicator.ts +++ b/packages/core/src/config/entity/entity-duplicators/product-duplicator.ts @@ -104,6 +104,7 @@ export const productDuplicator = new EntityDuplicator({ stockLevels: true, facetValues: true, productVariantPrices: true, + taxCategory: true, }, }); if (product.optionGroups && product.optionGroups.length) { @@ -167,13 +168,13 @@ export const productDuplicator = new EntityDuplicator({ const price = variant.productVariantPrices.find(p => idsAreEqual(p.channelId, ctx.channelId))?.price ?? variant.productVariantPrices[0]?.price; - return { productId: duplicatedProduct.id, price: price ?? variant.price, sku: `${variant.sku}-copy`, stockOnHand: 1, featuredAssetId: variant.featuredAsset?.id, + taxCategoryId: variant.taxCategory?.id, useGlobalOutOfStockThreshold: variant.useGlobalOutOfStockThreshold, trackInventory: variant.trackInventory, assetIds: variant.assets.map(value => value.assetId),