Skip to content

Commit

Permalink
fix(core-flows): shipping profile update (medusajs#11322)
Browse files Browse the repository at this point in the history
**What**
- prevent unsetting shipping profile on product update
  • Loading branch information
fPolic authored Feb 5, 2025
1 parent 5f7ff7f commit 742babf
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 15 deletions.
57 changes: 57 additions & 0 deletions integration-tests/http/__tests__/product/admin/product.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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>
)
}
30 changes: 25 additions & 5 deletions packages/core/core-flows/src/product/workflows/update-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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"],
Expand All @@ -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(
Expand Down

0 comments on commit 742babf

Please sign in to comment.