Skip to content

Commit

Permalink
fix(core): Copy tax category on product duplication (#2947)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschipperheyn authored Jul 16, 2024
1 parent 49456b5 commit 2f0521c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
47 changes: 46 additions & 1 deletion packages/core/e2e/duplicate-entity.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const productDuplicator = new EntityDuplicator({
stockLevels: true,
facetValues: true,
productVariantPrices: true,
taxCategory: true,
},
});
if (product.optionGroups && product.optionGroups.length) {
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 2f0521c

Please sign in to comment.