feat: Remove sales channels from pub keys (#6876)

**What**
- Add workflow + step for detaching sales channels from pub API keys
- Tweak linking error message to be more helpful
- Add `removeRemoteLink` step to delete API key workflow
This commit is contained in:
Oli Juhl
2024-03-29 16:01:10 +01:00
committed by GitHub
parent 8fd1488938
commit 1bcb13f892
16 changed files with 351 additions and 26 deletions

View File

@@ -16,6 +16,10 @@ export const associateApiKeysWithSalesChannelsStep = createStep(
async (input: StepInput, { container }) => {
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
if (!input.links) {
return
}
const links = input.links
.map((link) => {
return link.sales_channel_ids.map((id) => {

View File

@@ -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 detachApiKeysWithSalesChannelsStepId =
"detach-sales-channels-with-api-keys"
export const detachApiKeysWithSalesChannelsStep = createStep(
detachApiKeysWithSalesChannelsStepId,
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()
await remoteLink.dismiss(links)
return new StepResponse(void 0, links)
},
async (links, { container }) => {
if (!links?.length) {
return
}
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
await remoteLink.create(links)
}
)

View File

@@ -1,6 +1,7 @@
export * from "./associate-sales-channels-with-publishable-keys"
export * from "./create-api-keys"
export * from "./delete-api-keys"
export * from "./detach-sales-channels-from-publishable-keys"
export * from "./revoke-api-keys"
export * from "./update-api-keys"
export * from "./validate-sales-channel-exists"

View File

@@ -1,4 +1,6 @@
import { Modules } from "@medusajs/modules-sdk"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { removeRemoteLinkStep } from "../../common/steps/remove-remote-links"
import { deleteApiKeysStep } from "../steps"
type WorkflowInput = { ids: string[] }
@@ -7,6 +9,11 @@ export const deleteApiKeysWorkflowId = "delete-api-keys"
export const deleteApiKeysWorkflow = createWorkflow(
deleteApiKeysWorkflowId,
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
return deleteApiKeysStep(input.ids)
deleteApiKeysStep(input.ids)
// Please note, the ids here should be publishable key IDs
removeRemoteLinkStep({
[Modules.API_KEY]: { publishable_key_id: input.ids },
})
}
)

View File

@@ -3,3 +3,4 @@ export * from "./delete-api-keys"
export * from "./update-api-keys"
export * from "./revoke-api-keys"
export * from "./add-sales-channels-to-publishable-key"
export * from "./remove-sales-channels-from-publishable-key"

View File

@@ -0,0 +1,18 @@
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { detachApiKeysWithSalesChannelsStep } from "../steps/detach-sales-channels-from-publishable-keys"
type WorkflowInput = {
data: {
api_key_id: string
sales_channel_ids: string[]
}[]
}
export const removeSalesChannelsFromApiKeyWorkflowId =
"remove-sales-channels-from-api-key"
export const removeSalesChannelsFromApiKeyWorkflow = createWorkflow(
removeSalesChannelsFromApiKeyWorkflowId,
(input: WorkflowData<WorkflowInput>) => {
detachApiKeysWithSalesChannelsStep({ links: input.data })
}
)