fix(core-flows): shipping profile update (#11322)

**What**
- prevent unsetting shipping profile on product update
This commit is contained in:
Frane Polić
2025-02-05 15:37:43 +01:00
committed by GitHub
parent 5f7ff7f9f0
commit 742babfe3f
3 changed files with 90 additions and 15 deletions

View File

@@ -1528,6 +1528,63 @@ medusaIntegrationTestRunner({
)
})
it("updates products with shipping profiles", async () => {
const shippingProfile2 = (
await api.post(
`/admin/shipping-profiles`,
{ name: "heavy", type: "heavy" },
adminHeaders
)
).data.shipping_profile
let fetchProduct = await api.get(
`/admin/products/${baseProduct.id}?fields=+shipping_profile.id`,
adminHeaders
)
let payload: Record<string, any> = {
shipping_profile_id: shippingProfile2.id,
}
let response = await api
.post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
fetchProduct = await api.get(
`/admin/products/${baseProduct.id}?fields=+shipping_profile.id`,
adminHeaders
)
expect(fetchProduct.data.product.shipping_profile.id).toEqual(
shippingProfile2.id
)
payload = {
subtitle: "new subtitle",
}
response = await api
.post(`/admin/products/${baseProduct.id}`, payload, adminHeaders)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
fetchProduct = await api.get(
`/admin/products/${baseProduct.id}?fields=+shipping_profile.id`,
adminHeaders
)
expect(fetchProduct.data.product.shipping_profile.id).toEqual(
shippingProfile2.id
)
})
it("updates a product (update prices, tags, update status, delete collection, delete type, replaces images)", async () => {
const payload = {
collection_id: null,

View File

@@ -19,10 +19,6 @@ export const ProductShippingProfileSection = ({
const shippingProfile = product.shipping_profile
if (!shippingProfile) {
return null
}
return (
<Container className="p-0">
<div className="flex items-center justify-between px-6 py-4">
@@ -42,12 +38,14 @@ export const ProductShippingProfileSection = ({
/>
</div>
<SidebarLink
to={`/settings/locations/shipping-profiles/${shippingProfile.id}`}
labelKey={shippingProfile.name}
descriptionKey={shippingProfile.type}
icon={<ShoppingBag />}
/>
{shippingProfile && (
<SidebarLink
to={`/settings/locations/shipping-profiles/${shippingProfile.id}`}
labelKey={shippingProfile.name}
descriptionKey={shippingProfile.type}
icon={<ShoppingBag />}
/>
)}
</Container>
)
}

View File

@@ -141,6 +141,25 @@ function findProductsWithSalesChannels({
return !input.update?.sales_channels ? [] : productIds
}
function findProductsWithShippingProfiles({
updatedProducts,
input,
}: {
updatedProducts: ProductTypes.ProductDTO[]
input: UpdateProductWorkflowInput
}) {
let productIds = updatedProducts.map((p) => p.id)
if ("products" in input) {
const discardedProductIds: string[] = input.products
.filter((p) => !p.shipping_profile_id)
.map((p) => p.id as string)
return arrayDifference(productIds, discardedProductIds)
}
return !input.update?.shipping_profile_id ? [] : productIds
}
function prepareSalesChannelLinks({
input,
updatedProducts,
@@ -417,10 +436,6 @@ export const updateProductsWorkflow = createWorkflow(
const toUpdateInput = transform({ input }, prepareUpdateProductInput)
const updatedProducts = updateProductsStep(toUpdateInput)
const updatedPorductIds = transform({ updatedProducts }, (data) => {
return data.updatedProducts.map((p) => p.id)
})
const salesChannelLinks = transform(
{ input, updatedProducts },
prepareSalesChannelLinks
@@ -441,6 +456,11 @@ export const updateProductsWorkflow = createWorkflow(
findProductsWithSalesChannels
)
const productsWithShippingProfiles = transform(
{ updatedProducts, input },
findProductsWithShippingProfiles
)
const currentSalesChannelLinks = useRemoteQueryStep({
entry_point: "product_sales_channel",
fields: ["product_id", "sales_channel_id"],
@@ -450,7 +470,7 @@ export const updateProductsWorkflow = createWorkflow(
const currentShippingProfileLinks = useRemoteQueryStep({
entry_point: "product_shipping_profile",
fields: ["product_id", "shipping_profile_id"],
variables: { filters: { product_id: updatedPorductIds } },
variables: { filters: { product_id: productsWithShippingProfiles } },
}).config({ name: "get-current-shipping-profile-links-step" })
const toDeleteSalesChannelLinks = transform(