@@ -0,0 +1,47 @@
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
interface StepInput {
|
||||
links: {
|
||||
sales_channel_id: string
|
||||
product_ids: string[]
|
||||
}[]
|
||||
}
|
||||
|
||||
export const associateProductsWithSalesChannelsStepId =
|
||||
"associate-products-with-channels"
|
||||
export const associateProductsWithSalesChannelsStep = createStep(
|
||||
associateProductsWithSalesChannelsStepId,
|
||||
async (input: StepInput, { container }) => {
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const links = input.links
|
||||
.map((link) => {
|
||||
return link.product_ids.map((id) => {
|
||||
return {
|
||||
[Modules.PRODUCT]: {
|
||||
product_id: id,
|
||||
},
|
||||
[Modules.SALES_CHANNEL]: {
|
||||
sales_channel_id: link.sales_channel_id,
|
||||
},
|
||||
}
|
||||
})
|
||||
})
|
||||
.flat()
|
||||
|
||||
const createdLinks = await remoteLink.create(links)
|
||||
|
||||
return new StepResponse(createdLinks, links)
|
||||
},
|
||||
async (links, { container }) => {
|
||||
if (!links) {
|
||||
return
|
||||
}
|
||||
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
await remoteLink.dismiss(links)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,5 @@
|
||||
export * from "./associate-products-with-channels"
|
||||
export * from "./create-sales-channels"
|
||||
export * from "./update-sales-channels"
|
||||
export * from "./delete-sales-channels"
|
||||
export * from "./update-sales-channels"
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { SalesChannelDTO } from "@medusajs/types"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { associateProductsWithSalesChannelsStep } from "../steps/associate-products-with-channels"
|
||||
|
||||
type WorkflowInput = {
|
||||
data: {
|
||||
sales_channel_id: string
|
||||
product_ids: string[]
|
||||
}[]
|
||||
}
|
||||
|
||||
export const addProductsToSalesChannelsWorkflowId =
|
||||
"add-products-to-sales-channels"
|
||||
export const addProductsToSalesChannelsWorkflow = createWorkflow(
|
||||
addProductsToSalesChannelsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<SalesChannelDTO[]> => {
|
||||
return associateProductsWithSalesChannelsStep({ links: input.data })
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,5 @@
|
||||
export * from "./add-products-to-sales-channels"
|
||||
export * from "./create-sales-channels"
|
||||
export * from "./update-sales-channels"
|
||||
export * from "./delete-sales-channels"
|
||||
export * from "./update-sales-channels"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user