Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(medusa): admin get product should return prices when expected #5480

Merged
merged 11 commits into from
Oct 31, 2023
8 changes: 8 additions & 0 deletions .changeset/tough-cheetahs-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@medusajs/medusa": patch
"medusa-react": patch
"@medusajs/admin-ui": patch
"@medusajs/admin": patch
---

fix(medusa): admin get product should return prices when expected
136 changes: 136 additions & 0 deletions integration-tests/api/__tests__/admin/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,142 @@ describe("/admin/products", () => {
})
})

describe("GET /admin/products/:id", () => {
const productId = "testing-get-product"

beforeEach(async () => {
await simpleProductFactory(dbConnection, {
id: productId,
variants: [
{
title: "Test variant",
prices: [
{
currency: "usd",
amount: 100,
},
],
},
],
})

await adminSeeder(dbConnection)
})

afterEach(async () => {
const db = useDb()
await db.teardown()
})

it("should get a product with default relations", async () => {
const api = useApi()

const res = await api
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
.get(`/admin/products/${productId}`, adminHeaders)
.catch((err) => {
console.log(err)
})

const keysInResponse = Object.keys(res.data.product)

expect(res.status).toEqual(200)
expect(res.data.product.id).toEqual(productId)
expect(keysInResponse).toEqual(
expect.arrayContaining([
// fields
"id",
"created_at",
"updated_at",
"deleted_at",
"title",
"subtitle",
"description",
"handle",
"is_giftcard",
"status",
"thumbnail",
"weight",
"length",
"height",
"width",
"hs_code",
"origin_country",
"mid_code",
"material",
"collection_id",
"type_id",
"discountable",
"external_id",
"metadata",

// relations
"categories",
"collection",
"images",
"options",
"profiles",
"profile",
"profile_id",
"sales_channels",
"tags",
"type",
"variants",
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
])
)

const variants = res.data.product.variants
const hasPrices = variants.some((variant) => !!variant.prices)

expect(hasPrices).toBe(true)
})

it("should get a product with prices", async () => {
const api = useApi()

const res = await api
olivermrbl marked this conversation as resolved.
Show resolved Hide resolved
.get(
`/admin/products/${productId}?expand=variants,variants.prices`,
adminHeaders
)
.catch((err) => {
console.log(err)
})

const { id, variants } = res.data.product

expect(id).toEqual(productId)
expect(variants[0].prices).toEqual(
expect.arrayContaining([
expect.objectContaining({
amount: 100,
currency_code: "usd",
}),
])
)
})

it("should get a product only with variants expanded", async () => {
const api = useApi()

const res = await api
.get(`/admin/products/${productId}?expand=variants`, adminHeaders)
.catch((err) => {
console.log(err)
})

const { id, variants } = res.data.product

expect(id).toEqual(productId)
expect(variants[0]).toEqual(
expect.objectContaining({
title: "Test variant",
})
)
// prices is one of many properties that should not be expanded
expect(variants[0].prices).toBeUndefined()
})
})

describe("POST /admin/products", () => {
beforeEach(async () => {
await productSeeder(dbConnection)
Expand Down
4 changes: 3 additions & 1 deletion packages/medusa-react/src/hooks/admin/products/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AdminGetProductParams,
AdminGetProductsParams,
AdminProductsListRes,
AdminProductsListTagsRes,
Expand Down Expand Up @@ -35,6 +36,7 @@ export const useAdminProducts = (

export const useAdminProduct = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: "breaking"

id: string,
query?: AdminGetProductParams,
options?: UseQueryOptionsWrapper<
Response<AdminProductsRes>,
Error,
Expand All @@ -44,7 +46,7 @@ export const useAdminProduct = (
const { client } = useMedusa()
const { data, ...rest } = useQuery(
adminProductKeys.detail(id),
() => client.admin.products.retrieve(id),
() => client.admin.products.retrieve(id, query),
options
)
return { ...data, ...rest } as const
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PricingService, ProductService } from "../../../../services"

import IsolateProductDomainFeatureFlag from "../../../../loaders/feature-flags/isolate-product-domain"
import { MedusaError } from "@medusajs/utils"
import IsolateProductDomainFeatureFlag from "../../../../loaders/feature-flags/isolate-product-domain"
import { PricingService, ProductService } from "../../../../services"
import { FindParams } from "../../../../types/common"
import { defaultAdminProductRemoteQueryObject } from "./index"

/**
Expand Down Expand Up @@ -114,3 +114,5 @@ async function getProductWithIsolatedProductModule(req, id, retrieveConfig) {

return product
}

export class AdminGetProductParams extends FindParams {}
6 changes: 4 additions & 2 deletions packages/medusa/src/api/routes/admin/products/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import "reflect-metadata"

import { Product, ProductTag, ProductType, ProductVariant } from "../../../.."
import { FindParams, PaginatedResponse } from "../../../../types/common"
import { PaginatedResponse } from "../../../../types/common"
import middlewares, { transformQuery } from "../../../middlewares"

import { FlagRouter } from "@medusajs/utils"
import { Router } from "express"
import { PricedProduct } from "../../../../types/pricing"
import { validateSalesChannelsExist } from "../../../middlewares/validators/sales-channel-existence"
import { AdminGetProductParams } from "./get-product"
import { AdminGetProductsParams } from "./list-products"

const route = Router()
Expand Down Expand Up @@ -76,7 +77,7 @@ export default (app, featureFlagRouter: FlagRouter) => {
)
route.get(
"/:id",
transformQuery(FindParams, {
transformQuery(AdminGetProductParams, {
defaultRelations: defaultAdminProductRelations,
defaultFields: defaultAdminProductFields,
isList: false,
Expand Down Expand Up @@ -496,3 +497,4 @@ export * from "./set-metadata"
export * from "./update-option"
export * from "./update-product"
export * from "./update-variant"