fix(medusa): default sales channel for store variant endpoints (#4556)
* add changeset * include default sales channel when querying variant endpoints * make parameter optional * update default values * add integraiton tests
This commit is contained in:
1 parent
87e3a7d06a
commit
2b078f06d9
7 files changed
+86
-10
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { FlagRouter } from "@medusajs/utils"
|
||||
import { NextFunction, Request, Response } from "express"
|
||||
|
||||
import { FlagRouter } from "@medusajs/utils"
|
||||
import SalesChannelFeatureFlag from "../../loaders/feature-flags/sales-channels"
|
||||
import { SalesChannelService } from "../../services"
|
||||
|
||||
@@ -8,11 +9,13 @@ import { SalesChannelService } from "../../services"
|
||||
* @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> {
|
||||
export function withDefaultSalesChannel(
|
||||
{
|
||||
attachChannelAsArray,
|
||||
}: {
|
||||
attachChannelAsArray: boolean
|
||||
} = { attachChannelAsArray: false }
|
||||
): (req: Request, res: Response, next: NextFunction) => Promise<void> {
|
||||
return async (req: Request, _, next: NextFunction) => {
|
||||
const featureFlagRouter = req.scope.resolve(
|
||||
"featureFlagRouter"
|
||||
@@ -38,6 +41,7 @@ export function withDefaultSalesChannel({
|
||||
: defaultSalesChannel.id
|
||||
}
|
||||
} catch {
|
||||
// noop
|
||||
} finally {
|
||||
next()
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
|
||||
|
||||
route.get(
|
||||
"/:id",
|
||||
withDefaultSalesChannel({}),
|
||||
withDefaultSalesChannel(),
|
||||
transformStoreQuery(StoreGetProductsProductParams, {
|
||||
defaultRelations: defaultStoreProductsRelations,
|
||||
defaultFields: defaultStoreProductsFields,
|
||||
|
||||
@@ -7,6 +7,7 @@ import { StoreGetVariantsVariantParams } from "./get-variant"
|
||||
import { extendRequestParams } from "../../../middlewares/publishable-api-key/extend-request-params"
|
||||
import { validateProductVariantSalesChannelAssociation } from "../../../middlewares/publishable-api-key/validate-variant-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()
|
||||
|
||||
@@ -17,6 +18,7 @@ export default (app) => {
|
||||
|
||||
route.get(
|
||||
"/",
|
||||
withDefaultSalesChannel(),
|
||||
transformStoreQuery(StoreGetVariantsParams, {
|
||||
defaultRelations: defaultStoreVariantRelations,
|
||||
allowedRelations: allowedStoreVariantRelations,
|
||||
@@ -26,6 +28,7 @@ export default (app) => {
|
||||
)
|
||||
route.get(
|
||||
"/:id",
|
||||
withDefaultSalesChannel(),
|
||||
transformStoreQuery(StoreGetVariantsVariantParams, {
|
||||
defaultRelations: defaultStoreVariantRelations,
|
||||
allowedRelations: allowedStoreVariantRelations,
|
||||
|
||||
@@ -9,6 +9,7 @@ import { IsInt, IsOptional, IsString } from "class-validator"
|
||||
|
||||
import { FilterableProductVariantProps } from "../../../../types/product-variant"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { NumericalComparisonOperator } from "../../../../types/common"
|
||||
import { PriceSelectionParams } from "../../../../types/price-selection"
|
||||
import { Type } from "class-transformer"
|
||||
@@ -154,6 +155,7 @@ export default async (req, res) => {
|
||||
"cart_id",
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"sales_channel_id",
|
||||
])
|
||||
|
||||
if (validated.ids) {
|
||||
|
||||
Reference in new issue
Block a user