fix: Ensure sales channel updates don't remove sales channel on other products (#7510)
* fix: Make all product tests pass * fix: Ensure product update doesnt remove sales channels on other products
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from "./steps/remove-remote-links"
|
||||
export * from "./steps/use-remote-query"
|
||||
export * from "./steps/create-remote-links"
|
||||
export * from "./steps/dismiss-remote-links"
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { LinkDefinition, RemoteLink } from "@medusajs/modules-sdk"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
|
||||
type DismissRemoteLinksStepInput = LinkDefinition | LinkDefinition[]
|
||||
|
||||
export const dismissRemoteLinkStepId = "dismiss-remote-links"
|
||||
export const dismissRemoteLinkStep = createStep(
|
||||
dismissRemoteLinkStepId,
|
||||
async (data: DismissRemoteLinksStepInput, { container }) => {
|
||||
const entries = Array.isArray(data) ? data : [data]
|
||||
const link = container.resolve<RemoteLink>(
|
||||
ContainerRegistrationKeys.REMOTE_LINK
|
||||
)
|
||||
await link.dismiss(entries)
|
||||
|
||||
return new StepResponse(entries, entries)
|
||||
},
|
||||
async (dismissdLinks, { container }) => {
|
||||
if (!dismissdLinks) {
|
||||
return
|
||||
}
|
||||
|
||||
const link = container.resolve<RemoteLink>(
|
||||
ContainerRegistrationKeys.REMOTE_LINK
|
||||
)
|
||||
await link.create(dismissdLinks)
|
||||
}
|
||||
)
|
||||
@@ -1,18 +1,18 @@
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import {
|
||||
createWorkflow,
|
||||
transform,
|
||||
WorkflowData,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { updateProductsStep } from "../steps/update-products"
|
||||
|
||||
import {
|
||||
dismissRemoteLinkStep,
|
||||
createLinkStep,
|
||||
removeRemoteLinkStep,
|
||||
useRemoteQueryStep,
|
||||
} from "../../common"
|
||||
import { arrayDifference } from "@medusajs/utils"
|
||||
import { DeleteEntityInput, Modules } from "@medusajs/modules-sdk"
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ProductTypes } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
|
||||
type UpdateProductsStepInputSelector = {
|
||||
selector: ProductTypes.FilterableProductProps
|
||||
@@ -63,15 +63,16 @@ function updateProductIds({
|
||||
updatedProducts: ProductTypes.ProductDTO[]
|
||||
input: WorkflowInput
|
||||
}) {
|
||||
let productIds = updatedProducts.map((p) => p.id)
|
||||
|
||||
if ("products" in input) {
|
||||
let productIds = updatedProducts.map((p) => p.id)
|
||||
const discardedProductIds: string[] = input.products
|
||||
.filter((p) => !p.sales_channels)
|
||||
.map((p) => p.id as string)
|
||||
return arrayDifference(productIds, discardedProductIds)
|
||||
}
|
||||
|
||||
return !input.update.sales_channels ? [] : undefined
|
||||
return !input.update.sales_channels ? [] : productIds
|
||||
}
|
||||
|
||||
function prepareSalesChannelLinks({
|
||||
@@ -148,12 +149,12 @@ export const updateProductsWorkflow = createWorkflow(
|
||||
const currentLinks = useRemoteQueryStep({
|
||||
entry_point: "product_sales_channel",
|
||||
fields: ["product_id", "sales_channel_id"],
|
||||
variables: { product_id: updatedProductIds },
|
||||
variables: { filters: { product_id: updatedProductIds } },
|
||||
})
|
||||
|
||||
const toDeleteLinks = transform({ currentLinks }, prepareToDeleteLinks)
|
||||
|
||||
removeRemoteLinkStep(toDeleteLinks as DeleteEntityInput[])
|
||||
dismissRemoteLinkStep(toDeleteLinks)
|
||||
|
||||
const salesChannelLinks = transform(
|
||||
{ input, updatedProducts },
|
||||
|
||||
Reference in New Issue
Block a user