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:
5
.changeset/cuddly-pigs-tease.md
Normal file
5
.changeset/cuddly-pigs-tease.md
Normal file
@@ -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 { PricedVariant } from "../../../../types/pricing"
|
||||
import { Router } from "express"
|
||||
import { StoreGetVariantsParams } from "./list-variants"
|
||||
import { StoreGetVariantsVariantParams } from "./get-variant"
|
||||
import { PricedVariant } from "../../../../types/pricing"
|
||||
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 { validateProductVariantSalesChannelAssociation } from "../../../middlewares/publishable-api-key/validate-variant-sales-channel-association"
|
||||
import { withDefaultSalesChannel } from "../../../middlewares/with-default-sales-channel"
|
||||
import { StoreGetVariantsVariantParams } from "./get-variant"
|
||||
import { StoreGetVariantsParams } from "./list-variants"
|
||||
|
||||
const route = Router()
|
||||
|
||||
@@ -91,5 +91,6 @@ export type StoreVariantsListRes = {
|
||||
variants: PricedVariant[]
|
||||
}
|
||||
|
||||
export * from "./list-variants"
|
||||
export * from "./get-variant"
|
||||
export * from "./list-variants"
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { IsInt, IsOptional, IsString } from "class-validator"
|
||||
import {
|
||||
CartService,
|
||||
PricingService,
|
||||
@@ -5,17 +6,12 @@ import {
|
||||
ProductVariantService,
|
||||
RegionService,
|
||||
} from "../../../../services"
|
||||
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 { Type } from "class-transformer"
|
||||
import { NumericalComparisonOperator } from "../../../../types/common"
|
||||
import { PriceSelectionParams } from "../../../../types/price-selection"
|
||||
import { Type } from "class-transformer"
|
||||
import { defaultStoreVariantRelations } from "."
|
||||
import { omit } from "lodash"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import { IsType } from "../../../../utils/validators/is-type"
|
||||
|
||||
/**
|
||||
* @oas [get] /store/variants
|
||||
@@ -130,40 +126,22 @@ import { validator } from "../../../../utils/validator"
|
||||
*/
|
||||
export default async (req, res) => {
|
||||
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 listConfig = {
|
||||
relations: expandFields.length
|
||||
? expandFields
|
||||
: defaultStoreVariantRelations,
|
||||
skip: offset,
|
||||
take: limit,
|
||||
}
|
||||
|
||||
const filterableFields: FilterableProductVariantProps = omit(validated, [
|
||||
"ids",
|
||||
"limit",
|
||||
"offset",
|
||||
"expand",
|
||||
"cart_id",
|
||||
"region_id",
|
||||
"currency_code",
|
||||
"sales_channel_id",
|
||||
])
|
||||
let {
|
||||
cart_id,
|
||||
region_id,
|
||||
currency_code,
|
||||
sales_channel_id,
|
||||
ids,
|
||||
...filterableFields
|
||||
} = req.filterableFields
|
||||
|
||||
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) {
|
||||
sales_channel_id = req.publishableApiKeyScopes.sales_channel_ids[0]
|
||||
}
|
||||
@@ -177,7 +155,10 @@ export default async (req, res) => {
|
||||
req.scope.resolve("productVariantInventoryService")
|
||||
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 currencyCode = validated.currency_code
|
||||
|
||||
Reference in New Issue
Block a user