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:
@@ -9,13 +9,14 @@ import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminPostApiKeysApiKeySalesChannelsBatchReq } from "../../../../validators"
|
||||
import { AdminPostApiKeysApiKeySalesChannelsBatchAddReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const body = req.validatedBody as AdminPostApiKeysApiKeySalesChannelsBatchReq
|
||||
const body =
|
||||
req.validatedBody as AdminPostApiKeysApiKeySalesChannelsBatchAddReq
|
||||
|
||||
const apiKeyModule = req.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { removeSalesChannelsFromApiKeyWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminPostApiKeysApiKeySalesChannelsBatchRemoveReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const body =
|
||||
req.validatedBody as AdminPostApiKeysApiKeySalesChannelsBatchRemoveReq
|
||||
|
||||
const apiKeyModule = req.scope.resolve(ModuleRegistrationName.API_KEY)
|
||||
|
||||
const apiKey = await apiKeyModule.retrieve(req.params.id)
|
||||
|
||||
if (apiKey.type !== "publishable") {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_DATA,
|
||||
"Sales channels can only be associated with publishable API keys"
|
||||
)
|
||||
}
|
||||
|
||||
const workflowInput = {
|
||||
data: [
|
||||
{
|
||||
api_key_id: req.params.id,
|
||||
sales_channel_ids: body.sales_channel_ids,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const { errors } = await removeSalesChannelsFromApiKeyWorkflow(req.scope).run(
|
||||
{
|
||||
input: workflowInput,
|
||||
throwOnError: false,
|
||||
}
|
||||
)
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const query = remoteQueryObjectFromString({
|
||||
entryPoint: "api_key",
|
||||
fields: req.remoteQueryConfig.fields,
|
||||
variables: {
|
||||
id: req.params.id,
|
||||
},
|
||||
})
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const [result] = await remoteQuery(query)
|
||||
|
||||
res.status(200).json({ api_key: result })
|
||||
}
|
||||
@@ -5,7 +5,8 @@ import {
|
||||
AdminGetApiKeysApiKeyParams,
|
||||
AdminGetApiKeysParams,
|
||||
AdminPostApiKeysApiKeyReq,
|
||||
AdminPostApiKeysApiKeySalesChannelsBatchReq,
|
||||
AdminPostApiKeysApiKeySalesChannelsBatchAddReq,
|
||||
AdminPostApiKeysApiKeySalesChannelsBatchRemoveReq,
|
||||
AdminPostApiKeysReq,
|
||||
AdminRevokeApiKeysApiKeyReq,
|
||||
} from "./validators"
|
||||
@@ -84,7 +85,18 @@ export const adminApiKeyRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
AdminGetApiKeysApiKeyParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
transformBody(AdminPostApiKeysApiKeySalesChannelsBatchReq),
|
||||
transformBody(AdminPostApiKeysApiKeySalesChannelsBatchAddReq),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/api-keys/:id/sales-channels/batch/remove",
|
||||
middlewares: [
|
||||
transformQuery(
|
||||
AdminGetApiKeysApiKeyParams,
|
||||
QueryConfig.retrieveTransformQueryConfig
|
||||
),
|
||||
transformBody(AdminPostApiKeysApiKeySalesChannelsBatchRemoveReq),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -79,7 +79,10 @@ export class AdminRevokeApiKeysApiKeyReq {
|
||||
|
||||
export class AdminDeleteApiKeysApiKeyReq {}
|
||||
|
||||
export class AdminPostApiKeysApiKeySalesChannelsBatchReq {
|
||||
export class AdminPostApiKeysApiKeySalesChannelsBatchAddReq {
|
||||
@IsArray()
|
||||
sales_channel_ids: string[]
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-len
|
||||
export class AdminPostApiKeysApiKeySalesChannelsBatchRemoveReq extends AdminPostApiKeysApiKeySalesChannelsBatchAddReq {}
|
||||
|
||||
Reference in New Issue
Block a user