fix(core-flows): variants update unsets prices (#11349)
* fix: update variant flow for prices * fix: import add test * fix: test name
This commit is contained in:
1 parent
8f384d91df
commit
17e9673769
2 files changed
+110
-7
No files matched your search
@@ -3182,6 +3182,103 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should preserve variant prices when batch updating variants", async () => {
|
||||||
|
const productWithMultipleVariants = getProductFixture({
|
||||||
|
title: "Test batch variants",
|
||||||
|
handle: "test-batch-variants",
|
||||||
|
shipping_profile_id: shippingProfile.id,
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
title: "Variant 1",
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
currency_code: "usd",
|
||||||
|
amount: 100,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: "Variant 2",
|
||||||
|
prices: [
|
||||||
|
{
|
||||||
|
currency_code: "usd",
|
||||||
|
amount: 200,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
const createdProduct = (
|
||||||
|
await api.post(
|
||||||
|
"/admin/products",
|
||||||
|
productWithMultipleVariants,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
).data.product
|
||||||
|
|
||||||
|
const variant1Id = createdProduct.variants.find(
|
||||||
|
(v) => v.title === "Variant 1"
|
||||||
|
).id
|
||||||
|
|
||||||
|
const variant2Id = createdProduct.variants.find(
|
||||||
|
(v) => v.title === "Variant 2"
|
||||||
|
).id
|
||||||
|
|
||||||
|
const updatePayload1 = {
|
||||||
|
id: variant1Id,
|
||||||
|
title: "Test batch update variant",
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatePayload2 = {
|
||||||
|
id: variant2Id,
|
||||||
|
prices: [{ currency_code: "usd", amount: 300 }],
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await api.post(
|
||||||
|
`/admin/products/${createdProduct.id}/variants/batch`,
|
||||||
|
{
|
||||||
|
update: [updatePayload1, updatePayload2],
|
||||||
|
},
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
|
||||||
|
const dbData = (
|
||||||
|
await api.get(
|
||||||
|
`/admin/products/${createdProduct.id}?fields=*variants.prices`,
|
||||||
|
adminHeaders
|
||||||
|
)
|
||||||
|
).data.product.variants
|
||||||
|
|
||||||
|
expect(response.status).toEqual(200)
|
||||||
|
expect(dbData).toHaveLength(2)
|
||||||
|
expect(dbData).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: variant1Id,
|
||||||
|
title: "Test batch update variant",
|
||||||
|
prices: expect.arrayContaining([
|
||||||
|
// updated title but price remains the same
|
||||||
|
expect.objectContaining({
|
||||||
|
currency_code: "usd",
|
||||||
|
amount: 100,
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
id: variant2Id,
|
||||||
|
prices: expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
// updated price
|
||||||
|
currency_code: "usd",
|
||||||
|
amount: 300,
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it("successfully adds and removes products to a collection", async () => {
|
it("successfully adds and removes products to a collection", async () => {
|
||||||
const response = await api.post(
|
const response = await api.post(
|
||||||
`/admin/collections/${baseCollection.id}/products`,
|
`/admin/collections/${baseCollection.id}/products`,
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ import { getVariantPricingLinkStep } from "../steps/get-variant-pricing-link"
|
|||||||
/**
|
/**
|
||||||
* The data to update one or more product variants, along with custom data that's passed to the workflow's hooks.
|
* The data to update one or more product variants, along with custom data that's passed to the workflow's hooks.
|
||||||
*/
|
*/
|
||||||
export type UpdateProductVariantsWorkflowInput =
|
export type UpdateProductVariantsWorkflowInput = (
|
||||||
(| {
|
| {
|
||||||
/**
|
/**
|
||||||
* A filter to select the product variants to update.
|
* A filter to select the product variants to update.
|
||||||
*/
|
*/
|
||||||
@@ -45,7 +45,9 @@ export type UpdateProductVariantsWorkflowInput =
|
|||||||
*/
|
*/
|
||||||
prices?: Partial<PricingTypes.CreateMoneyAmountDTO>[]
|
prices?: Partial<PricingTypes.CreateMoneyAmountDTO>[]
|
||||||
})[]
|
})[]
|
||||||
}) & AdditionalData
|
}
|
||||||
|
) &
|
||||||
|
AdditionalData
|
||||||
|
|
||||||
export const updateProductVariantsWorkflowId = "update-product-variants"
|
export const updateProductVariantsWorkflowId = "update-product-variants"
|
||||||
/**
|
/**
|
||||||
@@ -117,9 +119,7 @@ export const updateProductVariantsWorkflowId = "update-product-variants"
|
|||||||
*/
|
*/
|
||||||
export const updateProductVariantsWorkflow = createWorkflow(
|
export const updateProductVariantsWorkflow = createWorkflow(
|
||||||
updateProductVariantsWorkflowId,
|
updateProductVariantsWorkflowId,
|
||||||
(
|
(input: WorkflowData<UpdateProductVariantsWorkflowInput>) => {
|
||||||
input: WorkflowData<UpdateProductVariantsWorkflowInput>
|
|
||||||
) => {
|
|
||||||
// Passing prices to the product module will fail, we want to keep them for after the variant is updated.
|
// Passing prices to the product module will fail, we want to keep them for after the variant is updated.
|
||||||
const updateWithoutPrices = transform({ input }, (data) => {
|
const updateWithoutPrices = transform({ input }, (data) => {
|
||||||
if ("product_variants" in data.input) {
|
if ("product_variants" in data.input) {
|
||||||
@@ -147,7 +147,13 @@ export const updateProductVariantsWorkflow = createWorkflow(
|
|||||||
// We don't want to do any pricing updates if the prices didn't change
|
// We don't want to do any pricing updates if the prices didn't change
|
||||||
const variantIds = transform({ input, updatedVariants }, (data) => {
|
const variantIds = transform({ input, updatedVariants }, (data) => {
|
||||||
if ("product_variants" in data.input) {
|
if ("product_variants" in data.input) {
|
||||||
return data.updatedVariants.map((v) => v.id)
|
const variantsWithPriceUpdates = new Set(
|
||||||
|
data.input.product_variants.filter((v) => !!v.prices).map((v) => v.id)
|
||||||
|
)
|
||||||
|
|
||||||
|
return data.updatedVariants
|
||||||
|
.map((v) => v.id)
|
||||||
|
.filter((id) => variantsWithPriceUpdates.has(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.input.update.prices) {
|
if (!data.input.update.prices) {
|
||||||
|
|||||||
Reference in new issue
Block a user