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:
@@ -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)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user