feat(core-flows,medusa): added api + workflows for rule types CRUD (#6684)
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { transformQuery } from "../../../api/middlewares"
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
import * as QueryConfig from "./query-config"
|
||||
import {
|
||||
AdminDeletePricingRuleTypesRuleTypeReq,
|
||||
AdminGetPricingRuleTypesParams,
|
||||
AdminGetPricingRuleTypesRuleTypeParams,
|
||||
AdminPostPricingRuleTypesReq,
|
||||
AdminPostPricingRuleTypesRuleTypeReq,
|
||||
} from "./validators"
|
||||
|
||||
export const adminPricingRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
@@ -22,6 +25,11 @@ export const adminPricingRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/pricing/rule-types",
|
||||
middlewares: [transformBody(AdminPostPricingRuleTypesReq)],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/admin/pricing/rule-types/:id",
|
||||
@@ -32,4 +40,14 @@ export const adminPricingRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/pricing/rule-types/:id",
|
||||
middlewares: [transformBody(AdminPostPricingRuleTypesRuleTypeReq)],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/pricing/rule-types/:id",
|
||||
middlewares: [transformBody(AdminDeletePricingRuleTypesRuleTypeReq)],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,10 +1,20 @@
|
||||
import {
|
||||
deletePricingRuleTypesWorkflow,
|
||||
updatePricingRuleTypesWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import { AdminGetPricingRuleTypesRuleTypeParams } from "../../validators"
|
||||
import { cleanResponseData } from "../../../../../utils/clean-response-data"
|
||||
import { defaultAdminPricingRuleTypeFields } from "../../query-config"
|
||||
import {
|
||||
AdminDeletePricingRuleTypesRuleTypeReq,
|
||||
AdminGetPricingRuleTypesRuleTypeParams,
|
||||
AdminPostPricingRuleTypesRuleTypeReq,
|
||||
} from "../../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<AdminGetPricingRuleTypesRuleTypeParams>,
|
||||
@@ -21,3 +31,47 @@ export const GET = async (
|
||||
|
||||
res.status(200).json({ rule_type: ruleType })
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPricingRuleTypesRuleTypeReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const workflow = updatePricingRuleTypesWorkflow(req.scope)
|
||||
const { result, errors } = await workflow.run({
|
||||
input: {
|
||||
data: [{ ...req.validatedBody, id: req.params.id }],
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
rule_type: cleanResponseData(result[0], defaultAdminPricingRuleTypeFields),
|
||||
})
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest<AdminDeletePricingRuleTypesRuleTypeReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = deletePricingRuleTypesWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: { ids: [id] },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
id,
|
||||
object: "rule_type",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { createPricingRuleTypesWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import { AdminGetPricingRuleTypesParams } from "../validators"
|
||||
import { cleanResponseData } from "../../../../utils/clean-response-data"
|
||||
import { defaultAdminPricingRuleTypeFields } from "../query-config"
|
||||
import {
|
||||
AdminGetPricingRuleTypesParams,
|
||||
AdminPostPricingRuleTypesReq,
|
||||
} from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest<AdminGetPricingRuleTypesParams>,
|
||||
@@ -28,3 +34,24 @@ export const GET = async (
|
||||
limit,
|
||||
})
|
||||
}
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPricingRuleTypesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const workflow = createPricingRuleTypesWorkflow(req.scope)
|
||||
const ruleTypesData = [req.validatedBody]
|
||||
|
||||
const { result, errors } = await workflow.run({
|
||||
input: { data: ruleTypesData },
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
rule_type: cleanResponseData(result[0], defaultAdminPricingRuleTypeFields),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IsOptional, IsString } from "class-validator"
|
||||
import { IsNotEmpty, IsNumber, IsOptional, IsString } from "class-validator"
|
||||
import { FindParams, extendedFindParamsMixin } from "../../../types/common"
|
||||
|
||||
export class AdminGetPricingRuleTypesRuleTypeParams extends FindParams {}
|
||||
@@ -10,3 +10,33 @@ export class AdminGetPricingRuleTypesParams extends extendedFindParamsMixin({
|
||||
@IsOptional()
|
||||
rule_attribute?: string[]
|
||||
}
|
||||
|
||||
export class AdminPostPricingRuleTypesReq {
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsString()
|
||||
rule_attribute: string
|
||||
|
||||
@IsNotEmpty()
|
||||
@IsNumber()
|
||||
default_priority: number
|
||||
}
|
||||
|
||||
export class AdminPostPricingRuleTypesRuleTypeReq {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
name?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
rule_attribute?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
default_priority?: number
|
||||
}
|
||||
|
||||
export class AdminDeletePricingRuleTypesRuleTypeReq {}
|
||||
|
||||
Reference in New Issue
Block a user