From 33862e2809de690bec508dee2d87e6ec0f85bf12 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 26 Oct 2023 18:31:54 +0200 Subject: [PATCH 1/6] fix(medusa): admin get product should return prices when expected --- packages/medusa/src/api/routes/admin/products/get-product.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/medusa/src/api/routes/admin/products/get-product.ts b/packages/medusa/src/api/routes/admin/products/get-product.ts index ca5db866155cb..145ca31926415 100644 --- a/packages/medusa/src/api/routes/admin/products/get-product.ts +++ b/packages/medusa/src/api/routes/admin/products/get-product.ts @@ -80,7 +80,7 @@ export default async (req, res) => { const product = rawProduct - if (!shouldSetPricing) { + if (shouldSetPricing) { await pricingService.setProductPrices([product]) } From 639ae66fa257d8271246ec852d6b76e6e12a535b Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Thu, 26 Oct 2023 20:09:16 +0200 Subject: [PATCH 2/6] fix: Support findParams in get-product --- .../api/__tests__/admin/product.js | 87 +++++++++++++++++++ .../ui/src/domain/products/edit/index.tsx | 6 +- .../src/hooks/admin/products/queries.ts | 4 +- .../api/routes/admin/products/get-product.ts | 7 +- .../src/api/routes/admin/products/index.ts | 6 +- 5 files changed, 104 insertions(+), 6 deletions(-) diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index 910e0749c6e95..615587b5511e6 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -909,6 +909,93 @@ 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", async () => { + const api = useApi() + + const res = await api + .get(`/admin/products/${productId}`, adminHeaders) + .catch((err) => { + console.log(err) + }) + + expect(res.status).toEqual(200) + expect(res.data.product.id).toEqual(productId) + }) + + it("should get a product with prices", async () => { + const api = useApi() + + const res = await api + .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) diff --git a/packages/admin-ui/ui/src/domain/products/edit/index.tsx b/packages/admin-ui/ui/src/domain/products/edit/index.tsx index 019000c7f48d7..891ebf708d80d 100644 --- a/packages/admin-ui/ui/src/domain/products/edit/index.tsx +++ b/packages/admin-ui/ui/src/domain/products/edit/index.tsx @@ -18,7 +18,11 @@ const Edit = () => { const { getWidgets } = useWidgets() - const { product, status, error } = useAdminProduct(id || "") + const { product, status, error } = useAdminProduct(id || "", { + expand: + // default relations + "variants,variants.prices,variants.options,profiles,images,options,tags,type,collection", + }) if (error) { const errorStatus = getErrorStatus(error) diff --git a/packages/medusa-react/src/hooks/admin/products/queries.ts b/packages/medusa-react/src/hooks/admin/products/queries.ts index aea540b0b5c71..448391b30bf3d 100644 --- a/packages/medusa-react/src/hooks/admin/products/queries.ts +++ b/packages/medusa-react/src/hooks/admin/products/queries.ts @@ -1,4 +1,5 @@ import { + AdminGetProductParams, AdminGetProductsParams, AdminProductsListRes, AdminProductsListTagsRes, @@ -35,6 +36,7 @@ export const useAdminProducts = ( export const useAdminProduct = ( id: string, + query?: AdminGetProductParams, options?: UseQueryOptionsWrapper< Response, Error, @@ -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 diff --git a/packages/medusa/src/api/routes/admin/products/get-product.ts b/packages/medusa/src/api/routes/admin/products/get-product.ts index 145ca31926415..cf7ed0b332097 100644 --- a/packages/medusa/src/api/routes/admin/products/get-product.ts +++ b/packages/medusa/src/api/routes/admin/products/get-product.ts @@ -1,7 +1,8 @@ -import { PricingService, ProductService } from "../../../../services" +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" -import { MedusaError } from "@medusajs/utils" /** * @oas [get] /admin/products/{id} @@ -113,3 +114,5 @@ async function getProductWithIsolatedProductModule(req, id, retrieveConfig) { return product } + +export class AdminGetProductParams extends FindParams {} diff --git a/packages/medusa/src/api/routes/admin/products/index.ts b/packages/medusa/src/api/routes/admin/products/index.ts index 1b0f4d6700e8f..11777b5991519 100644 --- a/packages/medusa/src/api/routes/admin/products/index.ts +++ b/packages/medusa/src/api/routes/admin/products/index.ts @@ -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() @@ -76,7 +77,7 @@ export default (app, featureFlagRouter: FlagRouter) => { ) route.get( "/:id", - transformQuery(FindParams, { + transformQuery(AdminGetProductParams, { defaultRelations: defaultAdminProductRelations, defaultFields: defaultAdminProductFields, isList: false, @@ -496,3 +497,4 @@ export * from "./set-metadata" export * from "./update-option" export * from "./update-product" export * from "./update-variant" + From 78238a73e40727449842207498dccdf6d6bc4092 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Fri, 27 Oct 2023 09:53:05 +0200 Subject: [PATCH 3/6] Create tough-cheetahs-hunt.md --- .changeset/tough-cheetahs-hunt.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .changeset/tough-cheetahs-hunt.md diff --git a/.changeset/tough-cheetahs-hunt.md b/.changeset/tough-cheetahs-hunt.md new file mode 100644 index 0000000000000..e81e8d38a2c46 --- /dev/null +++ b/.changeset/tough-cheetahs-hunt.md @@ -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 From df50095ed0b78e6a23c1199c96820ccbabcff154 Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Sun, 29 Oct 2023 10:29:34 +0100 Subject: [PATCH 4/6] Add default relations to test --- .../api/__tests__/admin/product.js | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index 615587b5511e6..3aec148eeba1d 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -936,7 +936,7 @@ describe("/admin/products", () => { await db.teardown() }) - it("should get a product", async () => { + it("should get a product with default relations", async () => { const api = useApi() const res = await api @@ -945,8 +945,52 @@ describe("/admin/products", () => { 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", + ]) + ) }) it("should get a product with prices", async () => { From faf5c6eb97f3e336f09e55b8673f2e12a1dccb08 Mon Sep 17 00:00:00 2001 From: olivermrbl Date: Sun, 29 Oct 2023 14:39:44 +0100 Subject: [PATCH 5/6] remove default rels from admin --- packages/admin-ui/ui/src/domain/products/edit/index.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/admin-ui/ui/src/domain/products/edit/index.tsx b/packages/admin-ui/ui/src/domain/products/edit/index.tsx index 891ebf708d80d..019000c7f48d7 100644 --- a/packages/admin-ui/ui/src/domain/products/edit/index.tsx +++ b/packages/admin-ui/ui/src/domain/products/edit/index.tsx @@ -18,11 +18,7 @@ const Edit = () => { const { getWidgets } = useWidgets() - const { product, status, error } = useAdminProduct(id || "", { - expand: - // default relations - "variants,variants.prices,variants.options,profiles,images,options,tags,type,collection", - }) + const { product, status, error } = useAdminProduct(id || "") if (error) { const errorStatus = getErrorStatus(error) From 17543a775f05a780ad0d24dfec0d4d383364037a Mon Sep 17 00:00:00 2001 From: adrien2p Date: Mon, 30 Oct 2023 13:07:47 +0100 Subject: [PATCH 6/6] check that prices exists --- integration-tests/api/__tests__/admin/product.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/integration-tests/api/__tests__/admin/product.js b/integration-tests/api/__tests__/admin/product.js index 3aec148eeba1d..b68c77ac06916 100644 --- a/integration-tests/api/__tests__/admin/product.js +++ b/integration-tests/api/__tests__/admin/product.js @@ -991,6 +991,11 @@ describe("/admin/products", () => { "variants", ]) ) + + 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 () => {