feat(core-flows,medusa,types,utils): add rules to promotion endpoints + workflow (#6692)
* feat(core-flows,medusa,types,utils): add rules to promotion endpoints + workflow * chore: fix specs * chore: move input type to types package
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import {
|
||||
defaultAdminPromotionFields,
|
||||
defaultAdminPromotionRelations,
|
||||
} from "../../query-config"
|
||||
import { AdminPostPromotionsPromotionRulesReq } from "../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = addRulesToPromotionsWorkflow(req.scope)
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.BUY_RULES,
|
||||
data: {
|
||||
id: req.params.id,
|
||||
...req.validatedBody,
|
||||
},
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotion = await promotionModuleService.retrieve(id, {
|
||||
select: defaultAdminPromotionFields,
|
||||
relations: defaultAdminPromotionRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
import {
|
||||
deletePromotionsWorkflow,
|
||||
updatePromotionsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../types/routing"
|
||||
|
||||
import { AdminPostPromotionsPromotionReq } from "../validators"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { UpdatePromotionDTO } from "@medusajs/types"
|
||||
import { IPromotionModuleService, UpdatePromotionDTO } from "@medusajs/types"
|
||||
import { AdminPostPromotionsPromotionReq } from "../validators"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import {
|
||||
defaultAdminPromotionFields,
|
||||
defaultAdminPromotionRelations,
|
||||
} from "../../query-config"
|
||||
import { AdminPostPromotionsPromotionRulesReq } from "../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = addRulesToPromotionsWorkflow(req.scope)
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.RULES,
|
||||
data: {
|
||||
id: req.params.id,
|
||||
...req.validatedBody,
|
||||
},
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotion = await promotionModuleService.retrieve(id, {
|
||||
select: defaultAdminPromotionFields,
|
||||
relations: defaultAdminPromotionRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPromotionModuleService } from "@medusajs/types"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../types/routing"
|
||||
import {
|
||||
defaultAdminPromotionFields,
|
||||
defaultAdminPromotionRelations,
|
||||
} from "../../query-config"
|
||||
import { AdminPostPromotionsPromotionRulesReq } from "../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = addRulesToPromotionsWorkflow(req.scope)
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.TARGET_RULES,
|
||||
data: {
|
||||
id: req.params.id,
|
||||
...req.validatedBody,
|
||||
},
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
const promotionModuleService: IPromotionModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.PROMOTION
|
||||
)
|
||||
|
||||
const promotion = await promotionModuleService.retrieve(id, {
|
||||
select: defaultAdminPromotionFields,
|
||||
relations: defaultAdminPromotionRelations,
|
||||
})
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
@@ -1,18 +1,14 @@
|
||||
import * as QueryConfig from "./query-config"
|
||||
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import {
|
||||
AdminGetPromotionsParams,
|
||||
AdminGetPromotionsPromotionParams,
|
||||
AdminPostPromotionsPromotionReq,
|
||||
AdminPostPromotionsPromotionRulesReq,
|
||||
AdminPostPromotionsReq,
|
||||
} from "./validators"
|
||||
import {
|
||||
isFeatureFlagEnabled,
|
||||
transformBody,
|
||||
transformQuery,
|
||||
} from "../../../api/middlewares"
|
||||
|
||||
import { MedusaV2Flag } from "@medusajs/utils"
|
||||
import { MiddlewareRoute } from "../../../loaders/helpers/routing/types"
|
||||
import { authenticate } from "../../../utils/authenticate-middleware"
|
||||
|
||||
@@ -51,4 +47,19 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/admin/promotions/:id",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/target-rules",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/buy-rules",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1,16 +1,43 @@
|
||||
export const defaultAdminPromotionRelations = ["campaign", "application_method"]
|
||||
export const defaultAdminPromotionRelations = [
|
||||
"campaign",
|
||||
"rules",
|
||||
"rules.values",
|
||||
"application_method",
|
||||
"application_method.buy_rules",
|
||||
"application_method.buy_rules.values",
|
||||
"application_method.target_rules",
|
||||
"application_method.target_rules.values",
|
||||
]
|
||||
export const allowedAdminPromotionRelations = [
|
||||
...defaultAdminPromotionRelations,
|
||||
]
|
||||
export const defaultAdminPromotionFields = [
|
||||
"id",
|
||||
"code",
|
||||
"campaign",
|
||||
"is_automatic",
|
||||
"type",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"deleted_at",
|
||||
"campaign.id",
|
||||
"campaign.name",
|
||||
"application_method.value",
|
||||
"application_method.type",
|
||||
"application_method.max_quantity",
|
||||
"application_method.target_type",
|
||||
"application_method.allocation",
|
||||
"application_method.created_at",
|
||||
"application_method.updated_at",
|
||||
"application_method.deleted_at",
|
||||
"application_method.buy_rules.attribute",
|
||||
"application_method.buy_rules.operator",
|
||||
"application_method.buy_rules.values.value",
|
||||
"application_method.target_rules.attribute",
|
||||
"application_method.target_rules.operator",
|
||||
"application_method.target_rules.values.value",
|
||||
"rules.attribute",
|
||||
"rules.operator",
|
||||
"rules.values.value",
|
||||
]
|
||||
|
||||
export const retrieveTransformQueryConfig = {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { CreatePromotionDTO, IPromotionModuleService } from "@medusajs/types"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../types/routing"
|
||||
import { CreatePromotionDTO, IPromotionModuleService } from "@medusajs/types"
|
||||
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { createPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
|
||||
export const GET = async (
|
||||
req: AuthenticatedMedusaRequest,
|
||||
|
||||
@@ -214,3 +214,10 @@ export class AdminPostPromotionsPromotionReq {
|
||||
@Type(() => PromotionRule)
|
||||
rules?: PromotionRule[]
|
||||
}
|
||||
|
||||
export class AdminPostPromotionsPromotionRulesReq {
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => PromotionRule)
|
||||
rules: PromotionRule[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user