Skip to content

Commit

Permalink
fix(medusa): Support fields param in list-variants (#5053)
Browse files Browse the repository at this point in the history
* fix(medusa): Support fields param in list-variants

* Create cuddly-pigs-tease.md

* address pr comments
  • Loading branch information
olivermrbl authored Sep 15, 2023
1 parent edf90ee commit dc94f05
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-pigs-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

fix(medusa): Support `fields` param in list-variants
33 changes: 33 additions & 0 deletions integration-tests/api/__tests__/store/product-variants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}),
]),
},
],
})
})
})
})
11 changes: 6 additions & 5 deletions packages/medusa/src/api/routes/store/variants/index.ts
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -91,5 +91,6 @@ export type StoreVariantsListRes = {
variants: PricedVariant[]
}

export * from "./list-variants"
export * from "./get-variant"
export * from "./list-variants"

51 changes: 16 additions & 35 deletions packages/medusa/src/api/routes/store/variants/list-variants.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { IsInt, IsOptional, IsString } from "class-validator"
import {
CartService,
PricingService,
ProductVariantInventoryService,
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
Expand Down Expand Up @@ -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]
}
Expand All @@ -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
Expand Down

0 comments on commit dc94f05

Please sign in to comment.