Feat/expand on list discounts (#1304)
This commit is contained in:
committed by
GitHub
parent
313cb0658b
commit
7a1e394b9d
@@ -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()
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
AdminDiscountsDeleteRes,
|
||||
AdminDiscountsListRes,
|
||||
AdminDiscountsRes,
|
||||
AdminGetDiscountParams,
|
||||
AdminGetDiscountsParams,
|
||||
AdminPostDiscountsDiscountDynamicCodesReq,
|
||||
AdminPostDiscountsDiscountReq,
|
||||
|
||||
@@ -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<AdminDiscountsRes>,
|
||||
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
|
||||
|
||||
@@ -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<Discount>(
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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<Discount> = {
|
||||
select: defaultAdminDiscountsFields,
|
||||
relations: defaultAdminDiscountsRelations,
|
||||
relations,
|
||||
skip: validated.offset,
|
||||
take: validated.limit,
|
||||
order: { created_at: "DESC" },
|
||||
|
||||
Reference in New Issue
Block a user