fix: Don't remove pricing if no variant is passed to update (#8416)
This commit is contained in:
@@ -689,7 +689,21 @@ medusaIntegrationTestRunner({
|
|||||||
barcode: "test-barcode-4",
|
barcode: "test-barcode-4",
|
||||||
allow_backorder: false,
|
allow_backorder: false,
|
||||||
manage_inventory: true,
|
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: [
|
options: [
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
value: "Large",
|
value: "Large",
|
||||||
|
|||||||
@@ -1634,6 +1634,83 @@ medusaIntegrationTestRunner({
|
|||||||
expect(response.data.product.images.length).toEqual(0)
|
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 () => {
|
it("updates a product by deleting a field from metadata", async () => {
|
||||||
const created = (
|
const created = (
|
||||||
await api.post(
|
await api.post(
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
|
||||||
import { UpdateProductsStepInput } from "./update-products"
|
|
||||||
import { IProductModuleService } from "@medusajs/types"
|
|
||||||
import { ModuleRegistrationName } from "@medusajs/utils"
|
|
||||||
|
|
||||||
export const getVariantIdsForProductsStepId = "get-variant-ids-for-products"
|
|
||||||
export const getVariantIdsForProductsStep = createStep(
|
|
||||||
getVariantIdsForProductsStepId,
|
|
||||||
async (data: UpdateProductsStepInput, { container }) => {
|
|
||||||
const service = container.resolve<IProductModuleService>(
|
|
||||||
ModuleRegistrationName.PRODUCT
|
|
||||||
)
|
|
||||||
let filters = {}
|
|
||||||
if ("products" in data) {
|
|
||||||
if (!data.products.length) {
|
|
||||||
return new StepResponse([])
|
|
||||||
}
|
|
||||||
|
|
||||||
filters = {
|
|
||||||
id: data.products.map((p) => p.id),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
filters = data.selector
|
|
||||||
}
|
|
||||||
|
|
||||||
const products = await service.listProducts(filters, {
|
|
||||||
select: ["variants.id"],
|
|
||||||
relations: ["variants"],
|
|
||||||
take: null,
|
|
||||||
})
|
|
||||||
|
|
||||||
return new StepResponse(
|
|
||||||
products.flatMap((p) => p.variants.map((v) => v.id))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
@@ -19,7 +19,6 @@ import {
|
|||||||
useRemoteQueryStep,
|
useRemoteQueryStep,
|
||||||
} from "../../common"
|
} from "../../common"
|
||||||
import { upsertVariantPricesWorkflow } from "./upsert-variant-prices"
|
import { upsertVariantPricesWorkflow } from "./upsert-variant-prices"
|
||||||
import { getVariantIdsForProductsStep } from "../steps/get-variant-ids-for-products"
|
|
||||||
|
|
||||||
type UpdateProductsStepInputSelector = {
|
type UpdateProductsStepInputSelector = {
|
||||||
selector: ProductTypes.FilterableProductProps
|
selector: ProductTypes.FilterableProductProps
|
||||||
@@ -214,7 +213,36 @@ export const updateProductsWorkflowId = "update-products"
|
|||||||
export const updateProductsWorkflow = createWorkflow(
|
export const updateProductsWorkflow = createWorkflow(
|
||||||
updateProductsWorkflowId,
|
updateProductsWorkflowId,
|
||||||
(input: WorkflowData<WorkflowInput>) => {
|
(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 toUpdateInput = transform({ input }, prepareUpdateProductInput)
|
||||||
const updatedProducts = updateProductsStep(toUpdateInput)
|
const updatedProducts = updateProductsStep(toUpdateInput)
|
||||||
@@ -237,7 +265,7 @@ export const updateProductsWorkflow = createWorkflow(
|
|||||||
entry_point: "product_sales_channel",
|
entry_point: "product_sales_channel",
|
||||||
fields: ["product_id", "sales_channel_id"],
|
fields: ["product_id", "sales_channel_id"],
|
||||||
variables: { filters: { product_id: updatedProductIds } },
|
variables: { filters: { product_id: updatedProductIds } },
|
||||||
})
|
}).config({ name: "get-current-sales-channel-links-step" })
|
||||||
|
|
||||||
const toDeleteSalesChannelLinks = transform(
|
const toDeleteSalesChannelLinks = transform(
|
||||||
{ currentSalesChannelLinks },
|
{ currentSalesChannelLinks },
|
||||||
|
|||||||
@@ -72,7 +72,7 @@ export const upsertVariantPricesWorkflow = createWorkflow(
|
|||||||
.map((v) => {
|
.map((v) => {
|
||||||
const priceSetId = linksMap.get(v.variant_id)
|
const priceSetId = linksMap.get(v.variant_id)
|
||||||
|
|
||||||
if (!priceSetId) {
|
if (!priceSetId || !v.prices) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user