Feat/expand on list discounts (#1304)

This commit is contained in:
Kasper Fabricius Kristensen authored and GitHub committed 2022-04-21 15:22:07 +02:00
1 parent 313cb0658b
commit 7a1e394b9d
5 files changed
+87 -6

No files matched your search

@@ -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 () => { it("should retrieve discount with product conditions created with factory", async () => {
const api = useApi() const api = useApi()
@@ -2,6 +2,7 @@ import {
AdminDiscountsDeleteRes, AdminDiscountsDeleteRes,
AdminDiscountsListRes, AdminDiscountsListRes,
AdminDiscountsRes, AdminDiscountsRes,
AdminGetDiscountParams,
AdminGetDiscountsParams, AdminGetDiscountsParams,
AdminPostDiscountsDiscountDynamicCodesReq, AdminPostDiscountsDiscountDynamicCodesReq,
AdminPostDiscountsDiscountReq, AdminPostDiscountsDiscountReq,
@@ -1,6 +1,7 @@
import { import {
AdminDiscountsListRes, AdminDiscountsListRes,
AdminDiscountsRes, AdminDiscountsRes,
AdminGetDiscountParams,
AdminGetDiscountsParams, AdminGetDiscountsParams,
} from "@medusajs/medusa" } from "@medusajs/medusa"
import { Response } from "@medusajs/medusa-js" import { Response } from "@medusajs/medusa-js"
@@ -34,6 +35,7 @@ export const useAdminDiscounts = (
export const useAdminDiscount = ( export const useAdminDiscount = (
id: string, id: string,
query?: AdminGetDiscountParams,
options?: UseQueryOptionsWrapper< options?: UseQueryOptionsWrapper<
Response<AdminDiscountsRes>, Response<AdminDiscountsRes>,
Error, Error,
@@ -43,7 +45,7 @@ export const useAdminDiscount = (
const { client } = useMedusa() const { client } = useMedusa()
const { data, ...rest } = useQuery( const { data, ...rest } = useQuery(
adminDiscountKeys.detail(id), adminDiscountKeys.detail(id),
() => client.admin.discounts.retrieve(id), () => client.admin.discounts.retrieve(id, query),
options options
) )
return { ...data, ...rest } as const return { ...data, ...rest } as const
@@ -1,5 +1,9 @@
import { IsOptional, IsString } from "class-validator"
import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "." import { defaultAdminDiscountsFields, defaultAdminDiscountsRelations } from "."
import { Discount } from "../../../.."
import DiscountService from "../../../../services/discount" import DiscountService from "../../../../services/discount"
import { getRetrieveConfig } from "../../../../utils/get-query-config"
import { validator } from "../../../../utils/validator"
/** /**
* @oas [get] /discounts/{id} * @oas [get] /discounts/{id}
* operationId: "GetDiscountsDiscount" * operationId: "GetDiscountsDiscount"
@@ -8,6 +12,9 @@ import DiscountService from "../../../../services/discount"
* x-authenticated: true * x-authenticated: true
* parameters: * parameters:
* - (path) id=* {string} The id of the Discount * - (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: * tags:
* - Discount * - Discount
* responses: * responses:
@@ -23,11 +30,27 @@ import DiscountService from "../../../../services/discount"
export default async (req, res) => { export default async (req, res) => {
const { discount_id } = req.params const { discount_id } = req.params
const validated = await validator(AdminGetDiscountParams, req.query)
const config = getRetrieveConfig<Discount>(
defaultAdminDiscountsFields,
defaultAdminDiscountsRelations,
validated?.fields?.split(",") as (keyof Discount)[],
validated?.expand?.split(",")
)
const discountService: DiscountService = req.scope.resolve("discountService") const discountService: DiscountService = req.scope.resolve("discountService")
const data = await discountService.retrieve(discount_id, { const data = await discountService.retrieve(discount_id, config)
select: defaultAdminDiscountsFields,
relations: defaultAdminDiscountsRelations,
})
res.status(200).json({ discount: data }) res.status(200).json({ discount: data })
} }
export class AdminGetDiscountParams {
@IsOptional()
@IsString()
expand?: string
@IsOptional()
@IsString()
fields?: string
}
@@ -43,9 +43,12 @@ export default async (req, res) => {
const discountService: DiscountService = req.scope.resolve("discountService") const discountService: DiscountService = req.scope.resolve("discountService")
const relations =
validated.expand?.split(",") ?? defaultAdminDiscountsRelations
const listConfig: FindConfig<Discount> = { const listConfig: FindConfig<Discount> = {
select: defaultAdminDiscountsFields, select: defaultAdminDiscountsFields,
relations: defaultAdminDiscountsRelations, relations,
skip: validated.offset, skip: validated.offset,
take: validated.limit, take: validated.limit,
order: { created_at: "DESC" }, order: { created_at: "DESC" },