fix(medusa): Support fields param in list-variants (#5053)
* fix(medusa): Support fields param in list-variants * Create cuddly-pigs-tease.md * address pr comments
This commit is contained in:
1 parent
edf90eecb4
commit
dc94f053d3
4 files changed
+60
-40
No files matched your search
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(medusa): Support `fields` param in list-variants
|
||||||
@@ -379,5 +379,38 @@ describe("/store/variants", () => {
|
|||||||
],
|
],
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should list variants with id using fields param", async () => {
|
||||||
|
const api = useApi()
|
||||||
|
|
||||||
|
const response = await api
|
||||||
|
.get("/store/variants?fields=id&expand=&limit=1")
|
||||||
|
.catch((err) => console.log(err))
|
||||||
|
|
||||||
|
expect(response.data).toEqual({
|
||||||
|
variants: [
|
||||||
|
{
|
||||||
|
id: expect.any(String),
|
||||||
|
created_at: expect.any(String),
|
||||||
|
// tax rates, prices, and calculated prices are added regardless of fields and expand
|
||||||
|
calculated_price: null,
|
||||||
|
calculated_price_incl_tax: null,
|
||||||
|
calculated_tax: null,
|
||||||
|
original_price: null,
|
||||||
|
original_price_incl_tax: null,
|
||||||
|
original_tax: null,
|
||||||
|
tax_rates: null,
|
||||||
|
prices: expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
id: expect.any(String),
|
||||||
|
variant_id: expect.any(String),
|
||||||
|
created_at: expect.any(String),
|
||||||
|
updated_at: expect.any(String),
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import middlewares, { transformStoreQuery } from "../../../middlewares"
|
import middlewares, { transformStoreQuery } from "../../../middlewares"
|
||||||
|
|
||||||
import { PricedVariant } from "../../../../types/pricing"
|
|
||||||
import { Router } from "express"
|
import { Router } from "express"
|
||||||
import { StoreGetVariantsParams } from "./list-variants"
|
import { PricedVariant } from "../../../../types/pricing"
|
||||||
import { StoreGetVariantsVariantParams } from "./get-variant"
|
|
||||||
import { extendRequestParams } from "../../../middlewares/publishable-api-key/extend-request-params"
|
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 { validateSalesChannelParam } from "../../../middlewares/publishable-api-key/validate-sales-channel-param"
|
||||||
|
import { validateProductVariantSalesChannelAssociation } from "../../../middlewares/publishable-api-key/validate-variant-sales-channel-association"
|
||||||
import { withDefaultSalesChannel } from "../../../middlewares/with-default-sales-channel"
|
import { withDefaultSalesChannel } from "../../../middlewares/with-default-sales-channel"
|
||||||
|
import { StoreGetVariantsVariantParams } from "./get-variant"
|
||||||
|
import { StoreGetVariantsParams } from "./list-variants"
|
||||||
|
|
||||||
const route = Router()
|
const route = Router()
|
||||||
|
|
||||||
@@ -91,5 +91,6 @@ export type StoreVariantsListRes = {
|
|||||||
variants: PricedVariant[]
|
variants: PricedVariant[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export * from "./list-variants"
|
|
||||||
export * from "./get-variant"
|
export * from "./get-variant"
|
||||||
|
export * from "./list-variants"
|
||||||
|
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { IsInt, IsOptional, IsString } from "class-validator"
|
||||||
import {
|
import {
|
||||||
CartService,
|
CartService,
|
||||||
PricingService,
|
PricingService,
|
||||||
@@ -5,17 +6,12 @@ import {
|
|||||||
ProductVariantService,
|
ProductVariantService,
|
||||||
RegionService,
|
RegionService,
|
||||||
} from "../../../../services"
|
} from "../../../../services"
|
||||||
import { IsInt, IsOptional, IsString } from "class-validator"
|
|
||||||
|
|
||||||
import { FilterableProductVariantProps } from "../../../../types/product-variant"
|
import { Type } from "class-transformer"
|
||||||
import { IsType } from "../../../../utils/validators/is-type"
|
|
||||||
import { MedusaError } from "@medusajs/utils"
|
|
||||||
import { NumericalComparisonOperator } from "../../../../types/common"
|
import { NumericalComparisonOperator } from "../../../../types/common"
|
||||||
import { PriceSelectionParams } from "../../../../types/price-selection"
|
import { PriceSelectionParams } from "../../../../types/price-selection"
|
||||||
import { Type } from "class-transformer"
|
|
||||||
import { defaultStoreVariantRelations } from "."
|
|
||||||
import { omit } from "lodash"
|
|
||||||
import { validator } from "../../../../utils/validator"
|
import { validator } from "../../../../utils/validator"
|
||||||
|
import { IsType } from "../../../../utils/validators/is-type"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [get] /store/variants
|
* @oas [get] /store/variants
|
||||||
@@ -130,40 +126,22 @@ import { validator } from "../../../../utils/validator"
|
|||||||
*/
|
*/
|
||||||
export default async (req, res) => {
|
export default async (req, res) => {
|
||||||
const validated = await validator(StoreGetVariantsParams, req.query)
|
const validated = await validator(StoreGetVariantsParams, req.query)
|
||||||
const { expand, offset, limit } = validated
|
|
||||||
|
|
||||||
let expandFields: string[] = []
|
|
||||||
if (expand) {
|
|
||||||
expandFields = expand.split(",")
|
|
||||||
}
|
|
||||||
|
|
||||||
const customer_id = req.user?.customer_id
|
const customer_id = req.user?.customer_id
|
||||||
|
|
||||||
const listConfig = {
|
let {
|
||||||
relations: expandFields.length
|
cart_id,
|
||||||
? expandFields
|
region_id,
|
||||||
: defaultStoreVariantRelations,
|
currency_code,
|
||||||
skip: offset,
|
sales_channel_id,
|
||||||
take: limit,
|
ids,
|
||||||
}
|
...filterableFields
|
||||||
|
} = req.filterableFields
|
||||||
const filterableFields: FilterableProductVariantProps = omit(validated, [
|
|
||||||
"ids",
|
|
||||||
"limit",
|
|
||||||
"offset",
|
|
||||||
"expand",
|
|
||||||
"cart_id",
|
|
||||||
"region_id",
|
|
||||||
"currency_code",
|
|
||||||
"sales_channel_id",
|
|
||||||
])
|
|
||||||
|
|
||||||
if (validated.ids) {
|
if (validated.ids) {
|
||||||
filterableFields.id = validated.ids.split(",")
|
filterableFields["id"] = validated.ids.split(",")
|
||||||
}
|
}
|
||||||
|
|
||||||
let sales_channel_id = validated.sales_channel_id
|
|
||||||
|
|
||||||
if (req.publishableApiKeyScopes?.sales_channel_ids.length === 1) {
|
if (req.publishableApiKeyScopes?.sales_channel_ids.length === 1) {
|
||||||
sales_channel_id = req.publishableApiKeyScopes.sales_channel_ids[0]
|
sales_channel_id = req.publishableApiKeyScopes.sales_channel_ids[0]
|
||||||
}
|
}
|
||||||
@@ -177,7 +155,10 @@ export default async (req, res) => {
|
|||||||
req.scope.resolve("productVariantInventoryService")
|
req.scope.resolve("productVariantInventoryService")
|
||||||
const regionService: RegionService = req.scope.resolve("regionService")
|
const regionService: RegionService = req.scope.resolve("regionService")
|
||||||
|
|
||||||
const rawVariants = await variantService.list(filterableFields, listConfig)
|
const rawVariants = await variantService.list(
|
||||||
|
filterableFields,
|
||||||
|
req.listConfig
|
||||||
|
)
|
||||||
|
|
||||||
let regionId = validated.region_id
|
let regionId = validated.region_id
|
||||||
let currencyCode = validated.currency_code
|
let currencyCode = validated.currency_code
|
||||||
|
|||||||
Reference in new issue
Block a user