feat: API key sales channel link (#6851)

What
- Add link between API key and sales channels
- Add API route for batch adding sales channels to a publishable API key
- Clean up API key API routes responses
- Move API key test suite from `integration-tests/modules` to `integration-tests/api`
This commit is contained in:
Oli Juhl
2024-03-28 10:15:11 +00:00
committed by GitHub
parent 6ee2ee845c
commit ea8d9d4d42
21 changed files with 568 additions and 260 deletions
@@ -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: {
api_key_id: string
sales_channel_ids: string[]
}[]
}
export const associateApiKeysWithSalesChannelsStepId =
"associate-sales-channels-with-api-keys"
export const associateApiKeysWithSalesChannelsStep = createStep(
associateApiKeysWithSalesChannelsStepId,
async (input: StepInput, { container }) => {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
const links = input.links
.map((link) => {
return link.sales_channel_ids.map((id) => {
return {
[Modules.API_KEY]: {
publishable_key_id: link.api_key_id,
},
[Modules.SALES_CHANNEL]: {
sales_channel_id: 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,4 +1,6 @@
export * from "./associate-sales-channels-with-publishable-keys"
export * from "./create-api-keys"
export * from "./delete-api-keys"
export * from "./update-api-keys"
export * from "./revoke-api-keys"
export * from "./update-api-keys"
export * from "./validate-sales-channel-exists"
@@ -0,0 +1,37 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ISalesChannelModuleService } from "@medusajs/types"
import { MedusaError, arrayDifference } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
interface StepInput {
sales_channel_ids: string[]
}
export const validateSalesChannelsExistStepId = "validate-sales-channels-exist"
export const validateSalesChannelsExistStep = createStep(
validateSalesChannelsExistStepId,
async (data: StepInput, { container }) => {
const salesChannelModuleService =
container.resolve<ISalesChannelModuleService>(
ModuleRegistrationName.SALES_CHANNEL
)
const salesChannels = await salesChannelModuleService.list(
{ id: data.sales_channel_ids },
{ select: ["id"] }
)
const salesChannelIds = salesChannels.map((v) => v.id)
const notFound = arrayDifference(data.sales_channel_ids, salesChannelIds)
if (notFound.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Sales channels with IDs ${notFound.join(", ")} do not exist`
)
}
return new StepResponse(salesChannelIds)
}
)
@@ -0,0 +1,31 @@
import {
WorkflowData,
createWorkflow,
transform,
} from "@medusajs/workflows-sdk"
import {
associateApiKeysWithSalesChannelsStep,
validateSalesChannelsExistStep,
} from "../steps"
type WorkflowInput = {
data: {
api_key_id: string
sales_channel_ids: string[]
}[]
}
export const addSalesChannelsToApiKeyWorkflowId =
"add-sales-channels-to-api-key"
export const addSalesChannelsToApiKeyWorkflow = createWorkflow(
addSalesChannelsToApiKeyWorkflowId,
(input: WorkflowData<WorkflowInput>) => {
const salesChannelIds = transform(input.data, (data) =>
data.map((d) => d.sales_channel_ids).flat()
)
validateSalesChannelsExistStep({
sales_channel_ids: salesChannelIds,
})
associateApiKeysWithSalesChannelsStep({ links: input.data })
}
)
@@ -2,3 +2,4 @@ export * from "./create-api-keys"
export * from "./delete-api-keys"
export * from "./update-api-keys"
export * from "./revoke-api-keys"
export * from "./add-sales-channels-to-publishable-key"