fix(core-flows): shipping profile update (#11322)
**What** - prevent unsetting shipping profile on product update
This commit is contained in:
@@ -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 () => {
|
it("updates a product (update prices, tags, update status, delete collection, delete type, replaces images)", async () => {
|
||||||
const payload = {
|
const payload = {
|
||||||
collection_id: null,
|
collection_id: null,
|
||||||
|
|||||||
+2
-4
@@ -19,10 +19,6 @@ export const ProductShippingProfileSection = ({
|
|||||||
|
|
||||||
const shippingProfile = product.shipping_profile
|
const shippingProfile = product.shipping_profile
|
||||||
|
|
||||||
if (!shippingProfile) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container className="p-0">
|
<Container className="p-0">
|
||||||
<div className="flex items-center justify-between px-6 py-4">
|
<div className="flex items-center justify-between px-6 py-4">
|
||||||
@@ -42,12 +38,14 @@ export const ProductShippingProfileSection = ({
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{shippingProfile && (
|
||||||
<SidebarLink
|
<SidebarLink
|
||||||
to={`/settings/locations/shipping-profiles/${shippingProfile.id}`}
|
to={`/settings/locations/shipping-profiles/${shippingProfile.id}`}
|
||||||
labelKey={shippingProfile.name}
|
labelKey={shippingProfile.name}
|
||||||
descriptionKey={shippingProfile.type}
|
descriptionKey={shippingProfile.type}
|
||||||
icon={<ShoppingBag />}
|
icon={<ShoppingBag />}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
</Container>
|
</Container>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,6 +141,25 @@ function findProductsWithSalesChannels({
|
|||||||
return !input.update?.sales_channels ? [] : productIds
|
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({
|
function prepareSalesChannelLinks({
|
||||||
input,
|
input,
|
||||||
updatedProducts,
|
updatedProducts,
|
||||||
@@ -417,10 +436,6 @@ export const updateProductsWorkflow = createWorkflow(
|
|||||||
const toUpdateInput = transform({ input }, prepareUpdateProductInput)
|
const toUpdateInput = transform({ input }, prepareUpdateProductInput)
|
||||||
const updatedProducts = updateProductsStep(toUpdateInput)
|
const updatedProducts = updateProductsStep(toUpdateInput)
|
||||||
|
|
||||||
const updatedPorductIds = transform({ updatedProducts }, (data) => {
|
|
||||||
return data.updatedProducts.map((p) => p.id)
|
|
||||||
})
|
|
||||||
|
|
||||||
const salesChannelLinks = transform(
|
const salesChannelLinks = transform(
|
||||||
{ input, updatedProducts },
|
{ input, updatedProducts },
|
||||||
prepareSalesChannelLinks
|
prepareSalesChannelLinks
|
||||||
@@ -441,6 +456,11 @@ export const updateProductsWorkflow = createWorkflow(
|
|||||||
findProductsWithSalesChannels
|
findProductsWithSalesChannels
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const productsWithShippingProfiles = transform(
|
||||||
|
{ updatedProducts, input },
|
||||||
|
findProductsWithShippingProfiles
|
||||||
|
)
|
||||||
|
|
||||||
const currentSalesChannelLinks = useRemoteQueryStep({
|
const currentSalesChannelLinks = useRemoteQueryStep({
|
||||||
entry_point: "product_sales_channel",
|
entry_point: "product_sales_channel",
|
||||||
fields: ["product_id", "sales_channel_id"],
|
fields: ["product_id", "sales_channel_id"],
|
||||||
@@ -450,7 +470,7 @@ export const updateProductsWorkflow = createWorkflow(
|
|||||||
const currentShippingProfileLinks = useRemoteQueryStep({
|
const currentShippingProfileLinks = useRemoteQueryStep({
|
||||||
entry_point: "product_shipping_profile",
|
entry_point: "product_shipping_profile",
|
||||||
fields: ["product_id", "shipping_profile_id"],
|
fields: ["product_id", "shipping_profile_id"],
|
||||||
variables: { filters: { product_id: updatedPorductIds } },
|
variables: { filters: { product_id: productsWithShippingProfiles } },
|
||||||
}).config({ name: "get-current-shipping-profile-links-step" })
|
}).config({ name: "get-current-shipping-profile-links-step" })
|
||||||
|
|
||||||
const toDeleteSalesChannelLinks = transform(
|
const toDeleteSalesChannelLinks = transform(
|
||||||
|
|||||||
Reference in New Issue
Block a user