chore(medusa): use batch/add and batch/remove endpoints for promotion rules (#6701)
This commit is contained in:
@@ -1,25 +1,19 @@
|
||||
import {
|
||||
addRulesToPromotionsWorkflow,
|
||||
removeRulesFromPromotionsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
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"
|
||||
} from "../../../../../../../types/routing"
|
||||
import {
|
||||
defaultAdminPromotionFields,
|
||||
defaultAdminPromotionRelations,
|
||||
} from "../../query-config"
|
||||
import {
|
||||
AdminDeletePromotionsPromotionRulesReq,
|
||||
AdminPostPromotionsPromotionRulesReq,
|
||||
} from "../../validators"
|
||||
} from "../../../../query-config"
|
||||
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
@@ -27,10 +21,7 @@ export const POST = async (
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.BUY_RULES,
|
||||
data: {
|
||||
id,
|
||||
...req.validatedBody,
|
||||
},
|
||||
data: { id, ...req.validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
@@ -50,33 +41,3 @@ export const POST = async (
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest<AdminDeletePromotionsPromotionRulesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.BUY_RULES,
|
||||
data: {
|
||||
id,
|
||||
...validatedBody,
|
||||
},
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminPostPromotionsPromotionRulesBatchRemoveReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchRemoveReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.BUY_RULES,
|
||||
data: { id, ...validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -1,25 +1,19 @@
|
||||
import {
|
||||
addRulesToPromotionsWorkflow,
|
||||
removeRulesFromPromotionsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
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"
|
||||
} from "../../../../../../../types/routing"
|
||||
import {
|
||||
defaultAdminPromotionFields,
|
||||
defaultAdminPromotionRelations,
|
||||
} from "../../query-config"
|
||||
import {
|
||||
AdminDeletePromotionsPromotionRulesReq,
|
||||
AdminPostPromotionsPromotionRulesReq,
|
||||
} from "../../validators"
|
||||
} from "../../../../query-config"
|
||||
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
@@ -28,10 +22,7 @@ export const POST = async (
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.RULES,
|
||||
data: {
|
||||
id,
|
||||
...req.validatedBody,
|
||||
},
|
||||
data: { id, ...req.validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
@@ -51,33 +42,3 @@ export const POST = async (
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest<AdminDeletePromotionsPromotionRulesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.RULES,
|
||||
data: {
|
||||
id,
|
||||
...validatedBody,
|
||||
},
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminPostPromotionsPromotionRulesBatchRemoveReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchRemoveReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.RULES,
|
||||
data: { id, ...validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -1,25 +1,19 @@
|
||||
import {
|
||||
addRulesToPromotionsWorkflow,
|
||||
removeRulesFromPromotionsWorkflow,
|
||||
} from "@medusajs/core-flows"
|
||||
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"
|
||||
} from "../../../../../../../types/routing"
|
||||
import {
|
||||
defaultAdminPromotionFields,
|
||||
defaultAdminPromotionRelations,
|
||||
} from "../../query-config"
|
||||
import {
|
||||
AdminDeletePromotionsPromotionRulesReq,
|
||||
AdminPostPromotionsPromotionRulesReq,
|
||||
} from "../../validators"
|
||||
} from "../../../../query-config"
|
||||
import { AdminPostPromotionsPromotionRulesBatchAddReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchAddReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
@@ -27,10 +21,7 @@ export const POST = async (
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.TARGET_RULES,
|
||||
data: {
|
||||
id,
|
||||
...req.validatedBody,
|
||||
},
|
||||
data: { id, ...req.validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
@@ -50,33 +41,3 @@ export const POST = async (
|
||||
|
||||
res.status(200).json({ promotion })
|
||||
}
|
||||
|
||||
export const DELETE = async (
|
||||
req: AuthenticatedMedusaRequest<AdminDeletePromotionsPromotionRulesReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.TARGET_RULES,
|
||||
data: {
|
||||
id,
|
||||
...validatedBody,
|
||||
},
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { removeRulesFromPromotionsWorkflow } from "@medusajs/core-flows"
|
||||
import { RuleType } from "@medusajs/utils"
|
||||
import {
|
||||
AuthenticatedMedusaRequest,
|
||||
MedusaResponse,
|
||||
} from "../../../../../../../types/routing"
|
||||
import { AdminPostPromotionsPromotionRulesBatchRemoveReq } from "../../../../validators"
|
||||
|
||||
export const POST = async (
|
||||
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesBatchRemoveReq>,
|
||||
res: MedusaResponse
|
||||
) => {
|
||||
const id = req.params.id
|
||||
const workflow = removeRulesFromPromotionsWorkflow(req.scope)
|
||||
const validatedBody = req.validatedBody
|
||||
|
||||
const { errors } = await workflow.run({
|
||||
input: {
|
||||
rule_type: RuleType.TARGET_RULES,
|
||||
data: { id, ...validatedBody },
|
||||
},
|
||||
throwOnError: false,
|
||||
})
|
||||
|
||||
if (Array.isArray(errors) && errors[0]) {
|
||||
throw errors[0].error
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
ids: validatedBody.rule_ids,
|
||||
object: "promotion-rule",
|
||||
deleted: true,
|
||||
})
|
||||
}
|
||||
@@ -2,11 +2,11 @@ import * as QueryConfig from "./query-config"
|
||||
|
||||
import { transformBody, transformQuery } from "../../../api/middlewares"
|
||||
import {
|
||||
AdminDeletePromotionsPromotionRulesReq,
|
||||
AdminGetPromotionsParams,
|
||||
AdminGetPromotionsPromotionParams,
|
||||
AdminPostPromotionsPromotionReq,
|
||||
AdminPostPromotionsPromotionRulesReq,
|
||||
AdminPostPromotionsPromotionRulesBatchAddReq,
|
||||
AdminPostPromotionsPromotionRulesBatchRemoveReq,
|
||||
AdminPostPromotionsReq,
|
||||
} from "./validators"
|
||||
|
||||
@@ -50,32 +50,38 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
||||
matcher: "/admin/promotions/:id/rules/batch/add",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/target-rules",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
||||
matcher: "/admin/promotions/:id/target-rules/batch/add",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/buy-rules",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
|
||||
matcher: "/admin/promotions/:id/buy-rules/batch/add",
|
||||
middlewares: [transformBody(AdminPostPromotionsPromotionRulesBatchAddReq)],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/promotions/:id/rules",
|
||||
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/rules/batch/remove",
|
||||
middlewares: [
|
||||
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/promotions/:id/target-rules",
|
||||
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/target-rules/batch/remove",
|
||||
middlewares: [
|
||||
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
|
||||
],
|
||||
},
|
||||
{
|
||||
method: ["DELETE"],
|
||||
matcher: "/admin/promotions/:id/buy-rules",
|
||||
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
|
||||
method: ["POST"],
|
||||
matcher: "/admin/promotions/:id/buy-rules/batch/remove",
|
||||
middlewares: [
|
||||
transformBody(AdminPostPromotionsPromotionRulesBatchRemoveReq),
|
||||
],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -216,14 +216,14 @@ export class AdminPostPromotionsPromotionReq {
|
||||
rules?: PromotionRule[]
|
||||
}
|
||||
|
||||
export class AdminPostPromotionsPromotionRulesReq {
|
||||
export class AdminPostPromotionsPromotionRulesBatchAddReq {
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => PromotionRule)
|
||||
rules: PromotionRule[]
|
||||
}
|
||||
|
||||
export class AdminDeletePromotionsPromotionRulesReq {
|
||||
export class AdminPostPromotionsPromotionRulesBatchRemoveReq {
|
||||
@ArrayNotEmpty()
|
||||
@IsString({ each: true })
|
||||
rule_ids: string[]
|
||||
|
||||
Reference in New Issue
Block a user