Skip to content

Commit

Permalink
fix: Don't remove pricing if no variant is passed to update (#8416)
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski authored Aug 2, 2024
1 parent 8bde271 commit 4b0119f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,21 @@ medusaIntegrationTestRunner({
barcode: "test-barcode-4",
allow_backorder: false,
manage_inventory: true,
prices: [],
prices: [
expect.objectContaining({
currency_code: "usd",
amount: 100,
}),

expect.objectContaining({
currency_code: "eur",
amount: 45,
}),
expect.objectContaining({
currency_code: "dkk",
amount: 30,
}),
],
options: [
expect.objectContaining({
value: "Large",
Expand Down
77 changes: 77 additions & 0 deletions integration-tests/http/__tests__/product/admin/product.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,83 @@ medusaIntegrationTestRunner({
expect(response.data.product.images.length).toEqual(0)
})

it("updating the product without variants keeps the variants and prices intact", async () => {
const payload = {
title: "Test an update",
}

await api
.post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})

const updatedProduct = (
await api.get(`/admin/products/${baseProduct.id}`, adminHeaders)
).data.product

expect(updatedProduct.variants).toEqual([
expect.objectContaining({
id: baseProduct.variants[0].id,
prices: expect.arrayContaining([
expect.objectContaining({
currency_code: "usd",
amount: 100,
}),
expect.objectContaining({
currency_code: "eur",
amount: 45,
}),
expect.objectContaining({
currency_code: "dkk",
amount: 30,
}),
]),
}),
])
})

it("updating the product variants without prices keeps the prices intact", async () => {
const payload = {
title: "Test an update",
variants: baseProduct.variants.map((variant) => ({
id: variant.id,
title: variant.id,
})),
}

await api
.post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})

const updatedProduct = (
await api.get(`/admin/products/${baseProduct.id}`, adminHeaders)
).data.product

expect(updatedProduct.variants).toEqual([
expect.objectContaining({
id: baseProduct.variants[0].id,
title: baseProduct.variants[0].id,
prices: expect.arrayContaining([
expect.objectContaining({
currency_code: "usd",
amount: 100,
}),
expect.objectContaining({
currency_code: "eur",
amount: 45,
}),
expect.objectContaining({
currency_code: "dkk",
amount: 30,
}),
]),
}),
])
})

it("updates a product by deleting a field from metadata", async () => {
const created = (
await api.post(
Expand Down

This file was deleted.

34 changes: 31 additions & 3 deletions packages/core/core-flows/src/product/workflows/update-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
useRemoteQueryStep,
} from "../../common"
import { upsertVariantPricesWorkflow } from "./upsert-variant-prices"
import { getVariantIdsForProductsStep } from "../steps/get-variant-ids-for-products"

type UpdateProductsStepInputSelector = {
selector: ProductTypes.FilterableProductProps
Expand Down Expand Up @@ -214,7 +213,36 @@ export const updateProductsWorkflowId = "update-products"
export const updateProductsWorkflow = createWorkflow(
updateProductsWorkflowId,
(input: WorkflowData<WorkflowInput>) => {
const previousVariantIds = getVariantIdsForProductsStep(input)
// We only get the variant ids of products that are updating the variants and prices.
const variantIdsSelector = transform({ input }, (data) => {
if ("products" in data.input) {
return {
filters: {
id: data.input.products
.filter((p) => !!p.variants)
.map((p) => p.id),
},
}
}

return {
filters: data.input.update?.variants ? data.input.selector : { id: [] },
}
})
const previousProductsWithVariants = useRemoteQueryStep({
entry_point: "product",
fields: ["variants.id"],
variables: variantIdsSelector,
}).config({ name: "get-previous-products-variants-step" })

const previousVariantIds = transform(
{ previousProductsWithVariants },
(data) => {
return data.previousProductsWithVariants.flatMap((p) =>
p.variants?.map((v) => v.id)
)
}
)

const toUpdateInput = transform({ input }, prepareUpdateProductInput)
const updatedProducts = updateProductsStep(toUpdateInput)
Expand All @@ -237,7 +265,7 @@ export const updateProductsWorkflow = createWorkflow(
entry_point: "product_sales_channel",
fields: ["product_id", "sales_channel_id"],
variables: { filters: { product_id: updatedProductIds } },
})
}).config({ name: "get-current-sales-channel-links-step" })

const toDeleteSalesChannelLinks = transform(
{ currentSalesChannelLinks },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const upsertVariantPricesWorkflow = createWorkflow(
.map((v) => {
const priceSetId = linksMap.get(v.variant_id)

if (!priceSetId) {
if (!priceSetId || !v.prices) {
return
}

Expand Down

0 comments on commit 4b0119f

Please sign in to comment.