@@ -0,0 +1,49 @@
|
||||
import { addProductsToSalesChannelsWorkflow } from "@medusajs/core-flows"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { defaultAdminSalesChannelFields } from "../../../../query-config"
|
||||
import { AdminPostSalesChannelsChannelProductsBatchReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const body =
|
||||
req.validatedBody as AdminPostSalesChannelsChannelProductsBatchReq
|
||||
|
||||
const workflowInput = {
|
||||
data: [
|
||||
{
|
||||
sales_channel_id: req.params.id,
|
||||
product_ids: body.product_ids,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const { errors } = await addProductsToSalesChannelsWorkflow(req.scope).run({
|
||||
input: workflowInput,
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "sales_channels",
|
||||
variables: { id: req.params.id },
|
||||
fields: defaultAdminSalesChannelFields,
|
||||
})
|
||||
|
||||
const [sales_channel] = await remoteQuery(queryObject)
|
||||
|
||||
res.status(200).json({ sales_channel })
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminGetSalesChannelsParams,
|
||||
AdminGetSalesChannelsSalesChannelParams,
|
||||
AdminPostSalesChannelsChannelProductsBatchReq,
|
||||
AdminPostSalesChannelsReq,
|
||||
AdminPostSalesChannelsSalesChannelReq,
|
||||
} from "./validators"
|
||||
@@ -56,4 +57,9 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/admin/sales-channels/:id",
|
||||
middlewares: [],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/sales-channels/:id/products/batch/add",
|
||||
middlewares: [transformBody(AdminPostSalesChannelsChannelProductsBatchReq)],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { OperatorMap } from "@medusajs/types"
|
||||
import { Type } from "class-transformer"
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
@@ -100,3 +101,8 @@ export class AdminPostSalesChannelsSalesChannelReq {
|
||||
@IsOptional()
|
||||
metadata?: Record<string, unknown>
|
||||
}
|
||||
|
||||
export class AdminPostSalesChannelsChannelProductsBatchReq {
|
||||
@IsArray()
|
||||
product_ids: string[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user