feat(core-flows): Use remote link methods to dismiss all links related to inventory item (#6737)
* use remote link methods to dismiss all links related to inventory item * use remove remote links common step
This commit is contained in:
50
packages/core-flows/src/common/steps/remove-remote-links.ts
Normal file
50
packages/core-flows/src/common/steps/remove-remote-links.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { DeleteEntityInput, RemoteLink } from "@medusajs/modules-sdk"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
|
||||
type RemoveRemoteLinksStepInput = DeleteEntityInput | DeleteEntityInput[]
|
||||
|
||||
export const removeRemoteLinkStepId = "remove-remote-links"
|
||||
export const removeRemoteLinkStep = createStep(
|
||||
removeRemoteLinkStepId,
|
||||
async (data: RemoveRemoteLinksStepInput, { container }) => {
|
||||
const entries = Array.isArray(data) ? data : [data]
|
||||
const grouped: DeleteEntityInput = {}
|
||||
|
||||
for (const entry of entries) {
|
||||
for (const moduleName of Object.keys(entry)) {
|
||||
grouped[moduleName] ??= {}
|
||||
|
||||
for (const linkableKey of Object.keys(entry[moduleName])) {
|
||||
grouped[moduleName][linkableKey] ??= []
|
||||
|
||||
const keys = Array.isArray(entry[moduleName][linkableKey])
|
||||
? entry[moduleName][linkableKey]
|
||||
: [entry[moduleName][linkableKey]]
|
||||
|
||||
grouped[moduleName][linkableKey] = (
|
||||
grouped[moduleName][linkableKey] as string[]
|
||||
).concat(keys as string[])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const link = container.resolve<RemoteLink>(
|
||||
ContainerRegistrationKeys.REMOTE_LINK
|
||||
)
|
||||
await link.delete(grouped)
|
||||
|
||||
return new StepResponse(void 0, grouped)
|
||||
},
|
||||
async (removedLinks, { container }) => {
|
||||
if (!removedLinks) {
|
||||
return
|
||||
}
|
||||
|
||||
const link = container.resolve<RemoteLink>(
|
||||
ContainerRegistrationKeys.REMOTE_LINK
|
||||
)
|
||||
await link.restore(removedLinks)
|
||||
}
|
||||
)
|
||||
@@ -1,59 +0,0 @@
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
import { ILinkModule } from "@medusajs/types"
|
||||
|
||||
export const deatachInventoryItemStepId = "deattach-inventory-items-step"
|
||||
export const deatachInventoryItemStep = createStep(
|
||||
deatachInventoryItemStepId,
|
||||
async (ids: string[], { container }) => {
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const linkModule: ILinkModule = remoteLink.getLinkModule(
|
||||
Modules.PRODUCT,
|
||||
"variant_id",
|
||||
Modules.INVENTORY,
|
||||
"inventory_item_id"
|
||||
)
|
||||
|
||||
const links = (await linkModule.list(
|
||||
{ inventory_item_id: ids },
|
||||
{ select: ["variant_id", "inventory_item_id"] }
|
||||
)) as { inventory_item_id: string; variant_id: string }[]
|
||||
|
||||
await remoteLink.dismiss(
|
||||
links.map(({ inventory_item_id, variant_id }) => ({
|
||||
[Modules.PRODUCT]: {
|
||||
variant_id,
|
||||
},
|
||||
[Modules.INVENTORY]: {
|
||||
inventory_item_id,
|
||||
},
|
||||
}))
|
||||
)
|
||||
|
||||
return new StepResponse(void 0, links)
|
||||
},
|
||||
async (input, { container }) => {
|
||||
if (!input?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const linkDefinitions = input.map(({ inventory_item_id, variant_id }) => ({
|
||||
[Modules.PRODUCT]: {
|
||||
variant_id,
|
||||
},
|
||||
[Modules.INVENTORY]: {
|
||||
inventory_item_id,
|
||||
},
|
||||
}))
|
||||
|
||||
const links = await remoteLink.create(linkDefinitions)
|
||||
}
|
||||
)
|
||||
@@ -1,5 +1,4 @@
|
||||
export * from "./delete-inventory-items"
|
||||
export * from "./deatach-inventory-items"
|
||||
export * from "./attach-inventory-items"
|
||||
export * from "./create-inventory-items"
|
||||
export * from "./validate-singular-inventory-items-for-tags"
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { deatachInventoryItemStep, deleteInventoryItemStep } from "../steps"
|
||||
|
||||
import { Modules } from "@medusajs/modules-sdk"
|
||||
import { deleteInventoryItemStep } from "../steps"
|
||||
import { removeRemoteLinkStep } from "../../common/steps/remove-remote-links"
|
||||
|
||||
export const deleteInventoryItemWorkflowId = "delete-inventory-item-workflow"
|
||||
export const deleteInventoryItemWorkflow = createWorkflow(
|
||||
@@ -7,7 +10,9 @@ export const deleteInventoryItemWorkflow = createWorkflow(
|
||||
(input: WorkflowData<string[]>): WorkflowData<string[]> => {
|
||||
deleteInventoryItemStep(input)
|
||||
|
||||
deatachInventoryItemStep(input)
|
||||
removeRemoteLinkStep({
|
||||
[Modules.INVENTORY]: { inventory_item_id: input },
|
||||
})
|
||||
return input
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user