feat(medusa): Add endpoint for retrieving a DiscountCondition (#1525)
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { IsOptional, IsString } from "class-validator"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import {
|
||||
defaultAdminDiscountConditionFields,
|
||||
defaultAdminDiscountConditionRelations,
|
||||
} from "."
|
||||
import { DiscountCondition } from "../../../../models"
|
||||
import { DiscountService } from "../../../../services"
|
||||
import DiscountConditionService from "../../../../services/discount-condition"
|
||||
import { getRetrieveConfig } from "../../../../utils/get-query-config"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
/**
|
||||
* @oas [get] /discounts/{discount_id}/conditions/{condition_id}
|
||||
* operationId: "GetDiscountsDiscountConditionsCondition"
|
||||
* summary: "Gets a DiscountCondition"
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (path) discount_id=* {string} The id of the Discount.
|
||||
* - (path) condition_id=* {string} The id of the DiscountCondition.
|
||||
* 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.
|
||||
* description: "Gets a DiscountCondition"
|
||||
|
||||
* tags:
|
||||
* - DiscountCondition
|
||||
* responses:
|
||||
* 200:
|
||||
* description: OK
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* properties:
|
||||
* discount_condition:
|
||||
* $ref: "#/components/schemas/discount_condition"
|
||||
*/
|
||||
|
||||
export default async (req, res) => {
|
||||
const { discount_id, condition_id } = req.params
|
||||
|
||||
const validatedParams = await validator(
|
||||
AdminGetDiscountsDiscountConditionsConditionParams,
|
||||
req.query
|
||||
)
|
||||
|
||||
const discountService: DiscountService = req.scope.resolve("discountService")
|
||||
|
||||
const discount = await discountService.retrieve(discount_id, {
|
||||
relations: ["rule", "rule.conditions"],
|
||||
})
|
||||
|
||||
const existsOnDiscount = discount.rule.conditions.some(
|
||||
(c) => c.id === condition_id
|
||||
)
|
||||
|
||||
if (!existsOnDiscount) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_FOUND,
|
||||
`Condition with id ${condition_id} does not belong to Discount with id ${discount_id}`
|
||||
)
|
||||
}
|
||||
|
||||
const config = getRetrieveConfig<DiscountCondition>(
|
||||
defaultAdminDiscountConditionFields,
|
||||
defaultAdminDiscountConditionRelations,
|
||||
validatedParams?.fields?.split(",") as (keyof DiscountCondition)[],
|
||||
validatedParams?.expand?.split(",")
|
||||
)
|
||||
|
||||
const conditionService: DiscountConditionService = req.scope.resolve(
|
||||
"discountConditionService"
|
||||
)
|
||||
|
||||
const discountCondition = await conditionService.retrieve(
|
||||
condition_id,
|
||||
config
|
||||
)
|
||||
|
||||
res.status(200).json({ discount_condition: discountCondition })
|
||||
}
|
||||
|
||||
export class AdminGetDiscountsDiscountConditionsConditionParams {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
expand?: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
fields?: string
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Router } from "express"
|
||||
import "reflect-metadata"
|
||||
import { Discount } from "../../../.."
|
||||
import { DiscountCondition } from "../../../../models"
|
||||
import { DeleteResponse, PaginatedResponse } from "../../../../types/common"
|
||||
import middlewares from "../../../middlewares"
|
||||
|
||||
@@ -50,6 +51,10 @@ export default (app) => {
|
||||
)
|
||||
|
||||
// Discount condition management
|
||||
route.get(
|
||||
"/:discount_id/conditions/:condition_id",
|
||||
middlewares.wrap(require("./get-condition").default)
|
||||
)
|
||||
route.post(
|
||||
"/:discount_id/conditions/:condition_id",
|
||||
middlewares.wrap(require("./update-condition").default)
|
||||
@@ -91,10 +96,19 @@ export const defaultAdminDiscountsRelations = [
|
||||
"rule.conditions",
|
||||
]
|
||||
|
||||
export const defaultAdminDiscountConditionFields: (keyof DiscountCondition)[] =
|
||||
["id", "type", "operator", "discount_rule_id", "created_at", "updated_at"]
|
||||
|
||||
export const defaultAdminDiscountConditionRelations = ["discount_rule"]
|
||||
|
||||
export type AdminDiscountsRes = {
|
||||
discount: Discount
|
||||
}
|
||||
|
||||
export type AdminDiscountConditionsRes = {
|
||||
discount_condition: DiscountCondition
|
||||
}
|
||||
|
||||
export type AdminDiscountsDeleteRes = DeleteResponse
|
||||
|
||||
export type AdminDiscountsListRes = PaginatedResponse & {
|
||||
@@ -108,6 +122,7 @@ export * from "./create-dynamic-code"
|
||||
export * from "./delete-condition"
|
||||
export * from "./delete-discount"
|
||||
export * from "./delete-dynamic-code"
|
||||
export * from "./get-condition"
|
||||
export * from "./get-discount"
|
||||
export * from "./get-discount-by-code"
|
||||
export * from "./list-discounts"
|
||||
|
||||
Reference in New Issue
Block a user