feat(core-flows,modules-sdk,types,medusa,link-modules): adds variant <> inventory item link endpoints (#7576)

what:

- adds variant inventory link management endpoints:
```
Link inventory item to variant
POST /products/:id/variants/:vid/inventory-items

Update variant's inventory item link
POST /products/:id/variants/:vid/inventory-items/:iid

Unlink variant's inventory item
DELETE /products/:id/variants/:vid/inventory-items/:iid
```

- a batch endpoint that does the above 3 across variants
```
POST /products/:id/variants/inventory-items
```
This commit is contained in:
Riqwan Thamir
2024-06-03 20:23:29 +02:00
committed by GitHub
parent 122186a78d
commit ecfbfcc707
23 changed files with 1279 additions and 126 deletions

View File

@@ -1,4 +1,8 @@
export * from "./steps/remove-remote-links"
export * from "./steps/use-remote-query"
export * from "./steps/create-remote-links"
export * from "./steps/dismiss-remote-links"
export * from "./steps/remove-remote-links"
export * from "./steps/use-remote-query"
export * from "./workflows/batch-links"
export * from "./workflows/create-links"
export * from "./workflows/dismiss-links"
export * from "./workflows/update-links"

View File

@@ -1,14 +1,11 @@
import { LinkDefinition, RemoteLink } from "@medusajs/modules-sdk"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
import { ContainerRegistrationKeys } from "@medusajs/utils"
type CreateRemoteLinksStepInput = LinkDefinition[]
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const createLinksStepId = "create-remote-links"
export const createRemoteLinkStep = createStep(
createLinksStepId,
async (data: CreateRemoteLinksStepInput, { container }) => {
async (data: LinkDefinition[], { container }) => {
const link = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)

View File

@@ -5,6 +5,7 @@ import { ContainerRegistrationKeys } from "@medusajs/utils"
type DismissRemoteLinksStepInput = LinkDefinition | LinkDefinition[]
// TODO: add ability for this step to restore links from only foreign keys
export const dismissRemoteLinkStepId = "dismiss-remote-links"
export const dismissRemoteLinkStep = createStep(
dismissRemoteLinkStepId,
@@ -18,18 +19,27 @@ export const dismissRemoteLinkStep = createStep(
const link = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
// Our current revert strategy for dismissed links are to recreate it again.
// This works when its just the primary keys, but when you have additional data
// in the links, we need to preserve them in order to recreate the links accurately.
const dataBeforeDismiss = (await link.list(data, {
asLinkDefinition: true,
})) as LinkDefinition[]
await link.dismiss(entries)
return new StepResponse(entries, entries)
return new StepResponse(entries, dataBeforeDismiss)
},
async (dismissdLinks, { container }) => {
if (!dismissdLinks) {
async (dataBeforeDismiss, { container }) => {
if (!dataBeforeDismiss?.length) {
return
}
const link = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
await link.create(dismissdLinks)
await link.create(dataBeforeDismiss)
}
)

View File

@@ -0,0 +1,48 @@
import { LinkDefinition, RemoteLink } from "@medusajs/modules-sdk"
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const updateRemoteLinksStepId = "update-remote-links-step"
export const updateRemoteLinksStep = createStep(
updateRemoteLinksStepId,
async (data: LinkDefinition[], { container }) => {
if (!data.length) {
return new StepResponse([], [])
}
const link = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
// Fetch all existing links and throw an error if any weren't found
const dataBeforeUpdate = (await link.list(data, {
asLinkDefinition: true,
})) as LinkDefinition[]
const unequal = dataBeforeUpdate.length !== data.length
if (unequal) {
throw new MedusaError(
MedusaError.Types.NOT_FOUND,
`Could not find all existing links from data`
)
}
// link.create here performs an upsert. By performing validation above, we can ensure
// that this method will always perform an update in these cases
await link.create(data)
return new StepResponse(data, dataBeforeUpdate)
},
async (dataBeforeUpdate, { container }) => {
if (!dataBeforeUpdate?.length) {
return
}
const link = container.resolve<RemoteLink>(
ContainerRegistrationKeys.REMOTE_LINK
)
await link.create(dataBeforeUpdate)
}
)

View File

@@ -0,0 +1,32 @@
import { LinkDefinition } from "@medusajs/modules-sdk"
import { BatchWorkflowInput } from "@medusajs/types"
import {
WorkflowData,
createWorkflow,
parallelize,
} from "@medusajs/workflows-sdk"
import { createRemoteLinkStep } from "../steps/create-remote-links"
import { dismissRemoteLinkStep } from "../steps/dismiss-remote-links"
import { updateRemoteLinksStep } from "../steps/update-remote-links"
export const batchLinksWorkflowId = "batch-links"
export const batchLinksWorkflow = createWorkflow(
batchLinksWorkflowId,
(
input: WorkflowData<
BatchWorkflowInput<LinkDefinition, LinkDefinition, LinkDefinition>
>
) => {
const [created, updated, deleted] = parallelize(
createRemoteLinkStep(input.create || []),
updateRemoteLinksStep(input.update || []),
dismissRemoteLinkStep(input.delete || [])
)
return {
created,
updated,
deleted,
}
}
)

View File

@@ -0,0 +1,11 @@
import { LinkDefinition } from "@medusajs/modules-sdk"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createRemoteLinkStep } from "../steps/create-remote-links"
export const createLinksWorkflowId = "create-link"
export const createLinksWorkflow = createWorkflow(
createLinksWorkflowId,
(input: WorkflowData<LinkDefinition[]>) => {
return createRemoteLinkStep(input)
}
)

View File

@@ -0,0 +1,11 @@
import { LinkDefinition } from "@medusajs/modules-sdk"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { dismissRemoteLinkStep } from "../steps/dismiss-remote-links"
export const dismissLinksWorkflowId = "dismiss-link"
export const dismissLinksWorkflow = createWorkflow(
dismissLinksWorkflowId,
(input: WorkflowData<LinkDefinition[]>) => {
return dismissRemoteLinkStep(input)
}
)

View File

@@ -0,0 +1,11 @@
import { LinkDefinition } from "@medusajs/modules-sdk"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateRemoteLinksStep } from "../steps/update-remote-links"
export const updateLinksWorkflowId = "update-link"
export const updateLinksWorkflow = createWorkflow(
updateLinksWorkflowId,
(input: WorkflowData<LinkDefinition[]>) => {
return updateRemoteLinksStep(input)
}
)