Feat(link-module, core-flows, medusa): Add sales channel location management (#6905)

* add locations with sales channels

* init, not working location based management

* working adding sales channel stock locations

* remote sales channels from location

* remove console log

* rm allowedFields

* redo errorhandler

* make validation take an array

* add changeset

* fix build

* Update packages/core-flows/src/sales-channel/workflows/remove-locations-from-sales-channel.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* add default cases to return early

* cleanup

* add sc test and remove associations

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Philip KorsholmandOli Juhl authored and GitHub committed 2024-04-03 08:38:53 +02:00
1 parent 27f4f0d724
commit edafe7db47
19 files changed
+483 -43

No files matched your search

@@ -0,0 +1,52 @@
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
import { Modules } from "@medusajs/modules-sdk"
interface StepInput {
links: {
sales_channel_id: string
location_ids: string[]
}[]
}
export const associateLocationsWithChannelStepId =
"associate-locations-with-channel-step"
export const associateLocationsWithChannelStep = createStep(
associateLocationsWithChannelStepId,
async (data: StepInput, { container }) => {
if (!data.links.length) {
return new StepResponse([], [])
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const links = data.links
.map((link) => {
return link.location_ids.map((id) => {
return {
[Modules.SALES_CHANNEL]: {
sales_channel_id: link.sales_channel_id,
},
[Modules.STOCK_LOCATION]: {
stock_location_id: id,
},
}
})
})
.flat()
const createdLinks = await remoteLink.create(links)
return new StepResponse(createdLinks, links)
},
async (links, { container }) => {
if (!links?.length) {
return
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.dismiss(links)
}
)
@@ -4,4 +4,5 @@ export * from "./create-sales-channels"
export * from "./delete-sales-channels"
export * from "./detach-products-from-sales-channels"
export * from "./update-sales-channels"
export * from "./associate-locations-with-channel"
export * from "./remove-locations-from-channels"
@@ -0,0 +1,71 @@
import { Modules, RemoteLink } from "@medusajs/modules-sdk"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
export const removeLocationsFromSalesChannelStepId =
"remove-locations-from-sales-channel"
export const removeLocationsFromSalesChannelStep = createStep(
removeLocationsFromSalesChannelStepId,
async (
data: {
sales_channel_id: string
location_ids: string[]
}[],
{ container }
) => {
if (!data.length) {
return new StepResponse([], [])
}
const remoteLink = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
const linkModule = remoteLink.getLinkModule(
Modules.SALES_CHANNEL,
"sales_channel_id",
Modules.STOCK_LOCATION,
"stock_location_id"
)
if (!linkModule) {
return new StepResponse([], [])
}
const links = data
.map((d) =>
d.location_ids.map((locId) => ({
sales_channel_id: d.sales_channel_id,
stock_location_id: locId,
}))
)
.flat()
await linkModule.softDelete(links)
return new StepResponse(void 0, links)
},
async (links, { container }) => {
if (!links?.length) {
return
}
const remoteLink = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
const linkModule = remoteLink.getLinkModule(
Modules.SALES_CHANNEL,
"sales_channel_id",
Modules.STOCK_LOCATION,
"stock_location_id"
)
if (!linkModule) {
return
}
await linkModule.restore(links)
}
)
@@ -0,0 +1,20 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { SalesChannelDTO } from "@medusajs/types"
import { associateLocationsWithChannelStep } from "../steps"
interface WorkflowInput {
data: {
sales_channel_id: string
location_ids: string[]
}[]
}
export const addLocationsToSalesChannelWorkflowId =
"add-locations-to-sales-channel"
export const addLocationsToSalesChannelWorkflow = createWorkflow(
addLocationsToSalesChannelWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<SalesChannelDTO[]> => {
return associateLocationsWithChannelStep({ links: input.data })
}
)
@@ -1,5 +1,8 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { Modules } from "@medusajs/modules-sdk"
import { deleteSalesChannelsStep } from "../steps/delete-sales-channels"
import { removeRemoteLinkStep } from "../../common/steps/remove-remote-links"
type WorkflowInput = { ids: string[] }
@@ -7,6 +10,10 @@ export const deleteSalesChannelsWorkflowId = "delete-sales-channels"
export const deleteSalesChannelsWorkflow = createWorkflow(
deleteSalesChannelsWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteSalesChannelsStep(input.ids)
deleteSalesChannelsStep(input.ids)
removeRemoteLinkStep({
[Modules.SALES_CHANNEL]: { sales_channel_id: input.ids },
})
}
)
@@ -3,4 +3,5 @@ export * from "./create-sales-channels"
export * from "./delete-sales-channels"
export * from "./remove-products-from-sales-channels"
export * from "./update-sales-channels"
export * from "./add-locations-to-sales-channel"
export * from "./remove-locations-from-sales-channel"
@@ -0,0 +1,19 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { removeLocationsFromSalesChannelStep } from "../steps"
interface WorkflowInput {
data: {
sales_channel_id: string
location_ids: string[]
}[]
}
export const removeLocationsFromSalesChannelWorkflowId =
"remove-locations-from-sales-channel"
export const removeLocationsFromSalesChannelWorkflow = createWorkflow(
removeLocationsFromSalesChannelWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
removeLocationsFromSalesChannelStep(input.data)
}
)