feat(medusa): Middleware to add default SC on query if no SC already exist on it (#3694)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { NextFunction, Request, Response } from "express"
|
||||
import SalesChannelFeatureFlag from "../../loaders/feature-flags/sales-channels"
|
||||
import { SalesChannelService } from "../../services"
|
||||
import { FlagRouter } from "../../utils/flag-router"
|
||||
|
||||
/**
|
||||
* Middleware that includes the default sales channel on the request, if no sales channels present
|
||||
* @param context Object of options
|
||||
* @param context.attachChannelAsArray Whether to attach the default sales channel as an array or just a string
|
||||
*/
|
||||
export function withDefaultSalesChannel({
|
||||
attachChannelAsArray,
|
||||
}: {
|
||||
attachChannelAsArray?: boolean
|
||||
}): (req: Request, res: Response, next: NextFunction) => Promise<void> {
|
||||
return async (req: Request, _, next: NextFunction) => {
|
||||
const featureFlagRouter = req.scope.resolve(
|
||||
"featureFlagRouter"
|
||||
) as FlagRouter
|
||||
|
||||
if (
|
||||
!featureFlagRouter.isFeatureEnabled(SalesChannelFeatureFlag.key) ||
|
||||
req.query.sales_channel_id?.length ||
|
||||
req.get("x-publishable-api-key")
|
||||
) {
|
||||
return next()
|
||||
}
|
||||
|
||||
const salesChannelService: SalesChannelService = req.scope.resolve(
|
||||
"salesChannelService"
|
||||
)
|
||||
|
||||
try {
|
||||
const defaultSalesChannel = await salesChannelService.retrieveDefault()
|
||||
if (defaultSalesChannel?.id) {
|
||||
req.query.sales_channel_id = attachChannelAsArray
|
||||
? [defaultSalesChannel.id]
|
||||
: defaultSalesChannel.id
|
||||
}
|
||||
} catch {
|
||||
} finally {
|
||||
next()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import { StoreGetProductsProductParams } from "./get-product"
|
||||
import { extendRequestParams } from "../../../middlewares/publishable-api-key/extend-request-params"
|
||||
import { validateProductSalesChannelAssociation } from "../../../middlewares/publishable-api-key/validate-product-sales-channel-association"
|
||||
import { validateSalesChannelParam } from "../../../middlewares/publishable-api-key/validate-sales-channel-param"
|
||||
import { withDefaultSalesChannel } from "../../../middlewares/with-default-sales-channel"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -26,6 +27,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
||||
|
||||
route.get(
|
||||
"/",
|
||||
withDefaultSalesChannel({ attachChannelAsArray: true }),
|
||||
transformStoreQuery(StoreGetProductsParams, {
|
||||
defaultRelations: defaultStoreProductsRelations,
|
||||
defaultFields: defaultStoreProductsFields,
|
||||
@@ -38,6 +40,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
||||
|
||||
route.get(
|
||||
"/:id",
|
||||
withDefaultSalesChannel({}),
|
||||
transformStoreQuery(StoreGetProductsProductParams, {
|
||||
defaultRelations: defaultStoreProductsRelations,
|
||||
defaultFields: defaultStoreProductsFields,
|
||||
|
||||
Reference in New Issue
Block a user