feat: add sales channel management (#6761)

Add V2 sales channel management to admin

`@medusajs/medusa`
- Add `POST /admin/sales-channels/:id/products/batch/remove`
- Refactor cross-module filter middleware to comply with the latest convention

`@medusajs/admin-next`
- Add all sales channel routes
- Moves the following sales channel UI to shared components in `modules/sales-channel`:
  - sales-channel-list
  - sales-channel-edit
  - sales-channel-details
    - sales-channel-general-section
  - sales-channel-create

The sales-channel-product-section is not shared because the API in V2 will change.
The sales-channel-add-products component is not shared because the API in V2 will change.

`@medusajs/core-flows`
- Add `detachProductsFromSalesChannelsStep`
- Add `removeProductsFromSalesChannelsWorkflow`
This commit is contained in:
Oli Juhl
2024-04-02 15:38:33 +02:00
committed by GitHub
parent 3dee91426e
commit 7895ff3849
40 changed files with 1184 additions and 58 deletions
@@ -83,6 +83,7 @@ export const defaultAdminProductFields = [
"*options.values",
"*tags",
"*images",
"*sales_channels",
"*variants",
"*variants.prices",
"*variants.options",
@@ -18,6 +18,7 @@ export const GET = async (
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
const selectFields = remapKeysForProduct(req.remoteQueryConfig.fields ?? [])
const queryObject = remoteQueryObjectFromString({
entryPoint: "product",
variables: {
@@ -0,0 +1,51 @@
import { removeProductsFromSalesChannelsWorkflow } 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 removeProductsFromSalesChannelsWorkflow(
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 })
}
@@ -62,4 +62,9 @@ export const adminSalesChannelRoutesMiddlewares: MiddlewareRoute[] = [
matcher: "/admin/sales-channels/:id/products/batch/add",
middlewares: [transformBody(AdminPostSalesChannelsChannelProductsBatchReq)],
},
{
method: ["POST"],
matcher: "/admin/sales-channels/:id/products/batch/remove",
middlewares: [transformBody(AdminPostSalesChannelsChannelProductsBatchReq)],
},
]