diff --git a/integration-tests/api/__tests__/admin/discount.js b/integration-tests/api/__tests__/admin/discount.js index e0282f8d64..a9475647a0 100644 --- a/integration-tests/api/__tests__/admin/discount.js +++ b/integration-tests/api/__tests__/admin/discount.js @@ -142,6 +142,58 @@ describe("/admin/discounts", () => { ) }) + it("should retrieve discount and only select the id field and retrieve the relation parent_discount", async () => { + const api = useApi() + + const group = await dbConnection.manager.insert(CustomerGroup, { + id: "customer-group-1", + name: "vip-customers", + }) + + await dbConnection.manager.insert(Customer, { + id: "cus_1234", + email: "oli@email.com", + groups: [group], + }) + + await simpleDiscountFactory(dbConnection, { + id: "test-discount", + code: "TEST", + rule: { + type: "percentage", + value: "10", + allocation: "total", + conditions: [ + { + type: "customer_groups", + operator: "in", + customer_groups: ["customer-group-1"], + }, + ], + }, + }) + + const response = await api + .get( + "/admin/discounts/test-discount?fields=id&expand=parent_discount", + { + headers: { + Authorization: "Bearer test_token", + }, + } + ) + .catch((err) => { + console.log(err) + }) + + const disc = response.data.discount + expect(response.status).toEqual(200) + expect(disc).toEqual({ + id: "test-discount", + parent_discount: null, + }) + }) + it("should retrieve discount with product conditions created with factory", async () => { const api = useApi() diff --git a/packages/medusa-js/src/resources/admin/discounts.ts b/packages/medusa-js/src/resources/admin/discounts.ts index 90a3657a96..b0048bc92e 100644 --- a/packages/medusa-js/src/resources/admin/discounts.ts +++ b/packages/medusa-js/src/resources/admin/discounts.ts @@ -2,6 +2,7 @@ import { AdminDiscountsDeleteRes, AdminDiscountsListRes, AdminDiscountsRes, + AdminGetDiscountParams, AdminGetDiscountsParams, AdminPostDiscountsDiscountDynamicCodesReq, AdminPostDiscountsDiscountReq, diff --git a/packages/medusa-react/src/hooks/admin/discounts/queries.ts b/packages/medusa-react/src/hooks/admin/discounts/queries.ts index bb71aa0097..975d39442f 100644 --- a/packages/medusa-react/src/hooks/admin/discounts/queries.ts +++ b/packages/medusa-react/src/hooks/admin/discounts/queries.ts @@ -1,6 +1,7 @@ import { AdminDiscountsListRes, AdminDiscountsRes, + AdminGetDiscountParams, AdminGetDiscountsParams, } from "@medusajs/medusa" import { Response } from "@medusajs/medusa-js" @@ -34,6 +35,7 @@ export const useAdminDiscounts = ( export const useAdminDiscount = ( id: string, + query?: AdminGetDiscountParams, options?: UseQueryOptionsWrapper< Response, Error, @@ -43,7 +45,7 @@ export const useAdminDiscount = ( const { client } = useMedusa() const { data, ...rest } = useQuery( adminDiscountKeys.detail(id), - () => client.admin.discounts.retrieve(id), + () => client.admin.discounts.retrieve(id, query), options ) return { ...data, ...rest } as const diff --git a/packages/medusa/src/api/routes/admin/discounts/get-discount.ts b/packages/medusa/src/api/routes/admin/discounts/get-discount.ts index 804157182e..2bcca52cb6 100644 --- a/packages/medusa/src/api/routes/admin/discounts/get-discount.ts +++ b/packages/medusa/src/api/routes/admin/discounts/get-discount.ts @@ -1,5 +1,9 @@ +import { IsOptional, IsString } from "class-validator" import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "." +import { Discount } from "../../../.." import DiscountService from "../../../../services/discount" +import { getRetrieveConfig } from "../../../../utils/get-query-config" +import { validator } from "../../../../utils/validator" /** * @oas [get] /discounts/{id} * operationId: "GetDiscountsDiscount" @@ -8,6 +12,9 @@ import DiscountService from "../../../../services/discount" * x-authenticated: true * parameters: * - (path) id=* {string} The id of the Discount + * query: + * - (query) expand {string} Comma separated list of relations to include in the results. + * - (query) fields {string} Comma separated list of fields to include in the results. * tags: * - Discount * responses: @@ -23,11 +30,27 @@ import DiscountService from "../../../../services/discount" export default async (req, res) => { const { discount_id } = req.params + const validated = await validator(AdminGetDiscountParams, req.query) + + const config = getRetrieveConfig( + defaultAdminDiscountsFields, + defaultAdminDiscountsRelations, + validated?.fields?.split(",") as (keyof Discount)[], + validated?.expand?.split(",") + ) + const discountService: DiscountService = req.scope.resolve("discountService") - const data = await discountService.retrieve(discount_id, { - select: defaultAdminDiscountsFields, - relations: defaultAdminDiscountsRelations, - }) + const data = await discountService.retrieve(discount_id, config) res.status(200).json({ discount: data }) } + +export class AdminGetDiscountParams { + @IsOptional() + @IsString() + expand?: string + + @IsOptional() + @IsString() + fields?: string +} diff --git a/packages/medusa/src/api/routes/admin/discounts/list-discounts.ts b/packages/medusa/src/api/routes/admin/discounts/list-discounts.ts index 48817605f6..3c46eb8fc9 100644 --- a/packages/medusa/src/api/routes/admin/discounts/list-discounts.ts +++ b/packages/medusa/src/api/routes/admin/discounts/list-discounts.ts @@ -43,9 +43,12 @@ export default async (req, res) => { const discountService: DiscountService = req.scope.resolve("discountService") + const relations = + validated.expand?.split(",") ?? defaultAdminDiscountsRelations + const listConfig: FindConfig = { select: defaultAdminDiscountsFields, - relations: defaultAdminDiscountsRelations, + relations, skip: validated.offset, take: validated.limit, order: { created_at: "DESC" },