feat(core-flows,medusa,types): remove rules from promotion endpoints + workflows (#6696)

This commit is contained in:
Riqwan Thamir
2024-03-14 09:19:05 +01:00
committed by GitHub
parent 640eccd5dd
commit 04a532e5ef
16 changed files with 485 additions and 53 deletions
@@ -1,4 +1,7 @@
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import {
addRulesToPromotionsWorkflow,
removeRulesFromPromotionsWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
@@ -10,7 +13,10 @@ import {
defaultAdminPromotionFields,
defaultAdminPromotionRelations,
} from "../../query-config"
import { AdminPostPromotionsPromotionRulesReq } from "../../validators"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminPostPromotionsPromotionRulesReq,
} from "../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
@@ -22,7 +28,7 @@ export const POST = async (
input: {
rule_type: RuleType.BUY_RULES,
data: {
id: req.params.id,
id,
...req.validatedBody,
},
},
@@ -44,3 +50,33 @@ 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,
})
}
@@ -1,4 +1,7 @@
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import {
addRulesToPromotionsWorkflow,
removeRulesFromPromotionsWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
@@ -10,7 +13,10 @@ import {
defaultAdminPromotionFields,
defaultAdminPromotionRelations,
} from "../../query-config"
import { AdminPostPromotionsPromotionRulesReq } from "../../validators"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminPostPromotionsPromotionRulesReq,
} from "../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
@@ -23,7 +29,7 @@ export const POST = async (
input: {
rule_type: RuleType.RULES,
data: {
id: req.params.id,
id,
...req.validatedBody,
},
},
@@ -45,3 +51,33 @@ 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,
})
}
@@ -1,4 +1,7 @@
import { addRulesToPromotionsWorkflow } from "@medusajs/core-flows"
import {
addRulesToPromotionsWorkflow,
removeRulesFromPromotionsWorkflow,
} from "@medusajs/core-flows"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IPromotionModuleService } from "@medusajs/types"
import { RuleType } from "@medusajs/utils"
@@ -10,7 +13,10 @@ import {
defaultAdminPromotionFields,
defaultAdminPromotionRelations,
} from "../../query-config"
import { AdminPostPromotionsPromotionRulesReq } from "../../validators"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminPostPromotionsPromotionRulesReq,
} from "../../validators"
export const POST = async (
req: AuthenticatedMedusaRequest<AdminPostPromotionsPromotionRulesReq>,
@@ -22,7 +28,7 @@ export const POST = async (
input: {
rule_type: RuleType.TARGET_RULES,
data: {
id: req.params.id,
id,
...req.validatedBody,
},
},
@@ -44,3 +50,33 @@ 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,
})
}
@@ -2,6 +2,7 @@ import * as QueryConfig from "./query-config"
import { transformBody, transformQuery } from "../../../api/middlewares"
import {
AdminDeletePromotionsPromotionRulesReq,
AdminGetPromotionsParams,
AdminGetPromotionsPromotionParams,
AdminPostPromotionsPromotionReq,
@@ -62,4 +63,19 @@ export const adminPromotionRoutesMiddlewares: MiddlewareRoute[] = [
matcher: "/admin/promotions/:id/buy-rules",
middlewares: [transformBody(AdminPostPromotionsPromotionRulesReq)],
},
{
method: ["DELETE"],
matcher: "/admin/promotions/:id/rules",
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
},
{
method: ["DELETE"],
matcher: "/admin/promotions/:id/target-rules",
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
},
{
method: ["DELETE"],
matcher: "/admin/promotions/:id/buy-rules",
middlewares: [transformBody(AdminDeletePromotionsPromotionRulesReq)],
},
]
@@ -8,6 +8,7 @@ import {
} from "@medusajs/utils"
import { Type } from "class-transformer"
import {
ArrayNotEmpty,
IsArray,
IsBoolean,
IsEnum,
@@ -221,3 +222,9 @@ export class AdminPostPromotionsPromotionRulesReq {
@Type(() => PromotionRule)
rules: PromotionRule[]
}
export class AdminDeletePromotionsPromotionRulesReq {
@ArrayNotEmpty()
@IsString({ each: true })
rule_ids: string[]
}